From f0507395a33d341c0a3a91c60340d437da74b694 Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Fri, 22 Sep 2023 15:51:12 -0700 Subject: [PATCH 1/2] Update comment.yml: add exponential backoff to adding `MergeRequested` label https://github.com/Azure/azure-sdk-tools/issues/6768 --- .github/workflows/comment.yml | 36 ++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml index db695baba900..ebb489c89f99 100644 --- a/.github/workflows/comment.yml +++ b/.github/workflows/comment.yml @@ -28,11 +28,37 @@ jobs: return } $label = 'MergeRequested' - Write-Host "gh pr edit $($payload.issue.number) --add-label `"$label`"" - gh pr edit $payload.issue.html_url --add-label "$label" - if ($LASTEXITCODE) { - Write-Warning "Failed to add label" - exit $LASTEXITCODE + + $retryCount = 0 + $maxRetries = 5 + $retryDelay = 5 # Initial retry delay in seconds + + while ($retryCount -lt $maxRetries) { + Write-Host "Attempt $(retryCount+1) out of $(maxRetries): gh pr edit $($payload.issue.number) --add-label `"$label`"" + gh pr edit $payload.issue.html_url --add-label "$label" + + if ($LASTEXITCODE -eq 0) { + Write-Host "Label added successfully on attempt $(retryCount+1) out of $maxRetries" + break + } else { + Write-Warning "Failed to add label on attempt $(retryCount+1) out of $maxRetries" + $retryCount++ + if ($retryCount -lt $maxRetries) { + # Exponential backoff in seconds: + # 5 = 1*5 + # 10 = 2*5 + # 20 = 4*5 + # 40 = 8*5 + $retryDelay = [math]::Pow(2, $retryCount) * $retryDelay + Write-Host "Sleeping for $retryDelay seconds." + Start-Sleep -Seconds $retryDelay + } + } + } + + if ($retryCount -ge $maxRetries) { + Write-Error "Max retry attempts of $maxRetries exhausted. Exiting with error." + exit 1 } env: PAYLOAD: ${{ toJson(github.event) }} From d23f12377368f995d06036e8774f5e043907fb98 Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Fri, 22 Sep 2023 16:49:03 -0700 Subject: [PATCH 2/2] Update comment.yml: bugfixes --- .github/workflows/comment.yml | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml index ebb489c89f99..cbab2770cb62 100644 --- a/.github/workflows/comment.yml +++ b/.github/workflows/comment.yml @@ -16,15 +16,15 @@ jobs: - name: Process comment shell: pwsh run: | - $payload = echo $env:PAYLOAD | ConvertFrom-Json -AsHashtable + $payload = Write-Output $env:PAYLOAD | ConvertFrom-Json -AsHashtable if (!$payload.comment -or !$payload.comment.body) { - Write-Host "Empty comment, returning..." + Write-Host "Skipping: empty comment." return } $body = $payload.comment.body.Trim().ToLowerInvariant() $expected = '/pr requestmerge' if ($body -ne $expected) { - Write-Host "Comment did not equal '$expected', skipping..." + Write-Host "Skipping: comment did not equal '$expected'." return } $label = 'MergeRequested' @@ -34,34 +34,33 @@ jobs: $retryDelay = 5 # Initial retry delay in seconds while ($retryCount -lt $maxRetries) { - Write-Host "Attempt $(retryCount+1) out of $(maxRetries): gh pr edit $($payload.issue.number) --add-label `"$label`"" + + Write-Host "Attempt $($retryCount+1) out of $($maxRetries): gh pr edit $($payload.issue.number) --add-label `"$label`"" gh pr edit $payload.issue.html_url --add-label "$label" if ($LASTEXITCODE -eq 0) { - Write-Host "Label added successfully on attempt $(retryCount+1) out of $maxRetries" + Write-Host "Label added successfully on attempt $($retryCount+1) out of $($maxRetries)." break } else { - Write-Warning "Failed to add label on attempt $(retryCount+1) out of $maxRetries" + Write-Warning "Failed to add label on attempt $($retryCount+1) out of $($maxRetries)." $retryCount++ if ($retryCount -lt $maxRetries) { - # Exponential backoff in seconds: - # 5 = 1*5 - # 10 = 2*5 - # 20 = 4*5 - # 40 = 8*5 - $retryDelay = [math]::Pow(2, $retryCount) * $retryDelay - Write-Host "Sleeping for $retryDelay seconds." + # $retryDelay = 5 exponential backoff in seconds: + # attempt 2: 5 = 1*5 + # attempt 3: 10 = 2*5 + # attempt 4: 20 = 4*5 + # attempt 5: 40 = 8*5 + Write-Host "Sleeping for $retryDelay seconds..." Start-Sleep -Seconds $retryDelay + $retryDelay = $retryDelay * 2 } } } if ($retryCount -ge $maxRetries) { - Write-Error "Max retry attempts of $maxRetries exhausted. Exiting with error." + Write-Error "Max retry attempts of $maxRetries exhausted. Exiting with error ('exit 1')." exit 1 } env: PAYLOAD: ${{ toJson(github.event) }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - -