forked from beyondcomputing-org/Atlassian.Bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtlassian.Bitbucket.Repository.Reviewer.psm1
147 lines (133 loc) · 5.32 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
using module .\Atlassian.Bitbucket.Authentication.psm1
function Get-BitbucketRepositoryReviewer {
[CmdletBinding()]
Param (
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the team in Bitbucket. Defaults to selected team if not provided.')]
[string]$Team = (Get-BitbucketSelectedTeam),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug
)
Process {
$endpoint = "repositories/$Team/$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 team in Bitbucket. Defaults to selected team if not provided.')]
[string]$Team = (Get-BitbucketSelectedTeam),
[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 = 'Name of the user to add as a default reviewer.')]
[string]$Nickname
)
Process {
$endpoint = "repositories/$Team/$RepoSlug/default-reviewers/$Nickname"
if ($pscmdlet.ShouldProcess($RepoSlug, "Add $Nickname to default reviewers")) {
$response = Invoke-BitbucketAPI -Path $endpoint -Method Put
if($response){
return [pscustomobject]@{
Nickname = $Nickname
Team = $Team
RepoSlug = $RepoSlug
Action = 'Added'
}
}else{
throw 'Bad Request'
}
}
}
}
function Remove-BitbucketRepositoryReviewer {
[CmdletBinding( SupportsShouldProcess = $true,
ConfirmImpact = 'High')]
Param (
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the team in Bitbucket. Defaults to selected team if not provided.')]
[string]$Team = (Get-BitbucketSelectedTeam),
[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 = 'Name of the user to be removed as a default reviewer.')]
[string]$Nickname
)
Process {
$endpoint = "repositories/$Team/$RepoSlug/default-reviewers/$Nickname"
if ($pscmdlet.ShouldProcess($RepoSlug, "Remove $Nickname from default reviewers")) {
Invoke-BitbucketAPI -Path $endpoint -Method Delete
[pscustomobject]@{
Nickname = $Nickname
Team = $Team
RepoSlug = $RepoSlug
Action = 'Deleted'
}
}
}
}
function Set-BitbucketRepositoryReviewer {
[CmdletBinding( SupportsShouldProcess = $true,
ConfirmImpact = 'High')]
Param (
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the team in Bitbucket. Defaults to selected team if not provided.')]
[string]$Team = (Get-BitbucketSelectedTeam),
[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 = 'Array of users to set as the default reviewers.')]
[string[]]$Nicknames
)
Process {
$existingUsers = (Get-BitbucketRepositoryReviewer -Team $Team -RepoSlug $RepoSlug).nickname
if ($existingUsers.Count -eq 0) {
foreach ($nickname in $Nicknames) {
Add-BitbucketRepositoryReviewer -Team $Team -RepoSlug $RepoSlug -Nickname $nickname
}
}
else {
# Calculate the delta between existing and expected users
$delta = Compare-Object -ReferenceObject $existingUsers -DifferenceObject $Nicknames
# Add missing users
$missingUsers = ($delta | Where-Object { $_.SideIndicator -eq '=>' }).InputObject
if ($missingUsers) {
$missingUsers | Add-BitbucketRepositoryReviewer -Team $Team -RepoSlug $RepoSlug
}
# Remove extra users
$extraUsers = ($delta | Where-Object { $_.SideIndicator -eq '<=' }).InputObject
if ($extraUsers) {
$extraUsers | Remove-BitbucketRepositoryReviewer -Team $Team -RepoSlug $RepoSlug
}
}
}
}