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

[d16-9] [CI][VSTS] Do not step over success statuses in rebuilds. #10566

Merged
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
31 changes: 25 additions & 6 deletions tools/devops/automation/scripts/GitHub.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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' }
Expand Down