-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Repository.Environment.psm1
186 lines (161 loc) · 6.31 KB
/
Atlassian.Bitbucket.Repository.Environment.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
using module .\Atlassian.Bitbucket.Authentication.psm1
<#
.SYNOPSIS
Gets all environments in the specified repsitory.
.DESCRIPTION
Gets all environments in the specified repsitory.
.EXAMPLE
C:\PS> Get-BitbucketRepositoryEnvironment -RepoSlug 'Repo'
Gets all environments in the Repo `Repo`.
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER RepoSlug
Name of the repo in Bitbucket.
#>
function Get-BitbucketRepositoryEnvironment {
[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,
[string]$EnvironmentName
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/environments/"
$response = Invoke-BitbucketAPI -Path $endpoint -Paginated
if ($EnvironmentName) {
return $response | Where-Object { $_.Name -eq $EnvironmentName }
}
else {
return $response
}
}
}
<#
.SYNOPSIS
Creates a new environment in the specified repsitory.
.DESCRIPTION
Creates a new environment in the specified repsitory.
.EXAMPLE
C:\PS> New-BitbucketRepositoryEnvironment -RepoSlug 'Repo' -Environment 'QA' -Type 'Test'
Creates a new environment called QA on the repo with a type of Test.
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER RepoSlug
Name of the repo in Bitbucket.
.PARAMETER Environment
Name of the environment.
.PARAMETER Type
Name of the environment type. ['Test', 'Staging','Production']
.PARAMETER Rank
Rank of the environment.
#>
function New-BitbucketRepositoryEnvironment {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
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 = 'Name of the environment.')]
[string]$Environment,
[Parameter( Mandatory = $true,
Position = 2,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the environment type.')]
[ValidateSet('Test', 'Staging', 'Production')]
[string]$Type,
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Rank of the environment.')]
[string]$Rank = 0
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/environments/"
$body = [ordered]@{
type = 'deployment_environment'
name = $Environment
rank = $Rank
environment_type = @{
type = 'deployment_environment_type'
name = $Type
}
lock = [ordered]@{
type = 'deployment_environment_lock_open'
name = 'OPEN'
}
restrictions = [ordered]@{
type = 'deployment_restrictions_configuration'
admin_only = $false
}
hidden = $true
environment_lock_enabled = $true
} | ConvertTo-Json -Depth 3 -Compress
if ($pscmdlet.ShouldProcess("$Environment in $RepoSlug", 'create')) {
return Invoke-BitbucketAPI -Path $endpoint -Method Post -Body $body
}
}
}
<#
.SYNOPSIS
Deletes an environment in the specified repsitory.
.DESCRIPTION
Deletes an environment in the specified repsitory.
.EXAMPLE
C:\PS> Remove-BitbucketRepositoryEnvironment -RepoSlug 'Repo' -Environment 'QA'
Deletes the environment called QA on the Repo.
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER RepoSlug
Name of the repo in Bitbucket.
.PARAMETER Environment
Name of the environment.
#>
function Remove-BitbucketRepositoryEnvironment {
[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 = 'Name of the environment.')]
[string]$Environment
)
Process {
$_environments = Get-BitbucketRepositoryEnvironment -Workspace $Workspace -RepoSlug $RepoSlug
$_uuid = ($_environments | Where-Object { $_.name -eq $Environment }).uuid
if ($_uuid) {
$endpoint = "repositories/$Workspace/$RepoSlug/environments/$_uuid"
if ($pscmdlet.ShouldProcess("$Environment in $RepoSlug", 'delete')) {
return Invoke-BitbucketAPI -Path $endpoint -Method Delete
}
}
}
}