-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Repository.GroupPermission.psm1
165 lines (150 loc) · 6.03 KB
/
Atlassian.Bitbucket.Repository.GroupPermission.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
using module .\Atlassian.Bitbucket.Authentication.psm1
using module .\Classes\Atlassian.Bitbucket.Permissions.psm1
using module .\Atlassian.Bitbucket.Tools.psm1
function Get-BitbucketRepositoryGroupPermission {
[OutputType([GroupPermissionV1])]
[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 = "group-privileges/$Workspace/$RepoSlug"
$permissions = Invoke-BitbucketAPI -Path $endpoint -API_Version '1.0'
foreach ($permission in $permissions) {
[GroupPermissionV1]::New($permission)
}
}
}
function Add-BitbucketRepositoryGroupPermission {
[OutputType([GroupPermissionV1])]
[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 group slug.')]
[string]$GroupSlug,
[Parameter( Mandatory = $true,
Position = 2,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The permission to give the group.')]
[ValidateSet('read', 'write', 'admin')]
[String]$Privilege
)
Process {
$endpoint = "group-privileges/$Workspace/$RepoSlug/$Workspace/$GroupSlug"
if ($pscmdlet.ShouldProcess("$GroupSlug $privilege in repo: $RepoSlug", 'grant')) {
$response = Invoke-BitbucketAPI -Path $endpoint -Method Put -Body $privilege -API_Version '1.0' -ContentType 'application/x-www-form-urlencoded'
Return [GroupPermissionV1]::New($response)
}
}
}
function Set-BitbucketRepositoryGroupPermission {
[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,
[GroupPermissionV1[]]$Permissions
)
Process {
$currentPermissions = Get-BitbucketRepositoryGroupPermission -RepoSlug $RepoSlug -Workspace $Workspace
# Remove extra permissions
foreach ($current in $currentPermissions) {
$extra = $true
foreach ($new in $Permissions) {
if (Compare-CustomObject $current $new) {
$extra = $false
break
}
}
if ($extra) {
$current | Remove-BitbucketRepositoryGroupPermission -RepoSlug $RepoSlug -Workspace $Workspace
}
else {
Write-Verbose "Matching Permission: $($current.groupslug) - $($current.privilege)"
}
}
# Add missing permissions
foreach ($new in $Permissions) {
$missing = $true
foreach ($current in $currentPermissions) {
if (Compare-CustomObject $new $current) {
$missing = $false
break
}
}
if ($missing) {
$new | Add-BitbucketRepositoryGroupPermission -RepoSlug $RepoSlug -Workspace $Workspace
}
}
}
}
function New-BitbucketRepositoryGroupPermission {
[OutputType([GroupPermissionV1])]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
param(
[string]$GroupSlug,
[ValidateSet('read', 'write', 'admin')]
[string]$Privilege
)
if ($pscmdlet.ShouldProcess('GroupPermissionV1 Object', 'create')) {
Return [GroupPermissionV1]::New($GroupSlug, $Privilege)
}
}
function Remove-BitbucketRepositoryGroupPermission {
[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 Slug of the group to be removed.')]
[string]$GroupSlug
)
Process {
$endpoint = "group-privileges/$Workspace/$RepoSlug/$Workspace/$GroupSlug"
if ($pscmdlet.ShouldProcess("$GroupSlug in repo: $RepoSlug", 'delete')) {
Return Invoke-BitbucketAPI -Path $endpoint -Method Delete -API_Version '1.0'
}
}
}