Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GetArtifacts when baselineWorkflowID is not specified #848

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading