-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Repository.BranchRestriction.psm1
255 lines (227 loc) · 8.73 KB
/
Atlassian.Bitbucket.Repository.BranchRestriction.psm1
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using module .\Atlassian.Bitbucket.Authentication.psm1
using module .\Classes\Atlassian.Bitbucket.BranchRestriction.psm1
using module .\Atlassian.Bitbucket.Tools.psm1
function Get-BitbucketRepositoryBranchRestriction {
[CmdletBinding()]
param(
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[Alias("Team")]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/branch-restrictions/"
Return Invoke-BitbucketAPI -Path $endpoint -Paginated | ConvertTo-BranchRestriction
}
}
function Add-BitbucketRepositoryBranchRestriction {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param(
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[Alias("Team")]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[BranchRestriction]$Restriction
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/branch-restrictions"
# Check for proper usage of the BranchRestriction. Must not use the base type.
if ($Restriction.GetType().Name -eq 'BranchRestriction') {
throw 'Do not use the base type BranchRestriction object. Instead use the MergeCheck or PermissionCheck object type.'
}
# If the branch_match_kind is Glob, remove branch_type property, otherwise the Bitbucket API will throw an exception
if ($Restriction.branch_match_kind -eq 'Glob') {
$globRestriction = $Restriction | Select-Object -ExcludeProperty branch_type -Property *
$body = $globRestriction | ConvertTo-Json -Depth 3 -Compress
}
else {
$body = $Restriction | ConvertTo-Json -Depth 3 -Compress
}
if ($pscmdlet.ShouldProcess("branch restriction: $($Restriction.kind) in $RepoSlug", 'add')) {
Return Invoke-BitbucketAPI -Path $endpoint -Method Post -Body $body
}
}
}
function Set-BitbucketRepositoryBranchRestriction {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param(
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[Alias("Team")]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[BranchRestriction[]]$Restrictions
)
Process {
$currentRestrictions = Get-BitbucketRepositoryBranchRestriction -RepoSlug $RepoSlug -Workspace $Workspace
# Remove extra restrictions
foreach ($current in $currentRestrictions) {
$extra = $true
foreach ($new in $Restrictions) {
Write-Debug "Comparing $($current.kind) with $($new.kind)"
if (Compare-CustomObject $current $new -IgnoreProperty 'id') {
$extra = $false
break
}
}
if ($extra) {
Write-Verbose "Removing Restriction: $($current.kind)"
Remove-BitbucketRepositoryBranchRestriction -RepoSlug $RepoSlug -Workspace $Workspace -RestrictionID $current.id
}
else {
Write-Verbose "Matching Restriction: $($current.kind)"
}
}
# Add missing restrictions
foreach ($new in $Restrictions) {
$missing = $true
foreach ($current in $currentRestrictions) {
if (Compare-CustomObject $new $current -IgnoreProperty 'id') {
$missing = $false
break
}
}
if ($missing) {
Add-BitbucketRepositoryBranchRestriction -RepoSlug $RepoSlug -Workspace $Workspace -Restriction $new
}
}
}
}
function Remove-BitbucketRepositoryBranchRestriction {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param(
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[Alias("Team")]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[Parameter( Mandatory = $true,
Position = 1,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The ID of the branch restriction to delete.')]
[Alias('ID')]
[int]$RestrictionID
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/branch-restrictions/$RestrictionID"
if ($pscmdlet.ShouldProcess("branch restriction: $RestrictionID in $RepoSlug", 'delete')) {
Return Invoke-BitbucketAPI -Path $endpoint -Method Delete
}
}
}
function New-BitbucketRepositoryBranchRestrictionMergeCheck {
[OutputType([MergeCheck])]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
param(
[ValidateSet(
'allow_auto_merge_when_builds_pass',
'enforce_merge_checks',
'require_all_dependencies_merged',
'require_approvals_to_merge',
'require_default_reviewer_approvals_to_merge',
'require_no_changes_requested',
'require_passing_builds_to_merge',
'require_tasks_to_be_completed',
'reset_pullrequest_approvals_on_change',
'reset_pullrequest_changes_requested_on_change'
)]
[string]$Kind,
[Nullable[int]]$Value,
[Parameter(ParameterSetName = "glob")]
[string]$Pattern,
[Parameter(ParameterSetName = "branchtype")]
[ValidateSet(
'feature',
'bugfix',
'release',
'hotfix',
'development',
'production'
)]
[string]$BranchType
)
if ($pscmdlet.ShouldProcess('MergeCheck Object', 'create')) {
if (-not ([string]::IsNullOrEmpty($BranchType))) {
Return [MergeCheck]::New($kind, $branchtype, $value, $false)
}
Return [MergeCheck]::New($kind, $pattern, $value, $true)
}
}
function New-BitbucketRepositoryBranchRestrictionPermissionCheck {
[OutputType([PermissionCheck])]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
param (
[ValidateSet(
'delete',
'force',
'push',
'restrict_merges'
)]
[string]$Kind,
[string]$UUID,
[Parameter(ParameterSetName = "glob")]
[string]$Pattern,
[Parameter(ParameterSetName = "branchtype")]
[ValidateSet(
'feature',
'bugfix',
'release',
'hotfix',
'development',
'production'
)]
[string]$BranchType,
[switch]$IsGroup
)
[string]$target = $Pattern
[bool]$isGlob = $true
if (-not ([string]::IsNullOrEmpty($BranchType))) {
$target = $BranchType
$isGlob = $false
}
if ($pscmdlet.ShouldProcess('PermissionCheck Object', 'create')) {
Return [PermissionCheck]::New($kind, $uuid, $target, $isGlob, $isgroup)
}
}
function ConvertTo-BranchRestriction {
[OutputType([MergeCheck])]
[CmdletBinding()]
param(
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true)]
[PSCustomObject]$Object
)
Process {
switch -regex ($Object.kind) {
'^require_|enforce_|reset_' { [MergeCheck]::New($Object) }
Default { [PermissionCheck]::New($Object) }
}
}
}