Skip to content

Commit f206d7d

Browse files
committed
Extract push + GitHub PR steps into a reusable template
1 parent e9dabf8 commit f206d7d

2 files changed

Lines changed: 91 additions & 70 deletions

File tree

common/config/azure-pipelines/bump-decoupled-deps.yaml

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -100,73 +100,8 @@ extends:
100100
displayName: 'Generate change files'
101101
condition: and(succeeded(), eq(variables.HasChanges, 'true'))
102102
103-
- bash: |
104-
set -e
105-
git push origin $(BranchName) --force
106-
displayName: 'Push branch'
107-
condition: and(succeeded(), eq(variables.HasChanges, 'true'))
108-
109-
- bash: |
110-
set -e
111-
112-
# Derive owner/repo from the git remote
113-
REPO_SLUG=$(git remote get-url origin | sed -E 's#.*github\.com[:/](.+/[^.]+)(\.git)?$#\1#')
114-
echo "Repository: ${REPO_SLUG}"
115-
116-
# Extract the authorization header that AzDO configured via persistCredentials
117-
AUTH_HEADER=$(git config --get-regexp 'http\..*\.extraheader' | head -1 | sed 's/^[^ ]* //')
118-
if [ -z "$AUTH_HEADER" ]; then
119-
echo "##[error]Could not extract authorization header from git config"
120-
exit 1
121-
fi
122-
123-
PR_TITLE="$(CommitMessage)"
124-
PR_BODY="Automated PR to bump decoupled local dependencies to the latest published versions."
125-
API_BASE="https://api.github.com/repos/${REPO_SLUG}"
126-
127-
# Helper to call the GitHub API and fail with a visible error
128-
github_api() {
129-
local RESPONSE HTTP_CODE
130-
RESPONSE=$(curl -s -w "\n%{http_code}" "$@")
131-
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
132-
BODY=$(echo "$RESPONSE" | sed '$d')
133-
134-
if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 300 ]]; then
135-
echo "$BODY"
136-
else
137-
echo "::error::GitHub API returned HTTP ${HTTP_CODE}:" >&2
138-
echo "$BODY" >&2
139-
return 1
140-
fi
141-
}
142-
143-
# Check if a PR already exists for this branch
144-
OWNER=$(echo "${REPO_SLUG}" | cut -d/ -f1)
145-
EXISTING_PR=$(github_api \
146-
-H "$AUTH_HEADER" \
147-
-H "Accept: application/vnd.github+json" \
148-
"${API_BASE}/pulls?head=${OWNER}:$(BranchName)&state=open" \
149-
| jq '.[0].number // empty')
150-
151-
if [ -n "$EXISTING_PR" ]; then
152-
echo "Updating existing PR #${EXISTING_PR}"
153-
github_api -X PATCH \
154-
-H "$AUTH_HEADER" \
155-
-H "Accept: application/vnd.github+json" \
156-
"${API_BASE}/pulls/${EXISTING_PR}" \
157-
-d "$(jq -n --arg body "$PR_BODY" '{body: $body}')"
158-
else
159-
echo "Creating new PR"
160-
github_api -X POST \
161-
-H "$AUTH_HEADER" \
162-
-H "Accept: application/vnd.github+json" \
163-
"${API_BASE}/pulls" \
164-
-d "$(jq -n \
165-
--arg title "$PR_TITLE" \
166-
--arg body "$PR_BODY" \
167-
--arg head "$(BranchName)" \
168-
--arg base "main" \
169-
'{title: $title, body: $body, head: $head, base: $base}')"
170-
fi
171-
displayName: 'Create or update GitHub PR'
172-
condition: and(succeeded(), eq(variables.HasChanges, 'true'))
103+
- template: /common/config/azure-pipelines/templates/push-and-create-github-pr.yaml@self
104+
parameters:
105+
BranchName: $(BranchName)
106+
PrTitle: $(CommitMessage)
107+
PrDescription: 'Automated PR to bump decoupled local dependencies to the latest published versions.'
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
parameters:
2+
- name: BranchName
3+
type: string
4+
- name: PrTitle
5+
type: string
6+
- name: PrDescription
7+
type: string
8+
default: ''
9+
- name: TargetBranch
10+
type: string
11+
default: 'main'
12+
- name: HasChangesVariableName
13+
type: string
14+
default: 'HasChanges'
15+
16+
steps:
17+
- bash: |
18+
set -e
19+
git push origin ${{ parameters.BranchName }} --force
20+
displayName: 'Push branch'
21+
condition: and(succeeded(), eq(variables['${{ parameters.HasChangesVariableName }}'], 'true'))
22+
23+
- bash: |
24+
set -e
25+
26+
# Derive owner/repo from the git remote
27+
REPO_SLUG=$(git remote get-url origin | sed -E 's#.*github\.com[:/](.+/[^.]+)(\.git)?$#\1#')
28+
echo "Repository: ${REPO_SLUG}"
29+
30+
# Extract the authorization header that AzDO configured via persistCredentials
31+
AUTH_HEADER=$(git config --get-regexp 'http\..*\.extraheader' | head -1 | sed 's/^[^ ]* //')
32+
if [ -z "$AUTH_HEADER" ]; then
33+
echo "##[error]Could not extract authorization header from git config. Ensure persistCredentials is enabled on the checkout step."
34+
exit 1
35+
fi
36+
37+
PR_TITLE="${{ parameters.PrTitle }}"
38+
PR_BODY="${{ parameters.PrDescription }}"
39+
API_BASE="https://api.github.com/repos/${REPO_SLUG}"
40+
41+
# Helper to call the GitHub API and fail with a visible error
42+
github_api() {
43+
local RESPONSE HTTP_CODE
44+
RESPONSE=$(curl -s -w "\n%{http_code}" "$@")
45+
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
46+
BODY=$(echo "$RESPONSE" | sed '$d')
47+
48+
if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 300 ]]; then
49+
echo "$BODY"
50+
else
51+
echo "::error::GitHub API returned HTTP ${HTTP_CODE}:" >&2
52+
echo "$BODY" >&2
53+
return 1
54+
fi
55+
}
56+
57+
# Check if a PR already exists for this branch
58+
OWNER=$(echo "${REPO_SLUG}" | cut -d/ -f1)
59+
EXISTING_PR=$(github_api \
60+
-H "$AUTH_HEADER" \
61+
-H "Accept: application/vnd.github+json" \
62+
"${API_BASE}/pulls?head=${OWNER}:${{ parameters.BranchName }}&state=open" \
63+
| jq '.[0].number // empty')
64+
65+
if [ -n "$EXISTING_PR" ]; then
66+
echo "Updating existing PR #${EXISTING_PR}"
67+
github_api -X PATCH \
68+
-H "$AUTH_HEADER" \
69+
-H "Accept: application/vnd.github+json" \
70+
"${API_BASE}/pulls/${EXISTING_PR}" \
71+
-d "$(jq -n --arg body "$PR_BODY" '{body: $body}')"
72+
else
73+
echo "Creating new PR"
74+
github_api -X POST \
75+
-H "$AUTH_HEADER" \
76+
-H "Accept: application/vnd.github+json" \
77+
"${API_BASE}/pulls" \
78+
-d "$(jq -n \
79+
--arg title "$PR_TITLE" \
80+
--arg body "$PR_BODY" \
81+
--arg head "${{ parameters.BranchName }}" \
82+
--arg base "${{ parameters.TargetBranch }}" \
83+
'{title: $title, body: $body, head: $head, base: $base}')"
84+
fi
85+
displayName: 'Create or update GitHub PR'
86+
condition: and(succeeded(), eq(variables['${{ parameters.HasChangesVariableName }}'], 'true'))

0 commit comments

Comments
 (0)