Skip to content

Commit 1f6c5e7

Browse files
committed
github-ci: close PRs updated after being opened
Enforce our policy of requiring a new pull request whenever changes are made to an existing one. On any push to an open, non-draft pull request the workflow comments with the policy and closes the pull request.
1 parent 736f506 commit 1f6c5e7

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Close updated PRs
2+
3+
on:
4+
pull_request_target:
5+
types:
6+
- synchronize
7+
8+
permissions:
9+
actions: write
10+
contents: read
11+
issues: write
12+
pull-requests: write
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
16+
cancel-in-progress: false
17+
18+
jobs:
19+
close-updated-pr:
20+
name: Close updated PR
21+
if: ${{ github.event.pull_request.draft == false }}
22+
runs-on: ubuntu-latest
23+
env:
24+
GH_TOKEN: ${{ github.token }}
25+
REPOSITORY: ${{ github.repository }}
26+
PR_NUMBER: ${{ github.event.pull_request.number }}
27+
AFTER: ${{ github.event.after }}
28+
steps:
29+
- name: Close updated PR
30+
run: |
31+
set -euo pipefail
32+
33+
body=$(cat <<'EOF'
34+
Closing this pull request: its branch was updated after the pull
35+
request was opened.
36+
37+
Per our policy, a new pull request is required when changes are made
38+
to an existing one. Please open a new pull request with the updated
39+
changes.
40+
EOF
41+
)
42+
43+
gh api \
44+
--method POST \
45+
"/repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" \
46+
-f body="${body}" \
47+
--silent
48+
49+
gh api \
50+
--method PATCH \
51+
"/repos/${REPOSITORY}/pulls/${PR_NUMBER}" \
52+
-f state=closed \
53+
--silent
54+
55+
# Run last: cancelling the runs for this push also cancels this run, so
56+
# this must come after the PR has already been closed.
57+
- name: Cancel running workflows for this push
58+
run: |
59+
set -euo pipefail
60+
61+
for status in queued in_progress; do
62+
gh api --paginate \
63+
"/repos/${REPOSITORY}/actions/runs?head_sha=${AFTER}&status=${status}" \
64+
--jq '.workflow_runs[].id' | while read -r run_id; do
65+
gh api \
66+
--method POST \
67+
"/repos/${REPOSITORY}/actions/runs/${run_id}/cancel" \
68+
--silent || true
69+
done
70+
done

0 commit comments

Comments
 (0)