Skip to content

Commit 5ea9182

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 c76f360 commit 5ea9182

2 files changed

Lines changed: 65 additions & 14 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Auto-close PR writer
2+
3+
on:
4+
workflow_run:
5+
workflows: [Auto-close PR]
6+
types: [completed]
7+
8+
permissions:
9+
issues: write
10+
pull-requests: write
11+
12+
jobs:
13+
close:
14+
name: Run
15+
if: >
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 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+
COMMENT_BODY: |
29+
At the moment we are not accepting contributions to the repository.
30+
31+
Feedback for GitHub Copilot for Xcode can be given in the [Copilot community discussions](https://github.com/github/CopilotForXcode/discussions).
32+
run: |
33+
set -euo pipefail
34+
35+
if [ -z "${PR_NUMBER:-}" ] || [ "$PR_NUMBER" = "null" ]; then
36+
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')"
37+
fi
38+
39+
if [ -z "${PR_NUMBER:-}" ]; then
40+
echo "Unable to identify a single open PR for workflow run; skipping."
41+
exit 0
42+
fi
43+
44+
pr_state="$(gh api "repos/$GH_REPO/pulls/$PR_NUMBER" --jq .state)"
45+
if [ "$pr_state" != "open" ]; then
46+
echo "PR #$PR_NUMBER is $pr_state; skipping."
47+
exit 0
48+
fi
49+
50+
head_repo="$(gh api "repos/$GH_REPO/pulls/$PR_NUMBER" --jq '.head.repo.full_name // ""')"
51+
head_ref="$(gh api "repos/$GH_REPO/pulls/$PR_NUMBER" --jq .head.ref)"
52+
if [ "$head_repo" = "$GH_REPO" ] && [[ "$head_ref" == release/* ]]; then
53+
echo "PR #$PR_NUMBER is from allowed release branch $head_ref in $head_repo; skipping."
54+
exit 0
55+
fi
56+
57+
gh api -X POST "repos/$GH_REPO/issues/$PR_NUMBER/comments" -f body="$COMMENT_BODY"
58+
gh api -X PATCH "repos/$GH_REPO/pulls/$PR_NUMBER" -f state=closed
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
name: Auto-close PR
22
on:
3-
pull_request_target:
3+
pull_request:
44
types: [opened, reopened]
55

6+
permissions:
7+
pull-requests: read
8+
69
jobs:
7-
close:
8-
name: Run
10+
signal:
11+
name: Signal
912
runs-on: ubuntu-latest
10-
permissions:
11-
pull-requests: write
1213
steps:
13-
- run: |
14-
gh pr close ${{ github.event.pull_request.number }} --comment \
15-
"At the moment we are not accepting contributions to the repository.
16-
17-
Feedback for GitHub Copilot for Xcode can be given in the [Copilot community discussions](https://github.com/github/CopilotForXcode/discussions)."
18-
if: ${{ !(startsWith(github.head_ref, 'release/') && github.event.pull_request.head.repo.full_name == github.repository) }}
19-
env:
20-
GH_REPO: ${{ github.repository }}
21-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
14+
- run: echo "Auto-close signal for PR #${{ github.event.pull_request.number }}"

0 commit comments

Comments
 (0)