-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSet-AzDoBranchPolicyMinimalApproval.ps1
More file actions
162 lines (141 loc) · 5.12 KB
/
Set-AzDoBranchPolicyMinimalApproval.ps1
File metadata and controls
162 lines (141 loc) · 5.12 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
function Set-AzDoBranchPolicyMinimalApproval {
<#
.SYNOPSIS
Creates a Minimal approval policy on a branch
.DESCRIPTION
Creates a Minimal approval policy on a branch
.EXAMPLE
$params = @{
CollectionUri = "https://dev.azure.com/contoso"
RepoName = "Repo 1"
ProjectName = "Project 1"
}
Set-AzDoBranchPolicyMinimalApproval @params
This example creates a new Azure Pipeline using the PowerShell pipeline
.EXAMPLE
'repo1', 'repo2' |
Set-AzDoBranchPolicyMinimalApproval -CollectionUri $CollectionUri -ProjectName $ProjectName -PAT "***"
This example creates a 'Minimum number of reviewers' policy on the main branch of repo1 and repo2
.OUTPUTS
[PSCustomObject]@{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
RepoName = $RepoName
id = $res.id
minimumApproverCount = $res.settings.minimumApproverCount
creatorVoteCounts = $res.settings.creatorVoteCounts
allowDownvotes = $res.settings.allowDownvotes
resetOnSourcePush = $res.settings.resetOnSourcePush
requireVoteOnLastIteration = $res.settings.requireVoteOnLastIteration
resetRejectionsOnSourcePush = $res.settings.resetRejectionsOnSourcePush
blockLastPusherVote = $res.settings.blockLastPusherVote
requireVoteOnEachIteration = $res.settings.requireVoteOnEachIteration
}
.NOTES
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param (
# Collection Uri of the organization
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$CollectionUri,
# Project where the pipeline will be created.
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$ProjectName,
# Name of the Repository containing the YAML-sourcecode
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string[]]
$RepoName,
# Branch to create the policy on
[Parameter()]
[string]
$branch = "main",
# Block pull requests until the comments are resolved
[Parameter()]
[int]
$minimumApproverCount = 2,
# Block self approval
[Parameter()]
[bool]
$creatorVoteCounts = $true
)
begin {
$result = @()
Write-Verbose "Starting function: Set-AzDoBranchPolicyMinimalApproval"
}
process {
$params = @{
uri = "$CollectionUri/$ProjectName/_apis/policy/configurations"
version = "7.2-preview.1"
method = 'POST'
}
$getAzDoBranchPolicyTypeSplat = @{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
PolicyType = "Minimum number of reviewers"
}
$policyId = (Get-AzDoBranchPolicyType @getAzDoBranchPolicyTypeSplat).policyId
foreach ($name in $RepoName) {
$getAzDoRepoSplat = @{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
RepoName = $name
}
$repoId = (Get-AzDoRepo @getAzDoRepoSplat).RepoId
$body = @{
isEnabled = $true
isBlocking = $true
type = @{
id = $policyId
}
settings = @{
minimumApproverCount = $minimumApproverCount
creatorVoteCounts = $creatorVoteCounts
allowDownvotes = $false
resetOnSourcePush = $false
requireVoteOnLastIteration = $false
resetRejectionsOnSourcePush = $false
blockLastPusherVote = $false
requireVoteOnEachIteration = $false
scope = @(
@{
repositoryId = $repoId
refName = "refs/heads/$branch"
matchKind = "exact"
}
)
}
}
if ($PSCmdlet.ShouldProcess($ProjectName, "Create Minimal approval policy on: $($PSStyle.Bold)$name$($PSStyle.Reset)")) {
$getAzDoBranchPolicySplat = @{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
ErrorAction = 'SilentlyContinue'
}
$existingPolicy = Get-AzDoBranchPolicy @getAzDoBranchPolicySplat |
Where-Object { ($_.type.id -eq $policyId) -and ($_.settings.scope.refName -eq "refs/heads/$branch") -and ($_.settings.scope.repositoryId -eq $repoId) }
if ($null -eq $existingPolicy) {
$result += ($body | Invoke-AzDoRestMethod @params)
} else {
Write-Warning "Policy on $name/$branch already exists. It is not possible to update policies"
}
} else {
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
}
}
}
end {
if ($result) {
$result | ForEach-Object {
[PSCustomObject]@{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
RepoName = $RepoName
PolicyId = $_.id
url = $_.url
}
}
}
}
}