|
| 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 |
0 commit comments