Skip to content

Commit 01f057a

Browse files
committed
Fallback to prior failed render run
1 parent f89a09a commit 01f057a

1 file changed

Lines changed: 82 additions & 1 deletion

File tree

Build/Agent/Build-RenderArtifactComment.ps1

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ param(
77
[string]$GitHubApiUrl = $env:GITHUB_API_URL,
88
[string]$GitHubOutputPath = $env:GITHUB_OUTPUT,
99
[string]$GitHubToken = $env:GITHUB_TOKEN,
10+
[string]$HeadRef = $env:GITHUB_HEAD_REF,
1011
[string]$HasArtifacts = $env:HAS_ARTIFACTS,
1112
[string]$PreviousCommentBody,
1213
[string]$PullRequestNumber = $env:PULL_REQUEST_NUMBER,
1314
[string]$Repository = $env:GITHUB_REPOSITORY,
1415
[string]$RunAttempt = $env:GITHUB_RUN_ATTEMPT,
1516
[string]$RunId = $env:GITHUB_RUN_ID,
1617
[string]$ServerUrl = $env:GITHUB_SERVER_URL,
17-
[string]$Sha = $env:GITHUB_SHA
18+
[string]$Sha = $env:GITHUB_SHA,
19+
[string]$WorkflowRef = $env:GITHUB_WORKFLOW_REF
1820
)
1921

2022
Set-StrictMode -Version Latest
@@ -182,6 +184,82 @@ function Get-PreviousFailureRunInfo {
182184
return $null
183185
}
184186

187+
function Get-WorkflowIdentifier {
188+
if ([string]::IsNullOrWhiteSpace($WorkflowRef)) {
189+
return 'CI.yml'
190+
}
191+
192+
$atIndex = $WorkflowRef.IndexOf('@')
193+
$workflowRefPrefix = if ($atIndex -ge 0) {
194+
$WorkflowRef.Substring(0, $atIndex)
195+
}
196+
else {
197+
$WorkflowRef
198+
}
199+
200+
$repositoryName = Get-RequiredValue -Name 'Repository' -Value $Repository
201+
$expectedPrefix = "$repositoryName/"
202+
if ($workflowRefPrefix.StartsWith($expectedPrefix, [System.StringComparison]::OrdinalIgnoreCase)) {
203+
return $workflowRefPrefix.Substring($expectedPrefix.Length)
204+
}
205+
206+
return 'CI.yml'
207+
}
208+
209+
function Get-PreviousFailureRunInfoFromWorkflowRuns {
210+
if ([string]::IsNullOrWhiteSpace($HeadRef)) {
211+
return $null
212+
}
213+
214+
$repositoryName = Get-RequiredValue -Name 'Repository' -Value $Repository
215+
$repositoryParts = $repositoryName.Split('/')
216+
if ($repositoryParts.Count -ne 2) {
217+
throw "Invalid GitHub repository value: $repositoryName"
218+
}
219+
220+
$owner = $repositoryParts[0]
221+
$repo = $repositoryParts[1]
222+
$workflowIdentifier = [System.Uri]::EscapeDataString((Get-WorkflowIdentifier))
223+
$encodedHeadRef = [System.Uri]::EscapeDataString($HeadRef)
224+
$runsUri = "$GitHubApiUrl/repos/$owner/$repo/actions/workflows/$workflowIdentifier/runs?branch=$encodedHeadRef&event=pull_request&per_page=20"
225+
$response = Invoke-GitHubApi -Method Get -Uri $runsUri
226+
$runs = @($response.workflow_runs)
227+
$currentRunId = if ([string]::IsNullOrWhiteSpace($RunId)) { $null } else { [string]$RunId }
228+
229+
foreach ($run in $runs) {
230+
if ($null -eq $run) {
231+
continue
232+
}
233+
234+
$runIdValue = [string]$run.id
235+
if (-not [string]::IsNullOrWhiteSpace($currentRunId) -and $runIdValue -eq $currentRunId) {
236+
continue
237+
}
238+
239+
if (-not [System.String]::Equals([string]$run.status, 'completed', [System.StringComparison]::OrdinalIgnoreCase)) {
240+
continue
241+
}
242+
243+
if (-not [System.String]::Equals([string]$run.conclusion, 'failure', [System.StringComparison]::OrdinalIgnoreCase)) {
244+
continue
245+
}
246+
247+
$runSha = [string]$run.head_sha
248+
$runAttemptValue = [string]$run.run_attempt
249+
if ([string]::IsNullOrWhiteSpace($runSha) -or [string]::IsNullOrWhiteSpace($runIdValue) -or [string]::IsNullOrWhiteSpace($runAttemptValue)) {
250+
continue
251+
}
252+
253+
$runShortSha = $runSha.Substring(0, [System.Math]::Min(12, $runSha.Length))
254+
return [pscustomobject]@{
255+
Label = "$runShortSha run $runIdValue.$runAttemptValue"
256+
Url = [string]$run.html_url
257+
}
258+
}
259+
260+
return $null
261+
}
262+
185263
$commentDirectory = Split-Path -Path $CommentPath -Parent
186264
if (-not (Test-Path -LiteralPath $commentDirectory)) {
187265
New-Item -ItemType Directory -Path $commentDirectory -Force | Out-Null
@@ -215,6 +293,9 @@ if ($renderArtifactsDetected) {
215293
}
216294
else {
217295
$previousFailureRun = Get-PreviousFailureRunInfo -Body (Get-PreviousCommentBody)
296+
if ($null -eq $previousFailureRun) {
297+
$previousFailureRun = Get-PreviousFailureRunInfoFromWorkflowRuns
298+
}
218299
$commentLines = @()
219300
if ($null -ne $previousFailureRun) {
220301
$commentLines += "@$FailureRunLabelMarker$($previousFailureRun.Label) -->".Substring(1)

0 commit comments

Comments
 (0)