Close Stale Deploy PRs #722
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Close Stale Deploy PRs | |
| on: | |
| pull_request: | |
| types: [closed] | |
| branches: [master] | |
| schedule: | |
| - cron: '0 6 * * *' | |
| jobs: | |
| close-stale: | |
| runs-on: ubuntu-latest | |
| if: | | |
| github.event_name == 'pull_request' && | |
| github.event.pull_request.merged == true && | |
| startsWith(github.head_ref, 'deploy/') | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Determine deploy type | |
| id: deploy-type | |
| env: | |
| HEAD_REF: ${{ github.head_ref }} | |
| MERGED_PR: ${{ github.event.pull_request.number }} | |
| run: | | |
| if [[ "$HEAD_REF" == deploy/dev-* ]]; then | |
| echo "prefix=deploy/dev-" >> "$GITHUB_OUTPUT" | |
| echo "type=dev" >> "$GITHUB_OUTPUT" | |
| elif [[ "$HEAD_REF" == deploy/prod-* ]]; then | |
| echo "prefix=deploy/prod-" >> "$GITHUB_OUTPUT" | |
| echo "type=prod" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "type=unknown" >> "$GITHUB_OUTPUT" | |
| fi | |
| echo "merged_pr=${MERGED_PR}" >> "$GITHUB_OUTPUT" | |
| - name: Close stale deploy PRs | |
| if: steps.deploy-type.outputs.type != 'unknown' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PREFIX: ${{ steps.deploy-type.outputs.prefix }} | |
| MERGED_PR: ${{ steps.deploy-type.outputs.merged_pr }} | |
| run: | | |
| gh pr list --repo "$GITHUB_REPOSITORY" --state open --json number,headRefName \ | |
| --jq ".[] | select(.headRefName | startswith(\"$PREFIX\")) | .number" | while read pr_number; do | |
| if [ -n "$pr_number" ] && [ "$pr_number" != "$MERGED_PR" ]; then | |
| echo "Closing stale PR #${pr_number}" | |
| gh pr close "$pr_number" --repo "$GITHUB_REPOSITORY" --delete-branch || true | |
| fi | |
| done | |
| close-stale-scheduled: | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'schedule' | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Close stale dev deploy PRs (keep newest) | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| prs=$(gh pr list --repo "$GITHUB_REPOSITORY" --state open --json number,headRefName \ | |
| --jq '[.[] | select(.headRefName | startswith("deploy/dev-"))] | sort_by(.number) | reverse | .[1:] | .[].number') | |
| for pr_number in $prs; do | |
| echo "Closing stale dev PR #${pr_number}" | |
| gh pr close "$pr_number" --repo "$GITHUB_REPOSITORY" --delete-branch || true | |
| done | |
| - name: Close stale prod deploy PRs (keep newest) | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| prs=$(gh pr list --repo "$GITHUB_REPOSITORY" --state open --json number,headRefName \ | |
| --jq '[.[] | select(.headRefName | startswith("deploy/prod-"))] | sort_by(.number) | reverse | .[1:] | .[].number') | |
| for pr_number in $prs; do | |
| echo "Closing stale prod PR #${pr_number}" | |
| gh pr close "$pr_number" --repo "$GITHUB_REPOSITORY" --delete-branch || true | |
| done |