Skip to content

Commit 50fbef9

Browse files
Copilotmarilene38
andcommitted
Improve auto-close PR workflow: idempotency, security, reliability, and observability
Co-authored-by: marilene38 <135267736+marilene38@users.noreply.github.com>
1 parent 4a91f75 commit 50fbef9

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

.github/workflows/auto-close-pr.yml

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ name: Auto-close PR
22

33
on:
44
pull_request_target:
5-
types: [opened, reopened, synchronize]
5+
types: [opened, reopened]
66

77
permissions:
88
pull-requests: write
9+
issues: write
910

1011
concurrency:
1112
group: auto-close-pr-${{ github.event.pull_request.number }}
@@ -15,23 +16,54 @@ jobs:
1516
close:
1617
name: Close fork PRs with guidance
1718
runs-on: ubuntu-latest
19+
timeout-minutes: 5
1820
steps:
21+
- name: Debug context
22+
env:
23+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
GH_REPO: ${{ github.repository }}
25+
run: |
26+
has_auto_closed_label="${{ contains(github.event.pull_request.labels.*.name, 'auto-closed') }}"
27+
echo "pr_number=${{ github.event.pull_request.number }}"
28+
echo "pr_state=${{ github.event.pull_request.state }}"
29+
echo "head_repo=${{ github.event.pull_request.head.repo.full_name }}"
30+
echo "base_repo=${{ github.repository }}"
31+
echo "author_association=${{ github.event.pull_request.author_association }}"
32+
echo "event_action=${{ github.event.action }}"
33+
echo "has_auto_closed_label=$has_auto_closed_label"
34+
1935
- name: Close PR from fork with message
2036
if: >-
2137
github.event.pull_request.head.repo.full_name != github.repository &&
22-
github.event.pull_request.state == 'open'
38+
github.event.pull_request.state == 'open' &&
39+
github.event.pull_request.author_association == 'NONE' &&
40+
!contains(github.event.pull_request.labels.*.name, 'auto-closed')
2341
env:
2442
GH_REPO: ${{ github.repository }}
2543
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2644
PR_NUMBER: ${{ github.event.pull_request.number }}
2745
run: |
2846
set -euo pipefail
29-
gh pr close "$PR_NUMBER" \
30-
--reason not_planned \
31-
--comment "$(cat <<'EOF'
47+
COMMENT="$(cat <<'EOF'
3248
Thanks for the pull request! At the moment we are not accepting contributions to this repository.
3349
3450
Feedback for GitHub Copilot for Xcode can be shared in the Copilot community discussions:
3551
https://github.com/orgs/community/discussions/categories/copilot
3652
EOF
3753
)"
54+
max_attempts=3
55+
for attempt in $(seq 1 "$max_attempts"); do
56+
if gh pr close "$PR_NUMBER" --reason not_planned --comment "$COMMENT"; then
57+
break
58+
elif [ "$attempt" -eq "$max_attempts" ]; then
59+
echo "All $max_attempts attempts to close PR failed" >&2
60+
exit 1
61+
else
62+
echo "Attempt $attempt failed, retrying in $((attempt * 5))s..."
63+
sleep $((attempt * 5))
64+
fi
65+
done
66+
gh label create "auto-closed" --force --color "d93f0b" --description "PR was automatically closed" \
67+
|| echo "::warning::Failed to create/update auto-closed label; label may not exist in repository"
68+
gh pr edit "$PR_NUMBER" --add-label "auto-closed"
69+
echo "✅ PR #$PR_NUMBER closed and labeled \`auto-closed\`" >> "$GITHUB_STEP_SUMMARY"

0 commit comments

Comments
 (0)