Skip to content

Instantly share code, notes, and snippets.

@xt0rted
Last active July 9, 2018 05:14
Show Gist options
  • Save xt0rted/aa262ace7b7dedbc2fc2afea89f23c09 to your computer and use it in GitHub Desktop.
Save xt0rted/aa262ace7b7dedbc2fc2afea89f23c09 to your computer and use it in GitHub Desktop.
Create sentry releases

How to use

To use this when deploying from AppVeyor you'll need to make the following changes to your appveyor.yml.

environment:
  SENTRY_AUTHTOKEN:
    secure: ...
  SENTRY_ORGANIZATION: org-name
  SENTRY_PROJECT: project-name

after_deploy:
  - ps: $buildDeployedAt = (get-date).ToUniversalTime().ToString("s")
  - ps: .\build\SentryDeployment.ps1 -authToken $env:SENTRY_AUTHTOKEN -organization $env:SENTRY_ORGANIZATION -project $env:SENTRY_PROJECT -version $env:APPVEYOR_BUILD_VERSION -repository $env:APPVEYOR_REPO_NAME -commit $env:APPVEYOR_REPO_COMMIT -dateReleased $buildDeployedAt

Note: There's currently a bug in Sentry's releases api so if the case of the APPVEYOR_REPO_NAME value doesn't match what you've added in Sentry the request will fail. Make sure these match!

[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)][string]$authToken,
[Parameter(Mandatory = $true)][string]$organization,
[Parameter(Mandatory = $true)][string]$project,
[Parameter(Mandatory = $true)][string]$version,
[Parameter(Mandatory = $true)][string]$repository,
[Parameter(Mandatory = $true)][string]$commit,
[parameter(Mandatory = $true)][DateTime]$dateReleased
)
function CreateRelease()
{
$retries = 1
$success = $false
while (!$success -and $retries -lt 6) {
$location = "https://sentry.io/api/0/organizations/$organization/releases/"
Write-Host "Invoke a web request for $location to create a new release. Attempting $retries"
Set-Variable -Name createResultStatus -Force -Scope Local -Value $null
Set-Variable -Name createResultStatusDescription -Force -Scope Local -Value $null
Set-Variable -Name result -Force -Scope Local
try {
$result = Invoke-WebRequest -Uri $location -Method Post -Body $bodyBytes -Headers $headers -ContentType "application/json; charset=utf-8"
} catch {
Write-Warning "Response body: ${Error.ErrorDetails}"
if($_.Exception.Response) {
$createResultStatus = $_.Exception.Response.StatusCode.value__
$createResultStatusDescription = $_.Exception.Response.StatusDescription
}
else {
$createResultStatus = "Exception"
$createResultStatusDescription = $_.Exception.Message
}
}
if ($result -eq $null) {
if ($createResultStatus -eq $null) {
$createResultStatus = "Unknown"
}
if ($createResultStatusDescription -eq $null) {
$createResultStatusDescription = "Unknown"
}
}
else {
$success = $true
}
if ($createResultStatus -eq 409 -or $createResultStatus -eq 404 -or $createResultStatus -eq 401) # no retry when conflict or unauthorized or not found
{
break
}
$retries = $retries + 1
sleep 1
}
$createResultStatus
$createResultStatusDescription
return
}
Write-Host "Creating release in Sentry"
Set-Variable -Name requestBody -Force -Scope Script
$requestBody = @{}
$requestBody.version = $version
$requestBody.ref = $commit
$requestBody.projects = @($project)
$requestBody.dateReleased = $dateReleased.GetDateTimeFormats("s")[0]
$requestBody.refs = @(@{
repository = $repository
commit = $commit
})
$bodyJson = $requestBody | ConvertTo-Json
Write-Host $bodyJson
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($bodyJson)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authToken")
Set-Variable -Name createReleaseResult1 -Force -Scope Local -Value $null
Set-Variable -Name createReleaseResultDescription -Force -Scope Local -Value ""
$createReleaseResult1, $createReleaseResultDescription = CreateRelease
if ($createReleaseResult1)
{
$output = "Failed to create a release for version: {0}. Error {1}, Description: {2}." -f $requestBody.version, $createReleaseResult1, $createReleaseResultDescription
throw $output
}
$str = "Release created. Version: {0}." -f $requestBody.version
Write-Host $str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment