-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-BitbucketCloudBranchRestriction.ps1
More file actions
53 lines (47 loc) · 1.66 KB
/
Copy pathNew-BitbucketCloudBranchRestriction.ps1
File metadata and controls
53 lines (47 loc) · 1.66 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
Function New-BitbucketCloudBranchRestriction {
param([Parameter(Mandatory = $false)] [PSCustomObject] $Session = (Get-BitbucketSession),
[Parameter(Mandatory = $false)] [String] $Workspace = $Session.Workspace,
[Parameter(Mandatory = $true)] [String] $Repository,
[Parameter(Mandatory = $false)] [String[]] $AllowedGroups,
[Parameter(Mandatory = $false)] [String[]] $AllowedUsers,
[Parameter(Mandatory = $true)] [String] $Branch
)
$payload = @{
type = "branchrestriction"
branch_match_kind="glob"
pattern=$Branch
kind = "push"
}
if($AllowedUsers){
$payload.Add("users", @($AllowedUsers | ForEach-Object {
return @{
username = $_
}
}))
}
if($AllowedGroups){
$payload.Add("groups", @($AllowedGroups | ForEach-Object {
return @{
slug = ($_.Replace(" ","-") -replace "\W","-").ToLower()
}
}))
}
$BranchRestriction = Get-BitbucketCloudBranchRestrictions `
-Repository $Repository | Where-Object pattern -like $Branch
if( $BranchRestriction ){
return ($payload -as [PSCustomObject] | ConvertTo-Json | Invoke-RestMethod `
-Uri "$($Session.BaseUrl)/2.0/repositories/$Workspace/$Repository/branch-restrictions/$($BranchRestriction.id)" `
-Method PUT `
-Headers @{
"Content-Type" = "application/json"
Authorization = $Session.Authorization
})
}
return ($payload | ConvertTo-Json | Invoke-RestMethod `
-Uri "$($Session.BaseUrl)/2.0/repositories/$Workspace/$Repository/branch-restrictions" `
-Method POST `
-Headers @{
"Content-Type" = "application/json"
Authorization = $Session.Authorization
})
}