1+ # Cleanup Dependabot PRs after combined PR is merged
2+ name : Cleanup Dependabot PRs
3+
4+ on :
5+ pull_request :
6+ types : [closed]
7+ branches :
8+ - main
9+
10+ jobs :
11+ cleanup :
12+ # Only run if PR was merged and title indicates it's a combined dependabot PR
13+ if : |
14+ github.event.pull_request.merged == true &&
15+ startsWith(github.event.pull_request.title, 'chore: merge multiple dependabot updates')
16+
17+ runs-on : ubuntu-latest
18+ permissions :
19+ contents : write
20+ pull-requests : write
21+
22+ steps :
23+ - name : Extract PR numbers from description
24+ id : extract
25+ env :
26+ PR_BODY : ${{ github.event.pull_request.body }}
27+ run : |
28+ echo "Extracting Dependabot PR numbers from combined PR description..."
29+
30+ # Extract all PR numbers mentioned with "Closes #" pattern
31+ PR_NUMBERS=$(echo "$PR_BODY" | grep -oP "Closes #\K\d+" | tr '\n' ' ')
32+
33+ if [ -z "$PR_NUMBERS" ]; then
34+ echo "No Dependabot PRs found to close"
35+ echo "count=0" >> $GITHUB_OUTPUT
36+ exit 0
37+ fi
38+
39+ echo "Found PRs to close: $PR_NUMBERS"
40+ echo "pr_numbers=$PR_NUMBERS" >> $GITHUB_OUTPUT
41+ echo "count=$(echo $PR_NUMBERS | wc -w)" >> $GITHUB_OUTPUT
42+
43+ - name : Close Dependabot PRs and delete branches
44+ if : steps.extract.outputs.count > 0
45+ env :
46+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
47+ PR_NUMBERS : ${{ steps.extract.outputs.pr_numbers }}
48+ run : |
49+ echo "### 🧹 Cleaning up Dependabot PRs" >> $GITHUB_STEP_SUMMARY
50+ echo "" >> $GITHUB_STEP_SUMMARY
51+
52+ SUCCESS_COUNT=0
53+ FAIL_COUNT=0
54+
55+ for pr_num in $PR_NUMBERS; do
56+ echo "Processing PR #$pr_num..."
57+
58+ # Get PR details
59+ pr_data=$(gh pr view "$pr_num" --json state,headRefName,author 2>/dev/null || echo "")
60+
61+ if [ -z "$pr_data" ]; then
62+ echo "❌ PR #$pr_num not found or already closed" >> $GITHUB_STEP_SUMMARY
63+ FAIL_COUNT=$((FAIL_COUNT + 1))
64+ continue
65+ fi
66+
67+ state=$(echo "$pr_data" | jq -r '.state')
68+ branch=$(echo "$pr_data" | jq -r '.headRefName')
69+ author=$(echo "$pr_data" | jq -r '.author.login')
70+
71+ # Only close open Dependabot PRs
72+ if [ "$state" != "OPEN" ]; then
73+ echo "⚠️ PR #$pr_num is already $state" >> $GITHUB_STEP_SUMMARY
74+ continue
75+ fi
76+
77+ if [ "$author" != "dependabot[bot]" ]; then
78+ echo "⚠️ PR #$pr_num is not from Dependabot (author: $author)" >> $GITHUB_STEP_SUMMARY
79+ continue
80+ fi
81+
82+ # Close the PR
83+ if gh pr close "$pr_num" --comment "Closed in favor of combined PR #${{ github.event.pull_request.number }}" 2>/dev/null; then
84+ echo "✅ Closed PR #$pr_num" >> $GITHUB_STEP_SUMMARY
85+ SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
86+
87+ # Delete the branch
88+ if git push origin --delete "$branch" 2>/dev/null; then
89+ echo " 🗑️ Deleted branch: $branch" >> $GITHUB_STEP_SUMMARY
90+ else
91+ echo " ⚠️ Could not delete branch: $branch" >> $GITHUB_STEP_SUMMARY
92+ fi
93+ else
94+ echo "❌ Failed to close PR #$pr_num" >> $GITHUB_STEP_SUMMARY
95+ FAIL_COUNT=$((FAIL_COUNT + 1))
96+ fi
97+ done
98+
99+ echo "" >> $GITHUB_STEP_SUMMARY
100+ echo "**Summary:** Closed $SUCCESS_COUNT PRs, $FAIL_COUNT failed" >> $GITHUB_STEP_SUMMARY
0 commit comments