-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTFS_CreateBranchPolicies.ps1
More file actions
181 lines (161 loc) · 4.5 KB
/
TFS_CreateBranchPolicies.ps1
File metadata and controls
181 lines (161 loc) · 4.5 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<#
.SYNOPSIS
Uses the TFS REST API to apply branch policies to specified branch
.DESCRIPTION
This script uses the TFS REST API for configurations to set branch policies for:
-approver group
-Minimum approvers (2)
-Required build
-Work Item required
.NOTES
-Branch is set via build definition variable at queue time
#>
param(
[string]$global:branch, # Branch to set policies on passed via build definition when queueing build
[string]$global:user, # Service account user name passed via build definition
[string]$global:passwd # Encrypted password passed via build definition
)
# Use TFS REST API for configurations: https://www.visualstudio.com/en-us/docs/integrate/api/policy/configurations#create-a-policy-configuration
[uri] $global:PolicyUri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + $env:SYSTEM_TEAMPROJECT + "/_apis/policy/configurations?api-version=2.0-preview"
write-host $PolicyUri
# Create secure credential
$global:secpasswd = ConvertTo-SecureString $passwd -AsPlainText -Force
$global:credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
Function ApplyApproverPolicy
{
# JSON for setting appovers
$JSONBody= @"
{
"isEnabled": true,
"isBlocking": true,
"type": {
"id": ""
},
"settings": {
"requiredReviewerIds": [
""
],
"filenamePatterns": [
"/ExamplePath"
],
"addedFilesOnly": false,
"scope": [
{
"repositoryId": null,
"refName": "refs/heads/$Branch",
"matchKind": "exact"
},
{
"repositoryId": null,
"refName": "refs/heads/$Branch/",
"matchKind": "prefix"
}
]
}
}
"@
# Use URI and JSON above to apply approver policy to specified branch
Invoke-RestMethod -Uri $PolicyUri -Method Post -ContentType application/json -Body $JSONBody -Credential $credential
}
Function ApplyMinimumApproverPolicy
{
# JSON for setting minimum approval count policy
$JSONBody= @"
{
"isEnabled": true,
"isBlocking": true,
"type": {
"id": ""
},
"settings": {
"minimumApproverCount": 2,
"scope": [
{
"repositoryId": null,
"refName": "refs/heads/$branch",
"matchKind": "exact"
}
]
}
}
"@
# Use URI and JSON above to apply minimum approver policy to specified branch
Invoke-RestMethod -Uri $PolicyUri -Method Post -ContentType application/json -Body $JSONBody -Credential $credential
}
Function ApplyBuildPolicy
{
# JSON for setting required build policy
$JSONBody= @"
{
"isEnabled": true,
"isBlocking": true,
"type": {
"id": ""
},
"settings": {
"buildDefinitionId": buildID,
"scope": [
{
"repositoryId": null,
"refName": "refs/heads/$branch",
"matchKind": "exact"
}
]
}
}
"@
# Use URI and JSON above to apply build policy to specified branch
Invoke-RestMethod -Uri $PolicyUri -Method Post -ContentType application/json -Body $JSONBody -Credential $credential
}
Function ApplyWorkItemPolicy
{
# JSON for setting work item required policy
$JSONBody= @"
{
"isEnabled": true,
"isBlocking": true,
"type": {
"id": ""
},
"settings": {
"scope": [
{
"repositoryId": null,
"refName": "refs/heads/$branch",
"matchKind": "exact"
}
]
}
}
"@
# Use URI and JSON above to apply work item required to specified branch
Invoke-RestMethod -Uri $PolicyUri -Method Post -ContentType application/json -Body $JSONBody -Credential $credential
}
Try
{
ApplyApproverPolicy
write-host "Approver Policy set on branch: $branch"
ApplyMinimumApproverPolicy
write-host "Minimum Approver Policy set on branch: $branch"
ApplyBuildPolicy
write-host "Build Policy set on branch: $branch"
ApplyWorkItemPolicy
write-host "Work Item required Policy set on branch: $branch"
}
Catch
{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
write-host $responseBody
}
Finally
{
# Clear global variable to cleanup
Clear-Variable user -Scope Global
Clear-Variable pwd -Scope Global
Clear-Variable branch -Scope Global
Clear-Variable PolicyUri -Scope Global
}