|
7 | 7 | [string]$GitHubApiUrl = $env:GITHUB_API_URL, |
8 | 8 | [string]$GitHubOutputPath = $env:GITHUB_OUTPUT, |
9 | 9 | [string]$GitHubToken = $env:GITHUB_TOKEN, |
| 10 | + [string]$HeadRef = $env:GITHUB_HEAD_REF, |
10 | 11 | [string]$HasArtifacts = $env:HAS_ARTIFACTS, |
11 | 12 | [string]$PreviousCommentBody, |
12 | 13 | [string]$PullRequestNumber = $env:PULL_REQUEST_NUMBER, |
13 | 14 | [string]$Repository = $env:GITHUB_REPOSITORY, |
14 | 15 | [string]$RunAttempt = $env:GITHUB_RUN_ATTEMPT, |
15 | 16 | [string]$RunId = $env:GITHUB_RUN_ID, |
16 | 17 | [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 |
18 | 20 | ) |
19 | 21 |
|
20 | 22 | Set-StrictMode -Version Latest |
@@ -182,6 +184,82 @@ function Get-PreviousFailureRunInfo { |
182 | 184 | return $null |
183 | 185 | } |
184 | 186 |
|
| 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 | + |
185 | 263 | $commentDirectory = Split-Path -Path $CommentPath -Parent |
186 | 264 | if (-not (Test-Path -LiteralPath $commentDirectory)) { |
187 | 265 | New-Item -ItemType Directory -Path $commentDirectory -Force | Out-Null |
@@ -215,6 +293,9 @@ if ($renderArtifactsDetected) { |
215 | 293 | } |
216 | 294 | else { |
217 | 295 | $previousFailureRun = Get-PreviousFailureRunInfo -Body (Get-PreviousCommentBody) |
| 296 | + if ($null -eq $previousFailureRun) { |
| 297 | + $previousFailureRun = Get-PreviousFailureRunInfoFromWorkflowRuns |
| 298 | + } |
218 | 299 | $commentLines = @() |
219 | 300 | if ($null -ne $previousFailureRun) { |
220 | 301 | $commentLines += "@$FailureRunLabelMarker$($previousFailureRun.Label) -->".Substring(1) |
|
0 commit comments