From 2d3ce56b675a56b64731fd372ef48d103e1a8bd3 Mon Sep 17 00:00:00 2001 From: Axel Marquez <158314644+axelmarquezh@users.noreply.github.com> Date: Mon, 29 Jul 2024 08:24:02 -0600 Subject: [PATCH] [ODS-6407] Improve workflow execution (#1113) --- .github/workflows/Lib edFi.ods.standard.yml | 1 - ...itDev workflows in Implementation repo.yml | 38 +------- build.githubactions.ps1 | 86 ++++++++++++++++++- 3 files changed, 89 insertions(+), 36 deletions(-) diff --git a/.github/workflows/Lib edFi.ods.standard.yml b/.github/workflows/Lib edFi.ods.standard.yml index 4f90cd9bf3..999a6c20cb 100644 --- a/.github/workflows/Lib edFi.ods.standard.yml +++ b/.github/workflows/Lib edFi.ods.standard.yml @@ -34,7 +34,6 @@ jobs: with: calling_branch: ${{ github.head_ref || github.ref_name }} build: - if: ${{ always() }} needs: FindStandardAndExtensionVersions runs-on: ubuntu-latest strategy: diff --git a/.github/workflows/Trgr InitDev workflows in Implementation repo.yml b/.github/workflows/Trgr InitDev workflows in Implementation repo.yml index f3ab3af283..6268cf5615 100644 --- a/.github/workflows/Trgr InitDev workflows in Implementation repo.yml +++ b/.github/workflows/Trgr InitDev workflows in Implementation repo.yml @@ -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 }} @@ -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 diff --git a/build.githubactions.ps1 b/build.githubactions.ps1 index 324b2a006b..dad2af40fe 100644 --- a/build.githubactions.ps1 +++ b/build.githubactions.ps1 @@ -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, @@ -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 } @@ -355,6 +438,7 @@ Invoke-Main { StandardVersions { Invoke-StandardVersions } StandardTag { Invoke-StandardTag } TpdmTag { Invoke-TpdmTag } + TriggerImplementationRepositoryWorkflows { TriggerImplementationRepositoryWorkflows } default { throw "Command '$Command' is not recognized" } } } \ No newline at end of file