Skip to content

Commit

Permalink
[ODS-6407] Improve workflow execution (#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
axelmarquezh authored Jul 29, 2024
1 parent 5aef401 commit 2d3ce56
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 36 deletions.
1 change: 0 additions & 1 deletion .github/workflows/Lib edFi.ods.standard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
with:
calling_branch: ${{ github.head_ref || github.ref_name }}
build:
if: ${{ always() }}
needs: FindStandardAndExtensionVersions
runs-on: ubuntu-latest
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ concurrency:
env:
EDFI_ODS_IMP_TOKEN: ${{ secrets.REPO_DISPATCH_TOKEN }}
REPOSITORY_OWNER: ${{ GITHUB.REPOSITORY_OWNER }}
BASE_REF: ${{ GITHUB.BASE_REF }}
HEAD_REF: ${{ GITHUB.HEAD_REF }}
REF_NAME: ${{ GITHUB.REF_NAME }}
REPOSITORY_DISPATCH_BRANCH: ${{ github.event.client_payload.branch }}
Expand Down Expand Up @@ -58,42 +59,11 @@ jobs:
}
Write-Host "Current Branch: $current_branch"
echo "current_branch=$current_branch" >> $Env:GITHUB_ENV
- name: Check Ed-Fi-ODS-Implementation Repo PR exists or not
- name: Trigger workflows in the matching PR in the Implementation repository
working-directory: ./Ed-Fi-ODS/
shell: pwsh
run: |
$accessToken = "${{ env.EDFI_ODS_IMP_TOKEN }}"
$repositoryName = "Ed-Fi-ODS-Implementation"
$apiUrl = "https://api.github.com/repos/${{ env.REPOSITORY_OWNER }}/$repositoryName/pulls?state=open&head=${{ env.REPOSITORY_OWNER }}:${{ env.current_branch }}"
$headers = @{
Authorization = "Bearer $accessToken"
Accept = "application/vnd.github.v3+json"
}
$response = Invoke-WebRequest -Uri $apiUrl -Headers $headers
# Access x-ratelimit-limit and x-ratelimit-remaining from response headers
$rateLimitLimit = $response.Headers['X-RateLimit-Limit']
$rateLimitRemaining = $response.Headers['X-RateLimit-Remaining']
Write-Host "x-ratelimit-limit: $rateLimitLimit"
Write-Host "x-ratelimit-remaining: $rateLimitRemaining"
$jsonResponse = $response | ConvertFrom-Json
foreach ($pr in $jsonResponse) {
Write-Host "current branch: ${{ env.current_branch }}"
Write-Host "ref: $($pr.head.ref)"
if ($pr.head.ref -eq "${{ env.current_branch }}")
{
Write-Host "PR #$($pr.number): $($pr.head.label)"
echo "EXIT_STEP=true">> $env:GITHUB_ENV
}
}
- name: Check for EXIT_STEP Status
if: env.EXIT_STEP == 'true'
run: |
echo "::notice::Ed-Fi-ODS-Implementation already have same branch ${{ env.current_branch }} running all Initdev builds ,so Skipping this build"
echo "EXIT_STEP value is ${{ env.EXIT_STEP }}"
.\build.githubactions.ps1 -Command TriggerImplementationRepositoryWorkflows
- name: Dispatch InitDev , Multitenancy workflow
if: env.EXIT_STEP != 'true'
uses: Codex-/return-dispatch@03a7fcd260cce601805567f86c892bd06d2719e1 #v1.12.0
Expand Down
86 changes: 85 additions & 1 deletion build.githubactions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
param(
# Command to execute, defaults to "Build".
[string]
[ValidateSet("DotnetClean", "Restore", "Build", "Test", "Pack", "Publish", "CheckoutBranch", "InstallCredentialHandler", "StandardVersions", "StandardTag", "TpdmTag")]
[ValidateSet(
"DotnetClean",
"Restore",
"Build",
"Test",
"Pack",
"Publish",
"CheckoutBranch",
"InstallCredentialHandler",
"StandardVersions",
"StandardTag",
"TpdmTag",
"TriggerImplementationRepositoryWorkflows")]
$Command = "Build",

[switch] $SelfContained,
Expand Down Expand Up @@ -296,6 +308,77 @@ function RepositoryTag {
return $versionTag
}

function TriggerImplementationRepositoryWorkflows {
<#
.SYNOPSIS
Searches for the corresponding PR in the Implementation repository; if found,
adds and removes a label to the PR, restarting all the PR workflows.
Note that the workflows must be configured to be triggered by the `unlabeled` pull_request event type.
#>

Assert-EnvironmentVariablesInitialized(@("current_branch", "REPOSITORY_OWNER", "EDFI_ODS_IMP_TOKEN"))

$headers = @{
Authorization = "Bearer $Env:EDFI_ODS_IMP_TOKEN"
Accept = "application/vnd.github.v3+json"
}
$body = @{
state = "open"
head = "${Env:REPOSITORY_OWNER}:${Env:current_branch}"
}
if ($Env:BASE_REF) {
$body.Add('base', $Env:BASE_REF)
}

Write-Host "Looking for a PR in the Implementation repository with the next parameters=$($body | ConvertTo-Json)."

$pr = Invoke-WebRequest `
-Uri "https://api.github.com/repos/$Env:REPOSITORY_OWNER/Ed-Fi-ODS-Implementation/pulls" `
-Body $body `
-Headers $headers `
| ConvertFrom-Json `
| Select-Object -First 1

if (-not $pr.number) {
Write-Host "There's no matching PR."
return
}

Write-Host "Triggering workflows in the matching PR: https://github.com/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/pull/$($pr.number)"

$label = "Trigger from ODS repo"
Invoke-WebRequest `
-Method Post `
-ContentType 'application/json' `
-Uri "https://api.github.com/repos/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/issues/$($pr.number)/labels" `
-Body @(@{labels = @($label) } | ConvertTo-Json) `
-Headers $headers `
| Out-Null

Start-Sleep -Seconds 1

Invoke-WebRequest `
-Method Delete `
-Uri "https://api.github.com/repos/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/issues/$($pr.number)/labels/$([uri]::EscapeDataString($label))" `
-Headers $headers `
| Out-Null

Write-Output "EXIT_STEP=true">> $Env:GITHUB_ENV
}

function Assert-EnvironmentVariablesInitialized {
param (
[Parameter(Mandatory = $true)]
[string[]]$Names
)

foreach ($name in $Names) {
if (-not (Test-Path "Env:$name")) {
throw "The environment variable '$name' must be initialized."
}
}
}

function Invoke-Build {
Write-Host "Building Version $version" -ForegroundColor Cyan
Invoke-Step { DotnetClean }
Expand Down Expand Up @@ -355,6 +438,7 @@ Invoke-Main {
StandardVersions { Invoke-StandardVersions }
StandardTag { Invoke-StandardTag }
TpdmTag { Invoke-TpdmTag }
TriggerImplementationRepositoryWorkflows { TriggerImplementationRepositoryWorkflows }
default { throw "Command '$Command' is not recognized" }
}
}

0 comments on commit 2d3ce56

Please sign in to comment.