forked from OctopusDeploy/OctoTFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctopus-CreateRelease.ps1
164 lines (139 loc) · 7.16 KB
/
Octopus-CreateRelease.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
param(
[string] [Parameter(Mandatory = $true)]
$ConnectedServiceName,
[string] [Parameter(Mandatory = $true)]
$ProjectName,
[string] [Parameter(Mandatory = $false)]
$ChangesetCommentReleaseNotes,
[string] [Parameter(Mandatory = $false)]
$WorkItemReleaseNotes,
[string] [Parameter(Mandatory = $false)]
$DeployTo,
[string] [Parameter(Mandatory = $false)]
$AdditionalArguments
)
#todo:
# - handle non-TFVC repositories
Write-Verbose "Entering script Octopus-CreateRelease.ps1"
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Common"
# Get release notes from linked changesets and work items
function Get-LinkedReleaseNotes($vssEndpoint, $comments, $workItems) {
$personalAccessToken = $vssEndpoint.Authorization.Parameters.AccessToken
$changesUri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/builds/$($env:BUILD_BUILDID)/changes"
$headers = @{Authorization = "Bearer $personalAccessToken"}
$changesResponse = Invoke-WebRequest -Uri $changesUri -Headers $headers -UseBasicParsing
$relatedChanges = $changesResponse.Content | ConvertFrom-Json
Write-Host "Related Changes = $($relatedChanges.value)"
$releaseNotes = ""
$nl = "`r`n`r`n"
if ($comments -eq $true) {
Write-Host "Build Provider: $($env:BUILD_REPOSITORY_PROVIDER)"
if ($env:BUILD_REPOSITORY_PROVIDER -eq "TfsVersionControl") {
Write-Host "Adding changeset comments to release notes"
$releaseNotes += "**Changeset Comments:**$nl"
$relatedChanges.value | ForEach-Object {$releaseNotes += "* [$($_.id) - $($_.author.displayName)]($(ChangesetUrl $_.location)): $($_.message)$nl"}
} else {
Write-Host "Adding commit messages to release notes"
$releaseNotes += "**Commit Messages:**$nl"
$relatedChanges.value | ForEach-Object {$releaseNotes += "* [$($_.id) - $($_.author.displayName)]($(CommitUrl $_)): $($_.message)$nl"}
}
}
if ($workItems -eq $true) {
Write-Host "Adding work items to release notes"
$releaseNotes += "**Work Items:**$nl"
if ($env:BUILD_REPOSITORY_PROVIDER -eq "TfsVersionControl") {
foreach ($c in $relatedChanges.value) {
# work item id is a hack because id is prefixed with "C", and I'm not 100% sure it's consistent.
$wiId = $c.location.Substring($c.location.LastIndexOf("/")+1)
$wiUri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)/_apis/tfvc/changesets/$wiId/workItems"
$wiResponse = Invoke-WebRequest -Uri $wiUri -Headers $headers -UseBasicParsing
$wi = $wiResponse.Content | ConvertFrom-Json
$wi.value | ForEach-Object {$releaseNotes += "* [$($_.id)]($($_.webUrl)): $($_.title) [$($_.state)]$nl"}
}
} else {
$relatedWorkItemsUri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/builds/$($env:BUILD_BUILDID)/workitems?api-version=2.0"
Write-Host "Performing POST request to $relatedWorkItemsUri"
$relatedWiResponse = Invoke-WebRequest -Uri $relatedWorkItemsUri -Method POST -Headers $headers -UseBasicParsing -ContentType "application/json"
$relatedWorkItems = $relatedWiResponse.Content | ConvertFrom-Json
Write-Host "Retrieved $($relatedWorkItems.count) work items"
if ($relatedWorkItems.count -gt 0) {
$workItemsUri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)/_apis/wit/workItems?ids=$(($relatedWorkItems.value.id) -join '%2C')"
Write-Host "Performing GET request to $workItemsUri"
$relatedWiDetailsResponse = Invoke-WebRequest -Uri $workItemsUri -Headers $headers -UseBasicParsing
$workItemsDetails = $relatedWiDetailsResponse.Content | ConvertFrom-Json
$workItemEditBaseUri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_workitems/edit"
$workItemsDetails.value | ForEach-Object {$releaseNotes += "* [$($_.id)]($workItemEditBaseUri/$($_.id)): $($_.fields.'System.Title') [$($_.fields.'System.State')]$nl"}
}
}
}
Write-Host "Release Notes:`r`n$releaseNotes"
return $releaseNotes
}
function ChangesetUrl($apiUrl) {
$wiId = $apiUrl.Substring($apiUrl.LastIndexOf("/")+1)
return "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_versionControl/changeset/$wiId"
}
function CommitUrl($change) {
$commitId = $change.id
$repositoryId = Split-Path (Split-Path (Split-Path $change.location -Parent) -Parent) -Leaf
return "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_git/$repositoryId/commit/$commitId"
}
# Returns the Octo.exe parameters for credentials
function Get-OctoCredentialParameters($serviceDetails) {
$pwd = $serviceDetails.Authorization.Parameters.Password
if ($pwd.StartsWith("API-")) {
return "--apiKey=$pwd"
} else {
$un = $serviceDetails.Authorization.Parameters.Username
return "--user=$un --pass=$pwd"
}
}
# Returns a path to the Octo.exe file
function Get-PathToOctoExe() {
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.ScriptBlock.File
$targetPath = Join-Path -Path $PSScriptRoot -ChildPath "Octo.exe"
return $targetPath
}
# Create a Release Notes file for Octopus
function Create-ReleaseNotes($linkedItemReleaseNotes) {
$buildNumber = $env:BUILD_BUILDNUMBER #works
$buildId = $env:BUILD_BUILDID #works
$projectName = $env:SYSTEM_TEAMPROJECT #works
#$buildUri = $env:BUILD_BUILDURI #works but is a vstfs:/// link
#Note: This URL will undoubtedly change in the future
$buildUri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$projectName/_BuildvNext#_a=summary&buildId=$buildId"
$buildName = $env:BUILD_DEFINITIONNAME #works
$repoName = $env:BUILD_REPOSITORY_NAME #works
#$repoUri = $env:BUILD_REPOSITORY_URI #nope :(
$notes = "Release created by Build [${buildName} #${buildNumber}](${buildUri}) in Project ${projectName} from the ${repoName} repository."
if (-not [System.String]::IsNullOrWhiteSpace($linkedItemReleaseNotes)) {
$notes += "`r`n`r`n$linkedItemReleaseNotes"
}
$fileguid = [guid]::NewGuid()
$fileLocation = Join-Path -Path $env:BUILD_STAGINGDIRECTORY -ChildPath "release-notes-$fileguid.md"
$notes | Out-File $fileLocation -Encoding utf8
return "--releaseNotesFile=`"$fileLocation`""
}
### Execution starts here ###
# Get required parameters
$connectedServiceDetails = Get-ServiceEndpoint -Name "$ConnectedServiceName" -Context $distributedTaskContext
$credentialParams = Get-OctoCredentialParameters($connectedServiceDetails)
$octopusUrl = $connectedServiceDetails.Url
# Get release notes
$linkedReleaseNotes = ""
$wiReleaseNotes = Convert-String $WorkItemReleaseNotes Boolean
$commentReleaseNotes = Convert-String $ChangesetCommentReleaseNotes Boolean
if ($wiReleaseNotes -or $commentReleaseNotes) {
$vssEndPoint = Get-ServiceEndPoint -Name "SystemVssConnection" -Context $distributedTaskContext
$linkedReleaseNotes = Get-LinkedReleaseNotes $vssEndPoint $commentReleaseNotes $wiReleaseNotes
}
$releaseNotesParam = Create-ReleaseNotes $linkedReleaseNotes
#deployment arguments
if (-not [System.String]::IsNullOrWhiteSpace($DeployTo)) {
$deployToParams = "--deployTo=`"$DeployTo`""
}
# Call Octo.exe
$octoPath = Get-PathToOctoExe
Write-Output "Path to Octo.exe = $octoPath"
Invoke-Tool -Path $octoPath -Arguments "create-release --project=`"$ProjectName`" --server=$octopusUrl $credentialParams $deployToParams $releaseNotesParam $AdditionalArguments"
Write-Verbose "Finishing Octopus-CreateRelease.ps1"