Skip to content

Commit f68f6aa

Browse files
committed
ci: fix blocked PR workflow to preserve JSON in ENV
1 parent dd40258 commit f68f6aa

File tree

1 file changed

+59
-55
lines changed

1 file changed

+59
-55
lines changed

.github/workflows/blocked_prs.yml

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,59 +28,59 @@ jobs:
2828
if: github.event_name != 'workflow_dispatch'
2929
id: pr_event_setup
3030
env:
31-
REPO_L: ${{ github.event.pull_request.base.repo.name }}
32-
OWNER_L: ${{ github.event.pull_request.base.repo.owner.login }}
33-
REPO_URL_L: ${{ github.event.pull_request.base.repo.html_url }}
34-
PR_HEAD_SHA_L: ${{ github.event.pull_request.head.sha }}
35-
PR_NUMBER_L: ${{ github.event.pull_request.number }}
36-
PR_HEAD_LABEL_L: ${{ github.event.pull_request.head.label }}
37-
PR_BODY_L: ${{ github.event.pull_request.body }}
38-
PR_LABLES_L: "${{ github.event.pull_request.labels }}"
31+
PR_JSON: ${{ toJSON(github.event.pull_request) }}
3932
run: |
4033
# setup env for the rest of the workflow
4134
{
42-
echo "REPO=$REPO_L"
43-
echo "OWNER=$OWNER_L"
44-
echo "REPO_URL=$REPO_URL_L"
45-
echo "PR_NUMBER=$PR_NUMBER_L"
46-
echo "PR_HEAD_SHA=$PR_HEAD_SHA_L"
47-
echo "PR_HEAD_LABEL=$PR_HEAD_LABEL_L"
48-
echo "PR_BODY=$PR_BODY_L"
49-
echo "PR_LABELS=$(jq 'reduce .[].name as $l ([]; . + [$l])' <<< "$PR_LABELS_L" )"
35+
echo "REPO=$(jq -r '.base.repo.name' <<< "$PR_JSON")"
36+
echo "OWNER=$(jq -r '.base.repo.owner.login' <<< "$PR_JSON")"
37+
echo "PR_NUMBER=$(jq -r '.number' <<< "$PR_JSON")"
38+
echo "JOB_DATA=$(jq '
39+
{
40+
"repo": .base.repo.name,
41+
"owner": .base.repo.owner.login,
42+
"repoUrl": .base.repo.html_url,
43+
"prNumber": .number,
44+
"prHeadSha": .head.sha,
45+
"prHeadLabel": .head.label,
46+
"prBody": .body,
47+
"prLabels": reduce .labels[].name as $l ([]; . + [$l])
48+
}
49+
' <<< "$PR_JSON")"
5050
} >> "$GITHUB_ENV"
5151
5252
- name: Setup From Dispatch Event
5353
if: github.event_name == 'workflow_dispatch'
5454
id: dispatch_event_setup
5555
env:
5656
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57-
OWNER_REPO_L: ${{ github.repository }}
58-
OWNER_L: ${{ github.repository_owner }}
59-
REPO_URL_L: https://github.com/${{ github.repository }}
6057
PR_NUMBER_L: ${{ inputs.pr_id }}
6158
run: |
6259
# setup env for the rest of the workflow
6360
owner_prefix="$OWNER_L/"
6461
REPO_L="${OWNER_REPO_L#"$owner_prefix"}"
65-
PR_L=$(
62+
PR_JSON=$(
6663
gh api \
67-
-H "Accept: application/vnd.github.text+json" \
64+
-H "Accept: application/vnd.github.raw+json" \
6865
-H "X-GitHub-Api-Version: 2022-11-28" \
6966
"/repos/$OWNER_L/$REPO_L/pulls/$PR_NUMBER_L"
7067
)
71-
PR_HEAD_SHA_L=$(jq -r '.head.sha' <<< "$PR_L")
72-
PR_HEAD_LABEL_L=$(jq -r '.head.label' <<< "$PR_L")
73-
PR_BODY_L=$(jq -r '.body_text' <<< "$PR_L")
74-
PR_LABELS_L=$(jq '.labels' <<< "$PR_L")
7568
{
76-
echo "REPO=$REPO_L"
77-
echo "OWNER=$OWNER_L"
78-
echo "REPO_URL=$REPO_URL_L"
79-
echo "PR_NUMBER=$PR_NUMBER_L"
80-
echo "PR_HEAD_SHA=$PR_HEAD_SHA_L"
81-
echo "PR_HEAD_LABEL=$PR_HEAD_LABEL_L"
82-
echo "PR_BODY=$PR_BODY_L"
83-
echo "PR_LABELS=$(jq 'reduce .[].name as $l ([]; . + [$l])' <<< "$PR_LABELS_L" )"
69+
echo "REPO=$(jq -r '.base.repo.name' <<< "$PR_JSON")"
70+
echo "OWNER=$(jq -r '.base.repo.owner.login' <<< "$PR_JSON")"
71+
echo "PR_NUMBER=$(jq -r '.number' <<< "$PR_JSON")"
72+
echo "JOB_DATA=$(jq '
73+
{
74+
"repo": .base.repo.name,
75+
"owner": .base.repo.owner.login,
76+
"repoUrl": .base.repo.html_url,
77+
"prNumber": .number,
78+
"prHeadSha": .head.sha,
79+
"prHeadLabel": .head.label,
80+
"prBody": .body,
81+
"prLabels": reduce .labels[].name as $l ([]; . + [$l])
82+
}
83+
' <<< "$PR_JSON")"
8484
} >> "$GITHUB_ENV"
8585
8686
@@ -89,7 +89,7 @@ jobs:
8989
run: |
9090
PRS=$(
9191
jq '
92-
. as $body
92+
.prBody as $body
9393
| (
9494
$body | scan("blocked (?(?by)|(?on)):? #(?<num>[0-9]+)")
9595
| map({
@@ -109,7 +109,7 @@ jobs:
109109
"blocking": $prs,
110110
"numBlocking": ( $prs | length),
111111
}
112-
' <<< "$PR_BODY"
112+
' <<< "$JOB_DATA"
113113
)
114114
echo "prs=$PRS" >> "$GITHUB_OUTPUT"
115115
@@ -120,12 +120,12 @@ jobs:
120120
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
121121
run: |
122122
BLOCKED_PR_DATA=$(
123-
while read -r PR ; do
123+
while read -r pr_data ; do
124124
gh api \
125125
-H "Accept: application/vnd.github+json" \
126126
-H "X-GitHub-Api-Version: 2022-11-28" \
127-
"/repos/$OWNER/$REPO/pulls/$(jq -r '.number' <<< "$PR")" \
128-
| jq --arg type "$(jq -r '.type' <<< "$PR")" \
127+
"/repos/$OWNER/$REPO/pulls/$(jq -r '.number' <<< "$pr_data")" \
128+
| jq --arg type "$(jq -r '.type' <<< "$pr_data")" \
129129
'
130130
. | {
131131
"type": $type,
@@ -180,14 +180,14 @@ jobs:
180180
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
181181
run: |
182182
# label pr dependencies with 'blocking' if not already
183-
while read -r PR_DATA ; do
184-
if jq -e 'all(.labels[]; . != "blocking")' <<< "$PR_DATA" > /dev/null ; then
185-
PR=$(jq -r '.number' <<< "$PR_DATA")
183+
while read -r pr_data ; do
184+
if jq -e 'all(.labels[]; . != "blocking")' <<< "$pr_data" > /dev/null ; then
185+
pr=$(jq -r '.number' <<< "$pr_data")
186186
gh api \
187187
--method POST \
188188
-H "Accept: application/vnd.github+json" \
189189
-H "X-GitHub-Api-Version: 2022-11-28" \
190-
"/repos/$OWNER/$REPO/issues/$PR/labels" \
190+
"/repos/$OWNER/$REPO/issues/$pr/labels" \
191191
-f "labels[]=blocking"
192192
fi
193193
done < <(jq -c '.[]' <<< "${{steps.blocked_data.outputs.state}}")
@@ -199,20 +199,21 @@ jobs:
199199
env:
200200
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
201201
run: |
202+
pr_head_sha=$(jq -r '.prHeadSha' <<< "$JOB_DATA")
202203
# create commit Status, overwrites previous identical context
203-
while read -r PR_DATA ; do
204+
while read -r pr_data ; do
204205
DESC=$(
205-
jq -r ' "Blocking PR #" + (.number | tostring) + " is " + (if .merged then "" else "not yet " end) + "merged"' <<< "$PR_DATA"
206+
jq -r ' "Blocking PR #" + (.number | tostring) + " is " + (if .merged then "" else "not yet " end) + "merged"' <<< "$pr_data"
206207
)
207208
gh api \
208209
--method POST \
209210
-H "Accept: application/vnd.github+json" \
210211
-H "X-GitHub-Api-Version: 2022-11-28" \
211-
"/repos/${OWNER}/${REPO}/statuses/${PR_HEAD_SHA}" \
212-
-f "state=$(jq -r 'if .merged then "success" else "failure" end' <<< "$PR_DATA")" \
213-
-f "target_url=$(jq -r '.basePrUrl' <<< "$PR_DATA" )" \
212+
"/repos/${OWNER}/${REPO}/statuses/${pr_head_sha}" \
213+
-f "state=$(jq -r 'if .merged then "success" else "failure" end' <<< "$pr_data")" \
214+
-f "target_url=$(jq -r '.basePrUrl' <<< "$pr_data" )" \
214215
-f "description=$DESC" \
215-
-f "context=continuous-integration/blocked-pr-check:$(jq '.number' <<< "$PR_DATA")"
216+
-f "context=continuous-integration/blocked-pr-check:$(jq '.number' <<< "$pr_data")"
216217
done < <(jq -c '.[]' <<< "${{steps.blocked_data.outputs.state}}")
217218
218219
- name: Context Comment
@@ -223,13 +224,16 @@ jobs:
223224
COMMENT_PATH="$(pwd)/temp_comment_file.txt"
224225
touch "$COMMENT_PATH"
225226
echo "" > "$COMMENT_PATH"
226-
while read -r PR_DATA ; do
227-
BASE_PR=$(jq '.number' <<< "$PR_DATA")
228-
BASE_REF_NAME=$(jq '.baseRefName' <<< "$PR_DATA")
229-
COMPARE_URL="$REPO_URL/compare/$BASE_REF_NAME...$PR_HEAD_LABEL"
230-
STATUS=$(jq 'if .merged then ":heavy_check_mark: Merged" else ":x: Not Merged" end' <<< "$PR_DATA")
231-
TYPE=$(jq -r '.type' <<< "$PR_DATA")
232-
echo " - $TYPE #$BASE_PR $STATUS [(compare)]($COMPARE_URL)" >> "$COMMENT_PATH"
227+
pr_head_label=$(jq -r '.prHeadLabel' <<< "$JOB_DATA")
228+
while read -r pr_data ; do
229+
base_pr=$(jq -r '.number' <<< "$pr_data")
230+
base_ref_name=$(jq -r '.baseRefName' <<< "$pr_data")
231+
base_repo_owner=$(jq -r '.baseRepoOwner' <<< "$pr_data")
232+
base_repo_name=$(jq -r '.baseRepoName' <<< "$pr_data")
233+
compare_url="https://github.com/$base_repo_owner/$base_repo_name/compare/$base_ref_name...$pr_head_label"
234+
status=$(jq 'if .merged then ":heavy_check_mark: Merged" else ":x: Not Merged" end' <<< "$pr_data")
235+
type=$(jq -r '.type' <<< "$pr_data")
236+
echo " - $type #$base_pr $status [(compare)]($compare_url)" >> "$COMMENT_PATH"
233237
done < <(jq -c '.[]' <<< "${{steps.blocked_data.outputs.state}}")
234238
echo "file_path=${COMMENT_PATH}" >> "$GITHUB_OUTPUT"
235239

0 commit comments

Comments
 (0)