Skip to content

Commit a66ac9f

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 a66ac9f

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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: true
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+
35+
Closing this pull request: its branch was updated after the pull
36+
request was opened.
37+
38+
Per our workflow, a new pull request is required when changes are made
39+
to an existing one. Please open a new pull request with the updated
40+
changes.
41+
42+
If you wish to create an in progress pull request that you can push to,
43+
please create a draft pull request.
44+
45+
Please see [GitHub Pull Request Workflow](https://docs.suricata.io/en/latest/devguide/contributing/github-pr-workflow.html).
46+
47+
EOF
48+
)
49+
50+
gh api \
51+
--method POST \
52+
"/repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" \
53+
-f body="${body}" \
54+
--silent
55+
56+
gh api \
57+
--method PATCH \
58+
"/repos/${REPOSITORY}/pulls/${PR_NUMBER}" \
59+
-f state=closed \
60+
--silent
61+
62+
# Run last, after the PR has been closed. Our own run is skipped so this
63+
# job does not show up as cancelled in the checks list.
64+
- name: Cancel running workflows for this push
65+
run: |
66+
set -euo pipefail
67+
68+
for status in queued in_progress; do
69+
gh api --paginate \
70+
"/repos/${REPOSITORY}/actions/runs?head_sha=${AFTER}&status=${status}" \
71+
--jq '.workflow_runs[].id' | while read -r run_id; do
72+
if [ "${run_id}" = "${GITHUB_RUN_ID}" ]; then
73+
continue
74+
fi
75+
gh api \
76+
--method POST \
77+
"/repos/${REPOSITORY}/actions/runs/${run_id}/cancel" \
78+
--silent || true
79+
done
80+
done

0 commit comments

Comments
 (0)