Skip to content

Instantly share code, notes, and snippets.

@kzu
Last active September 24, 2018 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kzu/f490ed15efe0e114927e09aef6ff727c to your computer and use it in GitHub Desktop.
Save kzu/f490ed15efe0e114927e09aef6ff727c to your computer and use it in GitHub Desktop.
Cross-platform GitHub status reporting powershell script for VSTS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$statusUrl = '$(GitHub.StatusUrl)'
$token = '$(GitHub.Token)'
if ($env:FIXEDSOURCEVERSION -eq $true) { return; }
# When the commit that triggered the build/release is a PR merge commit,
# this task will adjust the Build.SourceVersion to the original commit, not the
# merge commit
$message = "$env:BUILD_SOURCEVERSIONMESSAGE"
$commit = "$env:BUILD_SOURCEVERSION"
$sourceBranch = "$env:BUILD_SOURCEBRANCH"
$isPR = $sourceBranch.StartsWith('refs/pull/')
if ([string]::IsNullOrWhitespace($message)) {
Write-Host "No commit message was found. Determining current commit message for commit $commit..."
if ([string]::IsNullOrWhitespace($env:RELEASE_RELEASEID)) {
$message = (git log -1 --pretty=%s).trim()
Write-Host "Got commit message for build definition from git: $message"
} else {
# We can't invoke git from a RD since there isn't a repo clone in place
$uri = "https://api.github.com/repos/$(GitHub.Repository)/commits/$commit" + '?access_token=' + '$(GitHub.Token)'
Write-Host "Retrieving commit info from $uri"
# Get the commit message from GitHub via API
$message = Invoke-WebRequest -UseBasicParsing -Uri $uri | %{ (ConvertFrom-Json $_.Content).commit.message }
$message = ($message -split '\r?\n')[0]
Write-Host "Got commit message for release definition from GitHub: $message"
}
}
if (($message -match '^Merge ([^\s]+) into [^\s]+$') -and $isPR -eq $true) {
$commit = $matches[1]
if ($env:BUILD_SOURCEVERSION -match $commit) {
Write-Host "Current SourceVersion already matches $commit, no need to update it"
} else {
Write-Host "Adjusting PR commit from $env:BUILD_SOURCEVERSION to $commit"
Write-Host "##vso[task.setvariable variable=Build.SourceVersion;]$commit"
Write-Host "##vso[task.setvariable variable=FixedSourceVersion;]true"
}
} else {
Write-Host "Current commit message '$message' did not match the pattern for a merge commit. Leaving source version as $commit"
}
if (!$env:BUILD_SOURCEVERSION) {
# when manually triggering a build from a PR merge branch, the
# source version isn't set at all
# we need to report the status on the actual commit just before it
# Note this only applies to build definitions, since releases always
# have a source version they are being created for.
$merge = (git show | select-string Merge)[-1].ToString().Trim()
Write-Host ("merge {0}" -f $merge)
$commit = git rev-parse $merge.Split(' ')[1]
Write-Host ("##{0}[task.setvariable variable=Build.SourceVersion;]{1}" -f "vso", $commit)
} else {
$commit = $env:BUILD_SOURCEVERSION
}
$repo = '$(GitHub.Repository)'
$api = 'https://api.github.com'
$uri = "$api/repos/$repo/statuses/$commit"
$state = '$(GitHub.State)'
switch ($state)
{
'SucceededWithIssues' { $state = 'error' }
'Canceled' { $state = 'pending' }
'Failed' { $state = 'failure' }
'pending' { $state = 'pending' }
default { $state = 'success' }
}
$body = ConvertTo-Json @{ state = $state; context = '$(GitHub.Context)'; description = '$(GitHub.Description)'; target_url = $statusUrl }
Write-Host "Reporting status update to GitHub at $uri with state $state"
Write-Host $body
$uri = $uri + '?access_token=' + $token
Invoke-WebRequest -Method Post -UseBasicParsing -Uri $uri -Body $body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment