From d94ede01b3c4fb03a9288d4ab69551a445bc6628 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Wed, 13 Apr 2022 15:42:01 -0400 Subject: [PATCH] [CI] Migrate all code to use the new api to set the status on github. (#14728) --- tools/devops/automation/scripts/GitHub.psm1 | 29 +++++++++- .../templates/build/upload-azure.yml | 21 +++---- .../automation/templates/common/status.yml | 57 +++++++++++++++++++ .../devops/automation/templates/mac/build.yml | 14 +++-- .../automation/templates/tests/build.yml | 5 +- .../automation/templates/tests/run-tests.yml | 20 +++---- 6 files changed, 117 insertions(+), 29 deletions(-) create mode 100644 tools/devops/automation/templates/common/status.yml diff --git a/tools/devops/automation/scripts/GitHub.psm1 b/tools/devops/automation/scripts/GitHub.psm1 index d828f696d1..515db431cb 100644 --- a/tools/devops/automation/scripts/GitHub.psm1 +++ b/tools/devops/automation/scripts/GitHub.psm1 @@ -116,7 +116,7 @@ class GitHubStatuses { Authorization = ("token {0}" -f $this.Token) } - $url = [GitHubStatuses]::GetStatusUrl() + $url = $this.GetStatusUrl() # Check if the status was already set, if it was we will override yet print a message for the user to know this action was done. $presentStatuses = Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "GET" -ContentType 'application/json' } @@ -146,6 +146,32 @@ class GitHubStatuses { return Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body ($payload | ConvertTo-json) -ContentType 'application/json' } } + + [object] SetStatus($status, $description, $context) { + return $this.SetStatus($status, $description, $context, $null) + } +} + +<# + .SYNOPSIS + Creates a new GitHubComments object from that can be used to create comments for the build. +#> +function New-GitHubStatusesObject { + param ( + + [ValidateNotNullOrEmpty ()] + [string] + $Org, + + [ValidateNotNullOrEmpty ()] + [string] + $Repo, + + [ValidateNotNullOrEmpty ()] + [string] + $Token + ) + return [GitHubStatuses]::new($Org, $Repo, $Token) } class GitHubComments { @@ -1182,3 +1208,4 @@ Export-ModuleMember -Function Push-RepositoryDispatch # new future API that uses objects. Export-ModuleMember -Function New-GitHubCommentsObject +Export-ModuleMember -Function New-GitHubStatusesObject diff --git a/tools/devops/automation/templates/build/upload-azure.yml b/tools/devops/automation/templates/build/upload-azure.yml index d358f03d44..7e26ddfcf9 100644 --- a/tools/devops/automation/templates/build/upload-azure.yml +++ b/tools/devops/automation/templates/build/upload-azure.yml @@ -121,8 +121,9 @@ steps: - pwsh: | Import-Module $Env:SYSTEM_DEFAULTWORKINGDIRECTORY\tools\devops\automation\scripts\MaciosCI.psd1 + $statuses = New-GitHubStatusesObject -Org "xamarin" -Repo "xamarin-macios" -Token $(GitHub.Token) - Dir "$(Build.SourcesDirectory)\artifacts\package" + Dir "$(Build.SourcesDirectory)\\artifacts\\package" # $Env:STORAGE_URI/ ends with a /, annoying $pkgsVirtualUrl = "$Env:STORAGE_URI" +"$(Build.SourceBranchName)/$(Build.SourceVersion)/$(Build.BuildId)/package" @@ -177,7 +178,7 @@ steps: ShouldExist = $notarizedShouldExist; }, @{ - Path = "$pkgsPath\bundle.zip" ; + Path = "$pkgsPath\\bundle.zip" ; Context = "bundle.zip" ; Description = "bundle.zip" ; TargetUrl = "$pkgsVirtualUrl/bundle.zip" ; @@ -196,9 +197,9 @@ steps: foreach ($info in $statusInfo) { if (Test-Path $info.Path -PathType Leaf) { - Set-GitHubStatus -Status "success" -Description $info.Description -TargetUrl $info.TargetUrl -Context $info.Context + $statuses.SetStatus("success", $info.Description, $info.Context, $info.TargetUrl) } elseif ($info.ShouldExist) { - Set-GitHubStatus -Status "error" -Description $info.Error -Context $info.Context + $statuses.SetStatus("error", $info.Error, $info.Context) } } if ($Env:ENABLE_DOTNET -eq "True" -and $Env:SkipNugets -ne "True") { @@ -208,17 +209,17 @@ steps: if ($nugets.Count -gt 0) { Write-Host "Setting status to success." - Set-GitHubStatus -Status "success" -Description "Nugets built." -TargetUrl "$pkgsVirtualUrl/$n" -Context "$(Build.DefinitionName) (Nugets built)" + $statuses.SetStatus("success", "Nugets built.", "$(Build.DefinitionName) (Nugets built)", "$pkgsVirtualUrl/$n") Write-Host "Publishing result is $Env:NUGETS_PUBLISHED" if ($Env:NUGETS_PUBLISHED -ne "Failed") { - Set-GitHubStatus -Status "success" -Description "Nugets published." -TargetUrl "$pkgsVirtualUrl/$n" -Context "$(Build.DefinitionName) (Nugets published)" + $statuses.SetStatus("success", "Nugets published.", "$(Build.DefinitionName) (Nugets published)", "$pkgsVirtualUrl/$n") } else { - Set-GitHubStatus -Status "error" -Description "Error when publishing nugets." -TargetUrl "$pkgsVirtualUrl/$n" -Context "$(Build.DefinitionName) (Nugets published)" + $statuses.SetStatus("error", "Error when publishing nugets.", "$(Build.DefinitionName) (Nugets published)", "$pkgsVirtualUrl/$n") } } else { Write-Host "Setting nuget status to failure." - Set-GitHubStatus -Status "error" -Description "No nugets were built." -TargetUrl "$pkgsVirtualUrl/$n" -Context "$(Build.DefinitionName) (Nugets built)" - Set-GitHubStatus -Status "error" -Description "No nugets were published." -TargetUrl "$pkgsVirtualUrl/$n" -Context "$(Build.DefinitionName) (Nugets published)" + $statuses.SetStatus("error", "No nugets were built.", "$(Build.DefinitionName) (Nugets built)", "$pkgsVirtualUrl/$n") + $statuses.SetStatus("error", "No nugets were published.", "$(Build.DefinitionName) (Nugets published)", "$pkgsVirtualUrl/$n") } } @@ -226,7 +227,7 @@ steps: $msi = Get-ChildItem -Path $pkgsPath -Filter *.msi -File -Name foreach ($n in $msi) { - Set-GitHubStatus -Status "success" -Description "$n" -TargetUrl "$pkgsVirtualUrl/$n" -Context "$n" + $statuses.SetStatus("success", "$n", "$n", "$pkgsVirtualUrl/$n") } Write-Host "Setting SBOM.PackagesPath: ${pkgsPath}" diff --git a/tools/devops/automation/templates/common/status.yml b/tools/devops/automation/templates/common/status.yml new file mode 100644 index 0000000000..eaa9a61865 --- /dev/null +++ b/tools/devops/automation/templates/common/status.yml @@ -0,0 +1,57 @@ +# This templates allows to set a status on github. Takes the following paramenters: + +parameters: + +- name: org + type: string + default: "xamarin" + +- name: repo + type: string + default: "xamarin-macios" + +- name: githubToken + type: string + +- name: status + type: string + values: + - error + - failure + - pending + - success + +- name: description + type: string + +- name: context + type: string + +- name: targetUrl + type: string + default: '' + +- name: displayName + type: string + default: 'Set GitHub status' + +- name: continueOnError + default: true + +- name: condition + default: true + +- name: timeoutInMinutes + default: 5 + +steps: + +- pwsh: | + Import-Module .\\MaciosCI.psd1 + $statuses = New-GitHubStatusesObject -Org "${{ parameters.org }}" -Repo "${{ parameters.repo }}" -Token "${{ parameters.githubToken }}" + $statuses.SetStatus("${{ parameters.status }}", "${{ parameters.description }}", "${{ parameters.context }}", "${{ parameters.targetUrl }}") + displayName: ${{ parameters.displayName }} + workingDirectory: $(System.DefaultWorkingDirectory)/xamarin-macios/tools/devops/automation/scripts + continueOnError: ${{ parameters.continueOnError }} + condition: ${{ parameters.condition }} + timeoutInMinutes: ${{ parameters.timeoutInMinutes }} diff --git a/tools/devops/automation/templates/mac/build.yml b/tools/devops/automation/templates/mac/build.yml index 2d7702d732..0f30896d32 100644 --- a/tools/devops/automation/templates/mac/build.yml +++ b/tools/devops/automation/templates/mac/build.yml @@ -23,8 +23,10 @@ steps: # if something goes wrong before we successfully complete the tests (in which case we delete the file). - pwsh: | Import-Module $Env:SYSTEM_DEFAULTWORKINGDIRECTORY\xamarin-macios\tools\devops\automation\scripts\MaciosCI.psd1 - Set-Content -Path "$Env:GITHUB_FAILURE_COMMENT_FILE" -Value "Tests on macOS $Env:CONTEXT failed for unknown reasons." - Set-GitHubStatus -Status "pending" -Description "Tests on macOS $Env:CONTEXT have started." -Context "$Env:CONTEXT" + $statuses = New-GitHubStatusesObject -Org "xamarin" -Repo "xamarin-macios" -Token $(GitHub.Token) + + Set-Content -Path "$Env:GITHUB_FAILURE_COMMENT_FILE" -Value "Tests on macOS ${{ parameters.statusContext }} failed for unknown reasons." + $statuses.SetStatus("pending", "Tests on macOS ${{ parameters.statusContext }} have started.", "${{ parameters.statusContext }}") displayName: "Initialize state" env: BUILD_REVISION: $(Build.SourceVersion) @@ -182,6 +184,7 @@ steps: - pwsh: | Import-Module $Env:SYSTEM_DEFAULTWORKINGDIRECTORY\xamarin-macios\tools\devops\automation\scripts\MaciosCI.psd1 + $statuses = New-GitHubStatusesObject -Org "xamarin" -Repo "xamarin-macios" -Token $(GitHub.Token) Write-Host "Found tests" $testsPath = "$(Build.SourcesDirectory)/artifacts/mac-test-package/tests" @@ -210,7 +213,7 @@ steps: if ($failures.Count -ne 0) { # post status and comment in the build $failedTestsStr = [string]::Join(",",$failures) - Set-GitHubStatus -Status "error" -Description "Tests on macOS $Env:CONTEXT failed ($failedTestsStr)." -Context "$Env:CONTEXT" + $statuses.SetStatus("error", "Tests on macOS ${{ parameters.statusContext }} failed ($failedTestsStr).", "${{ parameters.statusContext }}") # build message $msg = [System.Text.StringBuilder]::new() $msg.AppendLine("Failed tests are:") @@ -244,11 +247,12 @@ steps: # Make sure to report any errors - pwsh: | Import-Module $Env:SYSTEM_DEFAULTWORKINGDIRECTORY\xamarin-macios\tools\devops\automation\scripts\MaciosCI.psd1 + $statuses = New-GitHubStatusesObject -Org "xamarin" -Repo "xamarin-macios" -Token $(GitHub.Token) if (Test-Path -Path "$Env:GITHUB_FAILURE_COMMENT_FILE" -PathType Leaf) { - Set-GitHubStatus -Status "error" -Description "Tests on macOS $Env:CONTEXT failed." -Context "$Env:CONTEXT" + $statuses.SetStatus("error", "Tests on macOS ${{ parameters.statusContext }} failed.", "${{ parameters.statusContext }}") New-GitHubCommentFromFile -Header "Tests on macOS $Env:CONTEXT failed" -Emoji ":x:" -Path "$Env:GITHUB_FAILURE_COMMENT_FILE" } else { - Set-GitHubStatus -Status "success" -Description "Tests on macOS $Env:CONTEXT passed." -Context "$Env:CONTEXT" + $statuses.SetStatus("success", "Tests on macOS ${{ parameters.statusContext }} passed.", "${{ parameters.statusContext }}") New-GitHubComment -Header "Tests on macOS $Env:CONTEXT passed" -Description "Tests passed" -Message "**All** tests on macOS $Env:CONTEXT passed." -Emoji ":white_check_mark:" } displayName: 'Report results to GitHub' diff --git a/tools/devops/automation/templates/tests/build.yml b/tools/devops/automation/templates/tests/build.yml index a793901707..ec7731893b 100644 --- a/tools/devops/automation/templates/tests/build.yml +++ b/tools/devops/automation/templates/tests/build.yml @@ -78,13 +78,14 @@ steps: # 3. Cancel the pipeline and do not execute any of the following steps. - pwsh: | Import-Module ./MaciosCI.psd1 + $statuses = New-GitHubStatusesObject -Org "xamarin" -Repo "xamarin-macios" -Token $(GitHub.Token) if ( -not (Test-HDFreeSpace -Size 20)) { - Set-GitHubStatus -Status "error" -Description "Not enough free space in the host." -Context "$Env:CONTEXT" + $statuses.SetStatus("error", "Not enough free space in the host.", "${{ parameters.statusContext }}") New-GitHubComment -Header "Tests failed catastrophically on $Env:CONTEXT" -Emoji ":fire:" -Description "Not enough free space in the host." Stop-Pipeline } else { - Set-GitHubStatus -Status "pending" -Description "Device tests on VSTS have been started." -Context "$Env:CONTEXT" + $statuses.SetStatus("pending", "Device tests on VSTS have been started.", "${{ parameters.statusContext }}") } env: BUILD_REVISION: $(Build.SourceVersion) diff --git a/tools/devops/automation/templates/tests/run-tests.yml b/tools/devops/automation/templates/tests/run-tests.yml index c6ab9d77a9..ee14615776 100644 --- a/tools/devops/automation/templates/tests/run-tests.yml +++ b/tools/devops/automation/templates/tests/run-tests.yml @@ -52,17 +52,15 @@ steps: # Update the status to pending, that way the monitoring person knows that we started running the tests. Up to this # point we were just setting up the agent. -- pwsh: | - Import-Module $Env:SYSTEM_DEFAULTWORKINGDIRECTORY/xamarin-macios/tools/devops/automation/scripts/MaciosCI.psd1 - Set-GitHubStatus -Status "pending" -Context "$Env:CONTEXT" -Description "Running device tests on $Env:CONTEXT" - env: - BUILD_REVISION: $(Build.SourceVersion) - CONTEXT: ${{ parameters.statusContext }} - GITHUB_TOKEN: $(GitHub.Token) - displayName: Set pending GitHub status - continueOnError: true - condition: succeededOrFailed() # re-starting the daemon should not be an issue - timeoutInMinutes: 5 +- template: ../common/status.yml + parameters: + status: "pending" + description: "Running device tests on ${{ parameters.statusContext }}" + context: ${{ parameters.statusContext }} + githubToken: $(GitHub.Token) + continueOnError: true + condition: succeededOrFailed() # re-starting the daemon should not be an issue + timeoutInMinutes: 5 - bash: | make -C src build/generator-frameworks.g.cs