@@ -9,10 +9,13 @@ $ErrorActionPreference = "Stop"
99$ProgressPreference = " SilentlyContinue"
1010Set-StrictMode - Version 2.0
1111
12+ # Import EnlistmentHelperFunctions module
13+ Import-Module " $PSScriptRoot \..\..\..\build\scripts\EnlistmentHelperFunctions.psm1" - DisableNameChecking
14+
1215Write-Host " Fetching open pull requests..."
1316
14- # Get all open pull requests
15- $prs = gh pr list -- state open -- json number, title, url -- limit 1000 | ConvertFrom-Json
17+ # Get all open pull requests with mergeable state
18+ $prs = gh pr list -- state open -- json number, title, url, mergeable -- limit 1000 | ConvertFrom-Json
1619
1720Write-Host " Found $ ( $prs.Count ) open pull requests"
1821
@@ -29,9 +32,18 @@ foreach ($pr in $prs) {
2932 Write-Host " "
3033 Write-Host " Checking PR #$ ( $pr.number ) : $ ( $pr.title ) "
3134
32- # Get checks for this PR
35+ # Check if PR is mergeable
36+ if ($pr.mergeable -ne " MERGEABLE" ) {
37+ Write-Host " PR is not in MERGEABLE state (current: $ ( $pr.mergeable ) ), skipping"
38+ continue
39+ }
40+
41+ # Get checks for this PR with retry
42+ $checks = $null
3343 try {
34- $checks = gh pr checks $pr.number -- json name, state, bucket, completedAt, link | ConvertFrom-Json
44+ $checks = Invoke-CommandWithRetry - ScriptBlock {
45+ gh pr checks $pr.number -- json name, state, bucket, completedAt, link | ConvertFrom-Json
46+ } - RetryCount $maxRetries - FirstDelay 2 - MaxWaitBetweenRetries 8
3547 }
3648 catch {
3749 Write-Host " ✗ Failed to get checks for PR: $_ "
@@ -67,51 +79,34 @@ foreach ($pr in $prs) {
6779
6880 Write-Host " Status check is older than $thresholdHours hours, requesting rerun..."
6981
70- # Try to rerequest the check with retries
82+ # Try to rerequest the check with retries using Invoke-CommandWithRetry
7183 $prFailed = $false
72- for ($retry = 0 ; $retry -lt $maxRetries ; $retry ++ ) {
73- try {
74- if ($retry -gt 0 ) {
75- # Exponential backoff: 2s, 4s, 8s
76- $delaySeconds = 2 * [Math ]::Pow(2 , $retry - 1 )
77- Write-Host " Retry attempt $ ( $retry + 1 ) /$maxRetries (waiting $delaySeconds seconds)..."
78- Start-Sleep - Seconds $delaySeconds
79- }
80-
81- # Rerequest the check by re-running the workflow
82- # First, get the workflow run ID from the check link
83- if ($statusCheck.link -match ' /runs/(\d+)' ) {
84- $runId = $matches [1 ]
85- # Validate run ID is a positive integer
86- if ([int ]$runId -gt 0 ) {
84+ try {
85+ # Extract run ID from the check link
86+ if ($statusCheck.link -match ' /runs/(\d+)' ) {
87+ $runId = $matches [1 ]
88+ # Validate run ID is a positive integer
89+ if ([int ]$runId -gt 0 ) {
90+ Invoke-CommandWithRetry - ScriptBlock {
8791 gh run rerun $runId - R $env: GITHUB_REPOSITORY | Out-Null
88- Write-Host " ✓ Successfully triggered re-run of workflow (run ID: $runId )"
89- $restarted ++
90- break
91- }
92- else {
93- Write-Host " ✗ Invalid run ID extracted: $runId "
94- $prFailed = $true
95- break
96- }
92+ } - RetryCount $maxRetries - FirstDelay 2 - MaxWaitBetweenRetries 8
93+ Write-Host " ✓ Successfully triggered re-run of workflow (run ID: $runId )"
94+ $restarted ++
9795 }
9896 else {
99- Write-Host " ✗ Could not extract run ID from link : $ ( $statusCheck .link ) "
97+ Write-Host " ✗ Invalid run ID extracted : $runId "
10098 $prFailed = $true
101- break
10299 }
103100 }
104- catch {
105- $errorMsg = $_.Exception.Message
106- if ($retry -eq $maxRetries - 1 ) {
107- Write-Host " ✗ Failed to restart workflow after $maxRetries attempts: $errorMsg "
108- $prFailed = $true
109- }
110- else {
111- Write-Host " ⚠ Attempt $ ( $retry + 1 ) failed: $errorMsg "
112- }
101+ else {
102+ Write-Host " ✗ Could not extract run ID from link: $ ( $statusCheck.link ) "
103+ $prFailed = $true
113104 }
114105 }
106+ catch {
107+ Write-Host " ✗ Failed to restart workflow: $_ "
108+ $prFailed = $true
109+ }
115110
116111 # Increment failed counter once per PR if any attempt failed
117112 if ($prFailed ) {
0 commit comments