Skip to content

Commit 7ceb12a

Browse files
committed
fix: enhance pull request workflow to handle labeled and unlabeled events, improve build comment management, and cancel in-progress builds
1 parent cc2ec6b commit 7ceb12a

File tree

1 file changed

+167
-21
lines changed

1 file changed

+167
-21
lines changed

.github/workflows/pull-request.yml

Lines changed: 167 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,165 @@ name: "(Pull Request): Build"
22

33
on:
44
pull_request:
5-
types: [labeled]
5+
types: [labeled, unlabeled]
66

7-
concurrency:
8-
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
9-
cancel-in-progress: true
7+
permissions:
8+
contents: read
9+
issues: write
10+
actions: write
1011

1112
jobs:
13+
gate:
14+
if: github.event.action == 'labeled' && github.event.label.name == 'build'
15+
runs-on: ubuntu-latest
16+
outputs:
17+
should_build: ${{ steps.gate.outputs.should_build }}
18+
steps:
19+
- name: Check for in-progress build runs
20+
id: gate
21+
env:
22+
GH_TOKEN: ${{ secrets.CHANGELOG_PAT || github.token }}
23+
PR_NUMBER: ${{ github.event.pull_request.number }}
24+
run: |
25+
set -euo pipefail
26+
27+
workflow_file="pull-request.yml"
28+
other_run_ids="$(gh api "repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow_file}/runs?per_page=100" --paginate \
29+
| jq -r --argjson pr "${PR_NUMBER}" --argjson current "${GITHUB_RUN_ID}" '
30+
.workflow_runs[]
31+
| select(.status != "completed")
32+
| select(.id != $current)
33+
| select(any(.pull_requests[]?; .number == $pr))
34+
| .id
35+
')"
36+
37+
if [ -n "${other_run_ids}" ]; then
38+
echo "::notice::Another build workflow run is already in progress; ignoring this trigger."
39+
echo "::notice::In-progress run ids: ${other_run_ids//$'\n'/, }"
40+
echo "should_build=false" >> "${GITHUB_OUTPUT}"
41+
else
42+
echo "should_build=true" >> "${GITHUB_OUTPUT}"
43+
fi
44+
45+
cleanup:
46+
if: github.event.action == 'unlabeled' && github.event.label.name == 'build'
47+
runs-on: ubuntu-latest
48+
env:
49+
GH_TOKEN: ${{ secrets.CHANGELOG_PAT || github.token }}
50+
PR_NUMBER: ${{ github.event.pull_request.number }}
51+
steps:
52+
- name: Cancel running build workflows for this PR
53+
run: |
54+
set -euo pipefail
55+
56+
workflow_file="pull-request.yml"
57+
run_ids="$(gh api "repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow_file}/runs?per_page=100" --paginate \
58+
| jq -r --argjson pr "${PR_NUMBER}" --argjson current "${GITHUB_RUN_ID}" '
59+
.workflow_runs[]
60+
| select(.status != "completed")
61+
| select(.id != $current)
62+
| select(any(.pull_requests[]?; .number == $pr))
63+
| .id
64+
')"
65+
66+
if [ -z "${run_ids}" ]; then
67+
echo "::notice::No in-progress build workflow runs to cancel."
68+
exit 0
69+
fi
70+
71+
echo "::group::Canceling build workflow runs"
72+
for run_id in ${run_ids}; do
73+
echo "Canceling run ${run_id}"
74+
gh api -X POST "repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}/cancel" || true
75+
done
76+
echo "::endgroup::"
77+
78+
- name: Delete build comments
79+
run: |
80+
set -euo pipefail
81+
82+
marker="<!-- code-snippets-build-comment -->"
83+
legacy_prefixes_regex='Build started.. a link to the built zip file will appear here soon..|### Download and install'
84+
bot_logins_regex='^(code-snippets-bot|github-actions\\[bot\\])$'
85+
86+
comment_ids="$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" --paginate \
87+
| jq -r --arg marker "${marker}" \
88+
--arg legacy_prefixes_regex "${legacy_prefixes_regex}" \
89+
--arg bot_logins_regex "${bot_logins_regex}" '
90+
.[]
91+
| select(.user.login | test($bot_logins_regex))
92+
| select(.body | contains($marker) or test($legacy_prefixes_regex))
93+
| .id
94+
')"
95+
96+
if [ -z "${comment_ids}" ]; then
97+
echo "::notice::No build comments found to delete."
98+
exit 0
99+
fi
100+
101+
echo "::group::Deleting build comments"
102+
for comment_id in ${comment_ids}; do
103+
echo "Deleting comment ${comment_id}"
104+
gh api -X DELETE "repos/${GITHUB_REPOSITORY}/issues/comments/${comment_id}" || true
105+
done
106+
echo "::endgroup::"
107+
12108
comment:
13-
if: contains(github.event.pull_request.labels.*.name, 'build')
109+
needs: gate
110+
if: needs.gate.outputs.should_build == 'true'
14111
runs-on: ubuntu-latest
15-
outputs:
16-
comment_id: ${{ steps.comment.outputs.comment-id }}
112+
outputs:
113+
comment_id: ${{ steps.comment.outputs.comment_id }}
114+
env:
115+
GH_TOKEN: ${{ secrets.CHANGELOG_PAT || github.token }}
116+
PR_NUMBER: ${{ github.event.pull_request.number }}
17117
steps:
18-
- name: Comment
118+
- name: Delete previous build comments
119+
run: |
120+
set -euo pipefail
121+
122+
marker="<!-- code-snippets-build-comment -->"
123+
legacy_prefixes_regex='Build started.. a link to the built zip file will appear here soon..|### Download and install'
124+
bot_logins_regex='^(code-snippets-bot|github-actions\\[bot\\])$'
125+
126+
comment_ids="$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" --paginate \
127+
| jq -r --arg marker "${marker}" \
128+
--arg legacy_prefixes_regex "${legacy_prefixes_regex}" \
129+
--arg bot_logins_regex "${bot_logins_regex}" '
130+
.[]
131+
| select(.user.login | test($bot_logins_regex))
132+
| select(.body | contains($marker) or test($legacy_prefixes_regex))
133+
| .id
134+
')"
135+
136+
if [ -z "${comment_ids}" ]; then
137+
echo "::notice::No previous build comments found."
138+
exit 0
139+
fi
140+
141+
echo "::group::Deleting previous build comments"
142+
for comment_id in ${comment_ids}; do
143+
echo "Deleting comment ${comment_id}"
144+
gh api -X DELETE "repos/${GITHUB_REPOSITORY}/issues/comments/${comment_id}" || true
145+
done
146+
echo "::endgroup::"
147+
148+
- name: Create build comment
19149
id: comment
20-
uses: codesnippetspro/create-or-update-comment@v4
21-
with:
22-
issue-number: ${{ github.event.pull_request.number }}
23-
body: |
24-
👌 Build started.. a link to the built zip file will appear here soon..
25-
150+
run: |
151+
set -euo pipefail
152+
153+
marker="<!-- code-snippets-build-comment -->"
154+
body="$(cat <<EOF
155+
${marker}
156+
### Build
157+
Build started… a link to the built zip file will appear here soon.
158+
EOF
159+
)"
160+
161+
comment_id="$(gh api -X POST "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -f body="${body}" --jq '.id')"
162+
echo "comment_id=${comment_id}" >> "${GITHUB_OUTPUT}"
163+
26164
install:
27165
needs: comment
28166
uses: ./.github/workflows/build.yml
@@ -35,10 +173,18 @@ jobs:
35173
steps:
36174
- name: Publish comment
37175
if: ${{ needs.install.outputs.artifact_name && needs.install.outputs.artifact_url }}
38-
uses: codesnippetspro/create-or-update-comment@v4
39-
with:
40-
edit-mode: replace
41-
comment-id: ${{ needs.comment.outputs.comment_id }}
42-
body: |
43-
### Download and install
44-
📦 [${{ needs.install.outputs.artifact_name }}.${{ needs.install.outputs.version }}.zip](${{ needs.install.outputs.artifact_url }})
176+
env:
177+
GH_TOKEN: ${{ secrets.CHANGELOG_PAT || github.token }}
178+
COMMENT_ID: ${{ needs.comment.outputs.comment_id }}
179+
run: |
180+
set -euo pipefail
181+
182+
marker="<!-- code-snippets-build-comment -->"
183+
body="$(cat <<EOF
184+
${marker}
185+
### Download and install
186+
📦 [${{ needs.install.outputs.artifact_name }}.zip](${{ needs.install.outputs.artifact_url }})
187+
EOF
188+
)"
189+
190+
gh api -X PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" -f body="${body}"

0 commit comments

Comments
 (0)