|
| 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 | + # pull_request does not run for conflicted PRs, so reconcile from the trusted default branch. |
| 8 | + schedule: |
| 9 | + - cron: '*/5 * * * *' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +permissions: {} |
| 13 | + |
| 14 | +jobs: |
| 15 | + close-invalid-pr-from-workflow-run: |
| 16 | + if: > |
| 17 | + github.repository == 'github/copilot-cli' && |
| 18 | + github.event_name == 'workflow_run' && |
| 19 | + github.event.workflow_run.event == 'pull_request' && |
| 20 | + github.event.workflow_run.repository.full_name == github.repository |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + actions: read |
| 24 | + pull-requests: write |
| 25 | + concurrency: |
| 26 | + group: close-invalid-pr-${{ github.event.workflow_run.pull_requests[0].number || github.run_id }} |
| 27 | + cancel-in-progress: false |
| 28 | + steps: |
| 29 | + - name: Close invalid PR |
| 30 | + env: |
| 31 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + GH_REPO: ${{ github.repository }} |
| 33 | + WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }} |
| 34 | + run: | |
| 35 | + set -euo pipefail |
| 36 | +
|
| 37 | + trusted_workflow_id="$(gh api "repos/$GH_REPO/actions/workflows/close-invalid.yml" --jq .id)" |
| 38 | + workflow_run="$(gh api "repos/$GH_REPO/actions/runs/$WORKFLOW_RUN_ID")" |
| 39 | +
|
| 40 | + if [ "$(jq -r .workflow_id <<<"$workflow_run")" != "$trusted_workflow_id" ] || |
| 41 | + [ "$(jq -r .event <<<"$workflow_run")" != "pull_request" ] || |
| 42 | + [ "$(jq -r .repository.full_name <<<"$workflow_run")" != "$GH_REPO" ]; then |
| 43 | + echo "Workflow run is not a trusted pull_request run from $GH_REPO; skipping." |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | +
|
| 47 | + if [ "$(jq '.pull_requests | length' <<<"$workflow_run")" -ne 1 ]; then |
| 48 | + echo "Workflow run is not associated with exactly one PR; skipping." |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +
|
| 52 | + pr_number="$(jq -r .pull_requests[0].number <<<"$workflow_run")" |
| 53 | + run_head_sha="$(jq -r .head_sha <<<"$workflow_run")" |
| 54 | + run_head_repo="$(jq -r '.head_repository.full_name // empty' <<<"$workflow_run")" |
| 55 | + pr="$(gh api "repos/$GH_REPO/pulls/$pr_number")" |
| 56 | +
|
| 57 | + if [ -z "$run_head_repo" ] || |
| 58 | + [ "$(jq -r .base.repo.full_name <<<"$pr")" != "$GH_REPO" ] || |
| 59 | + [ "$(jq -r '.head.repo.full_name // empty' <<<"$pr")" != "$run_head_repo" ] || |
| 60 | + [ "$(jq -r .head.sha <<<"$pr")" != "$run_head_sha" ]; then |
| 61 | + echo "PR #$pr_number no longer matches the workflow run head; skipping." |
| 62 | + exit 0 |
| 63 | + fi |
| 64 | +
|
| 65 | + if [ "$(jq -r .state <<<"$pr")" != "open" ] || |
| 66 | + ! jq -e 'any(.labels[]?; .name == "invalid")' >/dev/null <<<"$pr"; then |
| 67 | + echo "PR #$pr_number is not open with the invalid label; skipping." |
| 68 | + exit 0 |
| 69 | + fi |
| 70 | +
|
| 71 | + gh api -X PATCH "repos/$GH_REPO/pulls/$pr_number" -f state=closed |
| 72 | +
|
| 73 | + reconcile-invalid-prs: |
| 74 | + if: > |
| 75 | + github.repository == 'github/copilot-cli' && |
| 76 | + (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') |
| 77 | + runs-on: ubuntu-latest |
| 78 | + permissions: |
| 79 | + pull-requests: write |
| 80 | + concurrency: |
| 81 | + group: close-invalid-pr-reconciliation |
| 82 | + cancel-in-progress: false |
| 83 | + steps: |
| 84 | + - name: Close open PRs with the invalid label |
| 85 | + env: |
| 86 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 87 | + GH_REPO: ${{ github.repository }} |
| 88 | + run: | |
| 89 | + set -euo pipefail |
| 90 | +
|
| 91 | + gh api --paginate "repos/$GH_REPO/pulls?state=open&per_page=100" \ |
| 92 | + --jq '.[] | select(any(.labels[]?; .name == "invalid")) | .number' | |
| 93 | + while read -r pr_number; do |
| 94 | + pr="$(gh api "repos/$GH_REPO/pulls/$pr_number")" |
| 95 | +
|
| 96 | + if [ "$(jq -r .state <<<"$pr")" = "open" ] && |
| 97 | + jq -e 'any(.labels[]?; .name == "invalid")' >/dev/null <<<"$pr"; then |
| 98 | + gh api -X PATCH "repos/$GH_REPO/pulls/$pr_number" -f state=closed |
| 99 | + fi |
| 100 | + done |
0 commit comments