Last active
March 31, 2016 06:01
-
-
Save jhochwald/4ee756613d8782d7649e to your computer and use it in GitHub Desktop.
Creates an BitBucket Issue if the Build has been failed, do nothing if the Build is OK. I like to document every failed Build, so I find that useful!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region License | |
<# | |
{ | |
"info": { | |
"Statement": "Code is poetry", | |
"Author": "Joerg Hochwald", | |
"Contact": "joerg.hochwald@outlook.com", | |
"Link": "http://hochwald.net", | |
"Support": "https://github.com/jhochwald/MyPowerShellStuff/issues" | |
}, | |
"Copyright": "(c) 2012-2016 by Joerg Hochwald & Associates. All rights reserved." | |
} | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this list of | |
conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation and/or | |
other materials provided with the distribution. | |
3. Neither the name of the copyright holder nor the names of its contributors may | |
be used to endorse or promote products derived from this software without | |
specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
By using the Software, you agree to the License, Terms and Conditions above! | |
#> | |
#endregion License | |
<# | |
.SYNOPSIS | |
Create an BitBucket Issue if the Build is failed | |
.DESCRIPTION | |
Creates an BitBucket Issue if the Build has been failed, do nothing if the Build is OK | |
I like to document every failed Build, so I find that useful! | |
The new version of this script will comment and then close the issue is the build is a success | |
.PARAMETER TcAgentName | |
TeamCity Agent Name (Filled by Variable %teamcity.agent.name%) | |
.PARAMETER TcVersion | |
TeamCity Version String (Filled by Variable %system.teamcity.version%) | |
.PARAMETER TcBuild | |
TeamCity Build Number (Filled by Variable %build.number%) | |
.PARAMETER TcServerAddress | |
TeamCity Server Address (Filled by Variable) | |
.PARAMETER BbUsername | |
BitBucket User Name | |
.PARAMETER BbPassword | |
BitBucket password (Clear text) | |
.PARAMETER BbAccount | |
BitBucket Account (Might match TC Username) | |
.PARAMETER BbRepo | |
BitBucket repository (If names on TeamCity and BitBucket matches, you might use %env.TEAMCITY_PROJECT_NAME%) | |
.LINK | |
API https://confluence.atlassian.com/bitbucket/issues-resource-296095191.html | |
.NOTES | |
For internal use only! | |
I use this in a Build-Step that only runs if all others are a success: | |
# Create a new Temp File | |
Set-Content -Path "C:\dev\tmp\build.txt" -Value "OK" -Encoding UTF8 -Force -Confirm:$false -ErrorAction Continue -WarningAction SilentlyContinue | |
Remember: The Step that creates the Temp File should have "Only if build status is successful" as execution | |
Next you should create a new Build Step at the last position, with the following condition "Always, even if build stop command was issued". | |
This step Should be "PowerShell" as "Source Code" an it should contain something like this: | |
& C:\PATH_ON_AGENT\BuildReporter.ps1 -TcAgentName "%teamcity.agent.name%" -TcVersion "%system.teamcity.version%" -TcBuild "%build.number%" -TcServerAddress "%teamcity.serverUrl%" -BbUsername "USERNAME" -BbPassword "PASSWORD" -BbAccount "ACCOUNT" -BbRepo "REPOSITORYNAME" | |
#> | |
[CmdletBinding(ConfirmImpact = 'None', | |
SupportsShouldProcess = $true)] | |
param | |
( | |
[Parameter(Mandatory = $true)] | |
[System.String]$TcAgentName, | |
[Parameter(Mandatory = $true)] | |
[System.String]$TcVersion, | |
[Parameter(Mandatory = $true)] | |
[System.String]$TcBuild, | |
[Parameter(Mandatory = $true)] | |
[System.String]$TcServerAddress, | |
[Parameter(Mandatory = $true)] | |
[System.String]$BbUsername, | |
[Parameter(Mandatory = $true)] | |
[System.String]$BbPassword, | |
[Parameter(Mandatory = $true)] | |
[System.String]$BbAccount, | |
[Parameter(Mandatory = $true)] | |
[System.String]$BbRepo | |
) | |
BEGIN { | |
#region BitBucketVars | |
[version]$BbVersionIn = ($TcBuild) | |
[String]$BbVersion = "$(($BbVersionIn).Major).$(($BbVersionIn).Minor).$(($BbVersionIn).Build)" | |
$BbServerAddress = "https://api.bitbucket.org" | |
$BbCommand = $($BbServerAddress + "/1.0/repositories/$BbAccount/$BbRepo/issues") | |
$BbAuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $BbUsername, $BbPassword))) | |
#endregion BitBucketVars | |
#region General | |
# File created by a former step | |
$BuildSuccessFile = 'C:\dev\tmp\build.txt' | |
# File created by us if a build failed in a previous run | |
$OpenTicketFile = 'C:\dev\tmp\OpenTicket.txt' | |
#endregion General | |
} | |
PROCESS { | |
# Check if the BuildSuccessFile does not exists and that we have no $OpenTicketFile! | |
if ((-not (Test-Path -Path $BuildSuccessFile)) -and (-not (Test-Path -Path $OpenTicketFile))) { | |
# Open a Issue to track | |
try { | |
# Body of the Request is also the Issue Body | |
$BbCommandBody = "Build $TcBuild failed on $TcAgentName`r`n`r`nPlease login to $TcServerAddress to check the details!`r`n`r`nNET-Experts WindowsPowerShell Build Service powered by TeamCity $TcVersion" | |
# Check that these settings fit! | |
# Check the Docs in the Link above... | |
$BbCommandData = @{ | |
status = "new" | |
priority = "major" | |
title = "Build $TcBuild failed!" | |
responsible = "jhochwald" | |
content = "$BbCommandBody" | |
kind = "task" | |
component = "Build" | |
version = "$BbVersion" | |
} | |
# Fire up! | |
$BbRequest = (Invoke-RestMethod -Headers @{ Authorization = ("Basic {0}" -f $BbAuthInfo) } -Uri $BbCommand -Method "Post" -Body $BbCommandData -UserAgent "Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) NET-Experts PowerShell Build Service powered by TeamCity $TcVersion" -ErrorAction Stop -WarningAction SilentlyContinue) | |
# Dump it to the Build Log | |
Write-Output -InputObject $BbRequest | |
Set-Content -Path $OpenTicketFile -Value (($BbRequest).resource_uri) -Encoding UTF8 -Force -Confirm:$false -ErrorAction Stop -WarningAction SilentlyContinue | |
} catch { | |
# Houston, we have a problem! | |
Write-Warning -Message 'Could not open an issue on BitBucket!' | |
# Dump it to the Build Log | |
Write-Output -InputObject $BbRequest | |
} | |
} elseif ((Test-Path -Path $OpenTicketFile) -and (Test-Path -Path $BuildSuccessFile)) { | |
# OK, we have an open Ticket from a former run and this build is a success! | |
try { | |
# Get the Issue Info from our File | |
$BbTicketUrl = (Get-Content -Path $OpenTicketFile) | |
# Set the BitBucket stuff | |
$BbCommand = $($BbServerAddress + $BbTicketUrl + "/comments") | |
# Check that these settings fit! | |
# Check the Docs in the Link above... | |
$BbCommandData = @{ | |
content = "Fixed with Build $TcBuild" | |
} | |
# Fire up! | |
$BbRequest = (Invoke-RestMethod -Headers @{ Authorization = ("Basic {0}" -f $BbAuthInfo) } -Uri $BbCommand -Method "Post" -Body $BbCommandData -UserAgent "Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) NET-Experts PowerShell Build Service powered by TeamCity $TcVersion" -ErrorAction Stop -WarningAction SilentlyContinue) | |
# Dump it to the Build Log | |
Write-Output -InputObject $BbRequest | |
# Wait 3 Seconds | |
Write-Output "Wait..." | |
Start-Sleep -Seconds 3 | |
# Set the BitBucket stuff | |
$BbCommand = $($BbServerAddress + $BbTicketUrl) | |
# Check that these settings fit! | |
# Check the Docs in the Link above... | |
$BbCommandData = @{ | |
status = "resolved" | |
} | |
# Convert to Json | |
$BbCommandData = (ConvertTo-Json $BbCommandData) | |
# Fire up! | |
$BbRequest = (Invoke-RestMethod -Headers @{ Authorization = ("Basic {0}" -f $BbAuthInfo) } -Uri $BbCommand -Method "Put" -ContentType "application/json" -Body $BbCommandData -UserAgent "Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) NET-Experts PowerShell Build Service powered by TeamCity $TcVersion" -ErrorAction Stop -WarningAction SilentlyContinue) | |
# Dump it to the Build Log | |
Write-Output -InputObject $BbRequest | |
# Cleanup | |
$null = (Remove-Item -Path $OpenTicketFile -Force -Recurse -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue) > $null 2>&1 3>&1 | |
Start-Sleep -Seconds 1 | |
$null = (Remove-Item -Path $OpenTicketFile -Force -Recurse -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue) > $null 2>&1 3>&1 | |
} catch { | |
# Aw snap! But we do nothing... | |
Write-Warning -Message 'Could not update/close the issue on BitBucket!' | |
# Dump it to the Build Log | |
Write-Output -InputObject $BbRequest | |
} | |
} | |
} | |
END { | |
# Remove the Temp File... | |
if (Test-Path -Path $BuildSuccessFile) { | |
try { | |
$null = (Remove-Item -Path $BuildSuccessFile -Force -Recurse -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue) > $null 2>&1 3>&1 | |
Start-Sleep -Seconds 1 | |
$null = (Remove-Item -Path $BuildSuccessFile -Force -Recurse -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue) > $null 2>&1 3>&1 | |
} catch { | |
# Do nothing | |
Write-Debug "Unable to remove the old Info File!!!" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment