Skip to content

Commit

Permalink
Fix GetArtifacts when baselineWorkflowID is not specified (#848)
Browse files Browse the repository at this point in the history
_**Issue**_: 
`GetArtifacts` doesn't look for latest successful CICD run, when
`baselineWorkflowID` is not set.

_**Solution**_:
Call `FindLatestSuccessfulCICDRun` in `GetArtifacts` when
`baselineWorkflowID` is not specified.
  • Loading branch information
mazhelez authored Dec 11, 2023
1 parent e9c7f5e commit 1753468
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 122 deletions.
119 changes: 0 additions & 119 deletions Actions/DetermineBaselineWorkflowRun/DetermineBaselineWorkflowRun.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,125 +9,6 @@

Import-Module (Join-Path $PSScriptRoot '..\Github-Helper.psm1' -Resolve)

<#
Checks if all build jobs in a workflow run completed successfully.
#>
function CheckBuildJobsInWorkflowRun {
Param(
[Parameter(Mandatory = $true)]
[string] $token,
[Parameter(Mandatory = $true)]
[string] $repository,
[Parameter(Mandatory = $true)]
[string] $workflowRunId
)

$headers = GetHeader -token $token
$per_page = 100
$page = 1

$allSuccessful = $true
$anySuccessful = $false

while($true) {
$jobsURI = "https://api.github.com/repos/$repository/actions/runs/$workflowRunId/jobs?per_page=$per_page&page=$page"
Write-Host "- $jobsURI"
$workflowJobs = InvokeWebRequest -Headers $headers -Uri $jobsURI | ConvertFrom-Json

if($workflowJobs.jobs.Count -eq 0) {
# No more jobs, breaking out of the loop
break
}

$buildJobs = @($workflowJobs.jobs | Where-Object { $_.name.StartsWith('Build ') })

if($buildJobs.conclusion -eq 'success') {
$anySuccessful = $true
}

if($buildJobs.conclusion -ne 'success') {
# If there is a build job that is not successful, there is not need to check further
$allSuccessful = $false
break
}

$page += 1
}

return ($allSuccessful -and $anySuccessful)
}

<#
Gets the last successful CICD run ID for the specified repository and branch.
Successful CICD runs are those that have a workflow run named ' CI/CD' and successfully built all the projects.
If no successful CICD run is found, 0 is returned.
#>
function FindLatestSuccessfulCICDRun {
Param(
[Parameter(Mandatory = $true)]
[string] $repository,
[Parameter(Mandatory = $true)]
[string] $branch,
[Parameter(Mandatory = $true)]
[string] $token
)

$headers = GetHeader -token $token
$lastSuccessfulCICDRun = 0
$per_page = 100
$page = 1

Write-Host "Finding latest successful CICD run for branch $branch in repository $repository"

# Get the latest CICD workflow run
while($true) {
$runsURI = "https://api.github.com/repos/$repository/actions/runs?per_page=$per_page&page=$page&exclude_pull_requests=true&status=completed&branch=$branch"
Write-Host "- $runsURI"
$workflowRuns = InvokeWebRequest -Headers $headers -Uri $runsURI | ConvertFrom-Json

if($workflowRuns.workflow_runs.Count -eq 0) {
# No more workflow runs, breaking out of the loop
break
}

$CICDRuns = @($workflowRuns.workflow_runs | Where-Object { $_.name -eq ' CI/CD' })

foreach($CICDRun in $CICDRuns) {
if($CICDRun.conclusion -eq 'success') {
# CICD run is successful
$lastSuccessfulCICDRun = $CICDRun.id
break
}

# CICD run is considered successful if all build jobs were successful
$areBuildJobsSuccessful = CheckBuildJobsInWorkflowRun -workflowRunId $($CICDRun.id) -token $token -repository $repository

if($areBuildJobsSuccessful) {
$lastSuccessfulCICDRun = $CICDRun.id
Write-Host "Found last successful CICD run: $($lastSuccessfulCICDRun), from $($CICDRun.created_at)"
break
}

Write-Host "CICD run $($CICDRun.id) is not successful. Skipping."
}

if($lastSuccessfulCICDRun -ne 0) {
break
}

$page += 1
}

if($lastSuccessfulCICDRun -ne 0) {
Write-Host "Last successful CICD run for branch $branch in repository $repository is $lastSuccessfulCICDRun"
} else {
Write-Host "No successful CICD run found for branch $branch in repository $repository"
}

return $lastSuccessfulCICDRun
}

$workflowRunID = FindLatestSuccessfulCICDRun -token $token -repository $repository -branch $branch

# Set output variables
Expand Down
128 changes: 127 additions & 1 deletion Actions/Github-Helper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,126 @@ function Set-JsonContentLF {
}
}

<#
Checks if all build jobs in a workflow run completed successfully.
#>
function CheckBuildJobsInWorkflowRun {
Param(
[Parameter(Mandatory = $true)]
[string] $token,
[Parameter(Mandatory = $true)]
[string] $repository,
[Parameter(Mandatory = $true)]
[string] $workflowRunId
)

$headers = GetHeader -token $token
$per_page = 100
$page = 1

$allSuccessful = $true
$anySuccessful = $false

while($true) {
$jobsURI = "https://api.github.com/repos/$repository/actions/runs/$workflowRunId/jobs?per_page=$per_page&page=$page"
Write-Host "- $jobsURI"
$workflowJobs = InvokeWebRequest -Headers $headers -Uri $jobsURI | ConvertFrom-Json

if($workflowJobs.jobs.Count -eq 0) {
# No more jobs, breaking out of the loop
break
}

$buildJobs = @($workflowJobs.jobs | Where-Object { $_.name.StartsWith('Build ') })

if($buildJobs.conclusion -eq 'success') {
$anySuccessful = $true
}

if($buildJobs.conclusion -ne 'success') {
# If there is a build job that is not successful, there is not need to check further
$allSuccessful = $false
break
}

$page += 1
}

return ($allSuccessful -and $anySuccessful)
}

<#
Gets the last successful CICD run ID for the specified repository and branch.
Successful CICD runs are those that have a workflow run named ' CI/CD' and successfully built all the projects.
If no successful CICD run is found, 0 is returned.
#>
function FindLatestSuccessfulCICDRun {
Param(
[Parameter(Mandatory = $true)]
[string] $repository,
[Parameter(Mandatory = $true)]
[string] $branch,
[Parameter(Mandatory = $true)]
[string] $token
)

$headers = GetHeader -token $token
$lastSuccessfulCICDRun = 0
$per_page = 100
$page = 1

Write-Host "Finding latest successful CICD run for branch $branch in repository $repository"

# Get the latest CICD workflow run
while($true) {
$runsURI = "https://api.github.com/repos/$repository/actions/runs?per_page=$per_page&page=$page&exclude_pull_requests=true&status=completed&branch=$branch"
Write-Host "- $runsURI"
$workflowRuns = InvokeWebRequest -Headers $headers -Uri $runsURI | ConvertFrom-Json

if($workflowRuns.workflow_runs.Count -eq 0) {
# No more workflow runs, breaking out of the loop
break
}

$CICDRuns = @($workflowRuns.workflow_runs | Where-Object { $_.name -eq ' CI/CD' })

foreach($CICDRun in $CICDRuns) {
if($CICDRun.conclusion -eq 'success') {
# CICD run is successful
$lastSuccessfulCICDRun = $CICDRun.id
break
}

# CICD run is considered successful if all build jobs were successful
$areBuildJobsSuccessful = CheckBuildJobsInWorkflowRun -workflowRunId $($CICDRun.id) -token $token -repository $repository

if($areBuildJobsSuccessful) {
$lastSuccessfulCICDRun = $CICDRun.id
Write-Host "Found last successful CICD run: $($lastSuccessfulCICDRun), from $($CICDRun.created_at)"
break
}

Write-Host "CICD run $($CICDRun.id) is not successful. Skipping."
}

if($lastSuccessfulCICDRun -ne 0) {
break
}

$page += 1
}

if($lastSuccessfulCICDRun -ne 0) {
Write-Host "Last successful CICD run for branch $branch in repository $repository is $lastSuccessfulCICDRun"
} else {
Write-Host "No successful CICD run found for branch $branch in repository $repository"
}

return $lastSuccessfulCICDRun
}


<#
Gets the non-expired artifacts from the specified CICD run.
#>
Expand Down Expand Up @@ -813,7 +933,7 @@ function GetArtifacts {
[Parameter(Mandatory = $true)]
[string] $version,
[Parameter(Mandatory = $false)]
[string] $baselineWorkflowID = '0'
[string] $baselineWorkflowID = 'none'
)

$headers = GetHeader -token $token
Expand All @@ -822,9 +942,15 @@ function GetArtifacts {
# For latest version, use the artifacts from the last successful CICD run
if($version -eq '*') {
if($baselineWorkflowID -eq '0' -or $baselineWorkflowID -eq '') {
# If the baseline workflow ID is 0 or empty, it means that there is no baseline workflow ID
return @()
}

if($baselineWorkflowID -eq 'none') {
# If the baseline workflow ID is 'none', it means that we need to find the latest successful CICD run
$baselineWorkflowID = FindLatestSuccessfulCICDRun -repository $repository -branch $branch -token $token
}

$result = GetArtifactsFromWorkflowRun -workflowRun $baselineWorkflowID -token $token -api_url $api_url -repository $repository -mask $mask -projects $projects
return $result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ on:
baselineWorkflowRunId:
description: ID of the baseline workflow run, from where to download the current project dependencies, in case they are not built in the current workflow run
required: false
default: '0'
default: 'none'
type: string
secrets:
description: A comma-separated string with the names of the secrets, required for the workflow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ on:
baselineWorkflowRunId:
description: ID of the baseline workflow run, from where to download the current project dependencies, in case they are not built in the current workflow run
required: false
default: '0'
default: 'none'
type: string
secrets:
description: A comma-separated string with the names of the secrets, required for the workflow.
Expand Down

0 comments on commit 1753468

Please sign in to comment.