Skip to content

Commit b51ebc5

Browse files
subatoiCopilot
andauthored
Re-close invalid PRs reopened by people without write access (#64344)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0c10047 commit b51ebc5

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

‎.github/workflows/close-on-invalid-label.yaml‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
name: Close issue/PR on adding invalid label
22

33
# Closes public issues and PRs when the invalid label is added.
4+
# Re-closes invalid PRs that someone without write access reopens.
45

56
on:
67
issues:
78
types: [labeled]
8-
# pull_request_target lets the workflow close forked PRs after the invalid label is added.
9+
# pull_request_target lets the workflow close forked PRs when they're labeled invalid or reopened.
910
pull_request_target:
10-
types: [labeled]
11+
types: [labeled, reopened]
1112

1213
permissions:
1314
contents: read
14-
issues: write
15-
pull-requests: write
1615

1716
jobs:
1817
close-on-adding-invalid-label:
1918
if: github.repository == 'github/docs' && github.event.label.name == 'invalid'
2019
runs-on: ubuntu-latest
20+
permissions:
21+
contents: read
22+
issues: write
23+
pull-requests: write
2124

2225
steps:
2326
- name: Close issue
@@ -42,3 +45,29 @@ jobs:
4245
if: ${{ failure() && github.event_name != 'pull_request_target' }}
4346
with:
4447
slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }}
48+
49+
# Authors can reopen PRs they closed themselves, even after the invalid label is added.
50+
reclose-reopened-invalid-pr:
51+
if: >
52+
github.repository == 'github/docs' && github.event_name == 'pull_request_target' &&
53+
github.event.action == 'reopened' &&
54+
contains(github.event.pull_request.labels.*.name, 'invalid')
55+
runs-on: ubuntu-latest
56+
permissions:
57+
pull-requests: write
58+
steps:
59+
- name: Re-close PR unless reopened by someone with write access
60+
env:
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
PR_URL: ${{ github.event.pull_request.html_url }}
63+
SENDER: ${{ github.event.sender.login }}
64+
run: |
65+
if ! permission=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$SENDER/permission" --jq '.permission'); then
66+
echo "::warning::Couldn't look up permission for $SENDER, so treating them as read-only."
67+
permission=none
68+
fi
69+
if [[ "$permission" == "admin" || "$permission" == "write" ]]; then
70+
echo "$SENDER has $permission access, so leaving the PR open."
71+
exit 0
72+
fi
73+
gh pr close "$PR_URL" --comment 'This pull request is labeled `invalid`, so only maintainers can reopen it. If you think this is incorrect, please mention this in a corresponding issue.'

0 commit comments

Comments
 (0)