Skip to content

Commit 476581c

Browse files
mrecachinasCopilot
andcommitted
Migrate close-on-PR workflow off pull_request_target
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ef627e1 commit 476581c

2 files changed

Lines changed: 76 additions & 21 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Close invalid PR writer
2+
3+
on:
4+
workflow_run:
5+
workflows: [Close issue/PR on adding invalid label]
6+
types: [completed]
7+
8+
permissions:
9+
issues: read
10+
pull-requests: write
11+
12+
jobs:
13+
close-invalid-pr:
14+
if: >
15+
github.repository == 'github/copilot-cli' &&
16+
github.event.workflow_run.event == 'pull_request' &&
17+
github.event.workflow_run.conclusion == 'success' &&
18+
github.event.workflow_run.repository.full_name == github.repository
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Close invalid PR
22+
env:
23+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
GH_REPO: ${{ github.repository }}
25+
HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }}
26+
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
27+
PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
28+
run: |
29+
set -euo pipefail
30+
31+
if [ -z "${PR_NUMBER:-}" ] || [ "$PR_NUMBER" = "null" ]; then
32+
PR_NUMBER="$(gh api --method GET "repos/$GH_REPO/pulls" -f state=open -f head="$HEAD_OWNER:$HEAD_BRANCH" --jq 'if length == 1 then .[0].number else empty end')"
33+
fi
34+
35+
if [ -z "${PR_NUMBER:-}" ]; then
36+
echo "Unable to identify a single open PR for workflow run; skipping."
37+
exit 0
38+
fi
39+
40+
pr_state="$(gh api "repos/$GH_REPO/pulls/$PR_NUMBER" --jq .state)"
41+
if [ "$pr_state" != "open" ]; then
42+
echo "PR #$PR_NUMBER is $pr_state; skipping."
43+
exit 0
44+
fi
45+
46+
if ! gh api "repos/$GH_REPO/issues/$PR_NUMBER/labels" --jq '.[].name' | grep -Fxq invalid; then
47+
echo "PR #$PR_NUMBER does not currently have the invalid label; skipping."
48+
exit 0
49+
fi
50+
51+
gh api -X PATCH "repos/$GH_REPO/pulls/$PR_NUMBER" -f state=closed
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
name: Close issue/PR on adding invalid label
22

3-
# **What it does**: This action closes issues that are labeled as invalid in the repo.
3+
# **What it does**: This action closes issues and PRs that are labeled as invalid in the repo.
44

55
on:
66
issues:
77
types: [labeled]
8-
pull_request_target:
8+
pull_request:
99
types: [labeled]
1010

11-
permissions:
12-
contents: read
13-
issues: write
14-
pull-requests: write
11+
permissions: {}
1512

1613
jobs:
17-
close-on-adding-invalid-label:
18-
if:
19-
github.repository == 'github/copilot-cli' && github.event.label.name ==
20-
'invalid'
14+
close-issue-on-adding-invalid-label:
15+
if: >
16+
github.repository == 'github/copilot-cli' &&
17+
github.event_name == 'issues' &&
18+
github.event.label.name == 'invalid'
2119
runs-on: ubuntu-latest
22-
20+
permissions:
21+
issues: write
2322
steps:
2423
- name: Close issue
25-
if: ${{ github.event_name == 'issues' }}
2624
env:
27-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28-
URL: ${{ github.event.issue.html_url }}
29-
run: gh issue close $URL
25+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
GH_REPOSITORY: ${{ github.repository }}
27+
ISSUE_NUMBER: ${{ github.event.issue.number }}
28+
run: gh api -X PATCH "repos/$GH_REPOSITORY/issues/$ISSUE_NUMBER" -f state=closed
3029

31-
- name: Close PR
32-
if: ${{ github.event_name == 'pull_request_target' }}
33-
env:
34-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35-
URL: ${{ github.event.pull_request.html_url }}
36-
run: gh pr close $URL
30+
signal-invalid-pr-label:
31+
if: >
32+
github.repository == 'github/copilot-cli' &&
33+
github.event_name == 'pull_request' &&
34+
github.event.label.name == 'invalid'
35+
runs-on: ubuntu-latest
36+
permissions:
37+
pull-requests: read
38+
steps:
39+
- name: Record invalid PR label signal
40+
run: echo "Invalid label signal for PR #${{ github.event.pull_request.number }}"

0 commit comments

Comments
 (0)