Skip to content

Instantly share code, notes, and snippets.

@sergey-miryanov
Created November 13, 2015 10:27
Show Gist options
  • Save sergey-miryanov/65832e1e5a9e37f88144 to your computer and use it in GitHub Desktop.
Save sergey-miryanov/65832e1e5a9e37f88144 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Generates a project change log file.
.LINK
Script posted over:
http://open.bekk.no/generating-a-project-change-log-with-teamcity-and-powershell
#>
# Where the changelog file will be created
#$changelogFile = "%system.teamcity.build.tempDir%\changes_%teamcity.build.id%.txt"
$changelogFile = "changelog.txt"
# the url of teamcity server
$teamcityUrl = "%teamcity.serverUrl%"
# username/password to access Teamcity REST API
$authToken=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("%system.teamcity.auth.userId%:%system.teamcity.auth.password%"))
# Build id for the release notes
$buildId = %teamcity.build.id%
# Get the commit messages for the specified change id
# Ignore messages containing #ignore
# Ignore empty lines
Function GetCommitMessages($changeid)
{
$request = [System.Net.WebRequest]::Create("$teamcityUrl/httpAuth/app/rest/changes/id:$changeid")
$request.Headers.Add("AUTHORIZATION", "$authToken");
$xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd()
Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/change" |
where { ($_.Node["comment"].InnerText.Length -ne 0) -and (-Not $_.Node["comment"].InnerText.Contains('#ignore'))} |
foreach {"+ $($_.Node["comment"].InnerText.Trim().Replace("`n"," "))`n"}
}
# Grab all the changes
$request = [System.Net.WebRequest]::Create("$teamcityUrl/httpAuth/app/rest/changes?build=id:$($buildId)")
$request.Headers.Add("AUTHORIZATION", "$authToken");
$xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd()
# Then get all commit messages for each of them
$changelog = Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/changes/change" | Foreach {GetCommitMessages($_.Node.id)}
$oldchanges = Get-Content $changelogFile | Out-String
If($changelog.length -gt 0)
{
$date = Get-Date
$day = $date.Day
$month = $date.Month
$year = $date.Year
$newchanges = $changelog
$version = "%build.number% (${day}.${month}.${year})"
$delimeter = "------------------------------------------------------------"
$nl = [Environment]::NewLine
If($oldchanges.length -gt 0)
{
$strs = @($version,$delimeter,$newchanges,$nl,$oldchanges)
$newchanges = [string]::Join($nl,$strs)
}
Else
{
$strs = @($version,$delimeter,$newchanges,$nl)
$newchanges = [string]::Join($nl,$strs)
}
Write-Output $newchanges | Out-File $changelogFile -encoding utf8
Write-Host "Changelog saved to ${changelogFile}:"
$changelog
}
Else
{
Write-Host "No changes"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment