From 11a1c246fa8848facd87f8ba706b733c98f5de81 Mon Sep 17 00:00:00 2001 From: VS MobileTools Engineering Service 2 Date: Tue, 2 Feb 2021 19:08:50 -0800 Subject: [PATCH] [d16-9] [CI][VSTS] Do not step over success statuses in rebuilds. (#10566) * [CI][VSTS] Do not setp over success statuses in rebuilds. * Address reviews. Co-authored-by: Manuel de la Pena --- tools/devops/automation/scripts/GitHub.psm1 | 31 +++++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/tools/devops/automation/scripts/GitHub.psm1 b/tools/devops/automation/scripts/GitHub.psm1 index a73443d970ef..4c9d790fb099 100644 --- a/tools/devops/automation/scripts/GitHub.psm1 +++ b/tools/devops/automation/scripts/GitHub.psm1 @@ -124,6 +124,7 @@ function Set-GitHubStatus { "SYSTEM_TEAMPROJECT" = $Env:SYSTEM_TEAMPROJECT; "BUILD_BUILDID" = $Env:BUILD_BUILDID; "BUILD_REVISION" = $Env:BUILD_REVISION; + "BUILD_SOURCEBRANCHNAME" = $Env:BUILD_SOURCEBRANCHNAME; "GITHUB_TOKEN" = $Env:GITHUB_TOKEN; } @@ -134,6 +135,29 @@ function Set-GitHubStatus { } } + $url = "https://api.github.com/repos/xamarin/xamarin-macios/statuses/$Env:BUILD_REVISION" + + $headers = @{ + Authorization = ("token {0}" -f $Env:GITHUB_TOKEN) + } + + $requestContext = $Context + # before we set a status, we are going to check if it is present and a success, if it is a success, do not + # re-set it. The reason for this is that statuses are linked to the hash of a commit. A commit hash can be in two + # different branches. If a hash had a success we will not set the status and we will create a warning and a new context + # Why only on success, well we do want to support rebuilds, in any non-success case we want to step over + $presentStatuses = Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "GET" -ContentType 'application/json' } + + # try to find the status with the same context and make a decision, this is not a dict but an array :/ + foreach ($s in $presentStatuses) { + # we found a status from a previous build that was a success, we do not want to step on it + if (($s.context -eq $Context) -and ($s.state -eq "success")) { + Write-Host "WARNING: Not setting status for $Context because it was already set as a success, using '$Context $Env:BUILD_SOURCEBRANCHNAME' instead." + # modify the context to include the branch name in the status + $requestContext = "$Context $Env:BUILD_SOURCEBRANCHNAME" + } + } + # use the GitHub API to set the status for the given commit $detailsUrl = "" if ($TargetUrl) { @@ -145,12 +169,7 @@ function Set-GitHubStatus { state = $Status target_url = $detailsUrl description = $Description - context = $Context - } - $url = "https://api.github.com/repos/xamarin/xamarin-macios/statuses/$Env:BUILD_REVISION" - - $headers = @{ - Authorization = ("token {0}" -f $Env:GITHUB_TOKEN) + context = $requestContext } return Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body ($payload | ConvertTo-json) -ContentType 'application/json' }