-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Repository.Reviewer.psm1
156 lines (142 loc) · 5.67 KB
/
Atlassian.Bitbucket.Repository.Reviewer.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
using module .\Atlassian.Bitbucket.Authentication.psm1
function Get-BitbucketRepositoryReviewer {
[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/default-reviewers"
return Invoke-BitbucketAPI -Path $endpoint -Method Get -Paginated
}
}
function Add-BitbucketRepositoryReviewer {
[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 uuid of the user to add as a default reviewer.')]
[string]$UUID
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/default-reviewers/$UUID"
if ($pscmdlet.ShouldProcess($RepoSlug, "Add $UUID to default reviewers")) {
$response = Invoke-BitbucketAPI -Path $endpoint -Method Put
if ($response) {
return [pscustomobject]@{
UUID = $UUID
Workspace = $Workspace
RepoSlug = $RepoSlug
Action = 'Added'
}
}
else {
throw 'Bad Request'
}
}
}
}
function Remove-BitbucketRepositoryReviewer {
[CmdletBinding( SupportsShouldProcess = $true,
ConfirmImpact = 'High')]
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 uuid of the user to be removed as a default reviewer.')]
[string]$UUID
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/default-reviewers/$UUID"
if ($pscmdlet.ShouldProcess($RepoSlug, "Remove $UUID from default reviewers")) {
Invoke-BitbucketAPI -Path $endpoint -Method Delete
[pscustomobject]@{
UUID = $UUID
Workspace = $Workspace
RepoSlug = $RepoSlug
Action = 'Deleted'
}
}
}
}
function Set-BitbucketRepositoryReviewer {
[CmdletBinding( SupportsShouldProcess = $true,
ConfirmImpact = 'High')]
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,
ValueFromPipeline = $true,
HelpMessage = 'Array of users to set as the default reviewers.')]
[string[]]$UUIDs
)
Process {
$existingUsers = Get-BitbucketRepositoryReviewer -Workspace $Workspace -RepoSlug $RepoSlug
if ($existingUsers.Count -eq 0) {
foreach ($uuid in $UUIDs) {
Add-BitbucketRepositoryReviewer -Workspace $Workspace -RepoSlug $RepoSlug -UUID $uuid
}
}
else {
# Calculate the delta between existing and expected users
$delta = Compare-Object -ReferenceObject $existingUsers.uuid -DifferenceObject $UUIDs
# Add missing users
$missingUsers = ($delta | Where-Object { $_.SideIndicator -eq '=>' }).InputObject
if ($missingUsers) {
foreach ($missingUser in $missingUsers) {
Add-BitbucketRepositoryReviewer -Workspace $Workspace -RepoSlug $RepoSlug -UUID $missingUser
}
}
# Remove extra users
$extraUsers = ($delta | Where-Object { $_.SideIndicator -eq '<=' }).InputObject
if ($extraUsers) {
foreach ($extraUser in $extraUsers) {
Remove-BitbucketRepositoryReviewer -Workspace $Workspace -RepoSlug $RepoSlug -UUID $extraUser
}
}
}
}
}