-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTFS_UpdateWorkItemsWithBuildLink.ps1
More file actions
66 lines (57 loc) · 2.14 KB
/
TFS_UpdateWorkItemsWithBuildLink.ps1
File metadata and controls
66 lines (57 loc) · 2.14 KB
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
# Adds build link to associate work items
# Runs as the last step in build definitions
# Encrypted password passed via build definition
param(
[string]$passwd
)
Function UpdateWorkItemsWithBuildLink
{
$user = ""
$secpasswd = ConvertTo-SecureString $passwd -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
# Uri to get associated work items: https://www.visualstudio.com/en-us/integrate/api/build/builds#GetbuilddetailsWorkitems
[uri] $BuildWorkItemsUri = $tfsUrl + $env:SYSTEM_TEAMPROJECT + "/_apis/build/builds/" + $env:BUILD_BUILDID +"/workitems?api-version=2.0"
write-host $BuildWorkItemsUri
try
{
# Get build details: Workitems
$results = Invoke-RestMethod -Uri $BuildWorkItemsUri -Method Get -Credential $credential
write-host $results
}
catch
{
# Catch 404 and move on
$_.Exception.Response.StatusCode.Value__
}
$WorkitemIDs = $results.value.id
# List array of associated work items
write-host "Associated Work Item IDs:"$WorkitemIDs
# Loop through each work item and update with link to build
ForEach($WorkItemID in $WorkitemIDs)
{
# Uri to add hyperlink to work items: https://www.visualstudio.com/integrate/api/wit/work-items#UpdateworkitemsAddalink
[uri] $WorkItemLinkUri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + "_apis/wit/workitems/" + $WorkItemID +"?api-version=1.0"
write-host $WorkItemLinkUri
$JSONBody= @"
[{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "Hyperlink",
"url": " $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI$env:SYSTEM_TEAMPROJECT/_build#buildId=$env:BUILD_BUILDID&_a=summary"
}
}]
"@
# Add a link to each work item with build link
Invoke-RestMethod -Uri $WorkItemLinkUri -Method Patch -ContentType application/json-patch+json -Body $JSONBody -Credential $credential
}
}
Try
{
UpdateWorkItemsWithBuildLink
}
Catch
{
Write-Host "Exception Type: $($_.Exception.GetType().FullName)"
Write-Host "Exception Message: $($_.Exception.Message)"
}