1+ name : Auto-Complete PR
2+
3+ on :
4+ schedule :
5+ # Run every 3 hours
6+ - cron : ' 0 */3 * * *'
7+ workflow_dispatch :
8+
9+ permissions :
10+ contents : write
11+ pull-requests : write
12+
13+ jobs :
14+ auto-complete-pr :
15+ runs-on : windows-latest
16+ steps :
17+ - name : Checkout repository
18+ uses : actions/checkout@v4
19+
20+ - name : Get and complete oldest PR
21+ shell : pwsh
22+ env :
23+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
24+ run : |
25+ $ErrorActionPreference = 'Stop'
26+
27+ Write-Host "Fetching open PRs..."
28+
29+ # Get all open PRs sorted by creation date (oldest first)
30+ $prs = gh pr list --json number,title,createdAt,mergeable,isDraft,headRefOid --limit 100 | ConvertFrom-Json
31+ $prs = $prs | Sort-Object createdAt
32+
33+ if ($prs.Count -eq 0) {
34+ Write-Host "No open PRs found"
35+ exit 0
36+ }
37+
38+ Write-Host "Found $($prs.Count) open PR(s)"
39+
40+ # Find the first completable PR
41+ $completablePr = $null
42+
43+ foreach ($pr in $prs) {
44+ Write-Host "`nChecking PR #$($pr.number): $($pr.title)"
45+ Write-Host " Created: $($pr.createdAt)"
46+ Write-Host " Mergeable: $($pr.mergeable)"
47+ Write-Host " Draft: $($pr.isDraft)"
48+
49+ # Skip if draft or not mergeable
50+ if ($pr.isDraft -eq $true) {
51+ Write-Host " Skipping: PR is in draft"
52+ continue
53+ }
54+
55+ if ($pr.mergeable -ne "MERGEABLE") {
56+ Write-Host " Skipping: PR is not mergeable (state: $($pr.mergeable))"
57+ continue
58+ }
59+
60+ # Check CI status using GitHub CLI
61+ Write-Host " Checking CI status..."
62+ $checksJson = gh pr checks $pr.number --json bucket,conclusion,name,status,title | ConvertFrom-Json
63+
64+ if ($checksJson.Count -eq 0) {
65+ Write-Host " No checks found, considering as passed"
66+ $completablePr = $pr
67+ break
68+ }
69+
70+ $failedChecks = $checksJson | Where-Object {
71+ $_.conclusion -notin @("success", "skipped", "neutral") -and $_.status -eq "completed"
72+ }
73+
74+ $pendingChecks = $checksJson | Where-Object {
75+ $_.status -ne "completed"
76+ }
77+
78+ if ($failedChecks.Count -gt 0) {
79+ Write-Host " Failed checks found:"
80+ foreach ($check in $failedChecks) {
81+ Write-Host " - $($check.name): $($check.conclusion)"
82+ }
83+ continue
84+ }
85+
86+ if ($pendingChecks.Count -gt 0) {
87+ Write-Host " Pending checks found:"
88+ foreach ($check in $pendingChecks) {
89+ Write-Host " - $($check.name): $($check.status)"
90+ }
91+ continue
92+ }
93+
94+ Write-Host " All checks passed!"
95+ $completablePr = $pr
96+ break
97+ }
98+
99+ if ($null -eq $completablePr) {
100+ Write-Host "`n No completable PRs found"
101+ exit 0
102+ }
103+
104+ # Complete the PR
105+ Write-Host "`n Completing PR #$($completablePr.number): $($completablePr.title)"
106+
107+ try {
108+ # Merge the PR (options: --merge, --squash, --rebase)
109+ gh pr merge $completablePr.number --merge --delete-branch
110+
111+ Write-Host "Successfully completed PR #$($completablePr.number)"
112+
113+ # Add a comment
114+ gh pr comment $completablePr.number --body "Automatically completed by workflow"
115+
116+ # Set output for workflow summary
117+ Write-Host "`n## Workflow Summary" >> $env:GITHUB_STEP_SUMMARY
118+ Write-Host "Completed PR #$($completablePr.number): $($completablePr.title)" >> $env:GITHUB_STEP_SUMMARY
119+
120+ } catch {
121+ Write-Host "Failed to complete PR #$($completablePr.number)"
122+ Write-Host "Error: $_"
123+
124+ Write-Host "`n## Workflow Summary" >> $env:GITHUB_STEP_SUMMARY
125+ Write-Host "Failed to complete PR #$($completablePr.number): $_" >> $env:GITHUB_STEP_SUMMARY
126+
127+ exit 1
128+ }
0 commit comments