-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Pipeline.psm1
315 lines (270 loc) · 10.9 KB
/
Atlassian.Bitbucket.Pipeline.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using module .\Atlassian.Bitbucket.Authentication.psm1
function Enable-BitbucketPipelineConfig {
[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
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/pipelines_config"
$body = @{
enabled = $true
} | ConvertTo-Json -Depth 1 -Compress
if ($pscmdlet.ShouldProcess("pipelines on repo $RepoSlug", 'enable')) {
return Invoke-BitbucketAPI -Path $endpoint -Body $body -Method Put
}
}
}
function Get-BitbucketPipelineConfig {
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/pipelines_config"
return Invoke-BitbucketAPI -Path $endpoint -Method Get
}
}
function Start-BitbucketPipeline {
[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( Position = 1,
ValueFromPipelineByPropertyName = $true)]
[string]$Branch = 'master',
[Parameter( Position = 2,
ValueFromPipelineByPropertyName = $true)]
[string]$CustomPipe,
[Parameter( Position = 3,
ValueFromPipelineByPropertyName = $true)]
[HashTable[]]$Variables
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/pipelines/"
# Add selector for custom pipes
if ($CustomPipe) {
$body = [ordered]@{
target = [ordered]@{
type = 'pipeline_ref_target'
ref_type = 'branch'
ref_name = $Branch
selector = [ordered]@{
type = 'custom'
pattern = $CustomPipe
}
}
}
}
else {
$body = [ordered]@{
target = [ordered]@{
type = 'pipeline_ref_target'
ref_type = 'branch'
ref_name = $Branch
}
}
}
# Add variables
if ($Variables) {
$body | Add-Member -NotePropertyName variables -NotePropertyValue $Variables
}
$body = $body | ConvertTo-Json -Depth 5 -Compress
if ($pscmdlet.ShouldProcess('pipeline', 'start')) {
return Invoke-BitbucketAPI -Path $endpoint -Body $body -Method Post
}
}
}
function Wait-BitbucketPipeline {
[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( Position = 0,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository object from Bitbucket.')]
$repository,
[Parameter( Position = 1,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[Alias('uuid')]
[string]$PipelineUUID,
[int]$SleepSeconds = 10,
[int]$TimeoutSeconds = 7200
)
Process {
if ($repository) {
$RepoSlug = $repository.uuid
}
if (!$RepoSlug) {
Throw 'A repo must be provided'
}
$endpoint = "repositories/$Workspace/$RepoSlug/pipelines/$PipelineUUID"
Write-Progress -Id 0 'Watching pipeline for successful completion...'
$poll = $true
do {
$response = Invoke-BitbucketAPI -Path $endpoint
if ($response.state.name -eq 'COMPLETED') {
$poll = $false
}
elseif($response.state.name -in ('PENDING', 'IN_PROGRESS') -and $response.state.stage.name -in ('PAUSED', 'HALTED')){
Throw "The triggered pipeline was $($response.state.stage.name). Failing Build!!"
}
else {
if ($TimeoutSeconds -lt $SleepSeconds) {
Throw "The $TimeoutSeconds second timeout expired before the pipeline completed."
}
else {
Write-Verbose "Pipeline has not completed yet. Waiting for $SleepSeconds more seconds before re-checking. $TimeoutSeconds left before timing out."
$TimeoutSeconds -= $SleepSeconds
Start-Sleep -Seconds $SleepSeconds
}
}
} while ($poll)
return $response
}
}
<#
.SYNOPSIS
Get pipeline details
.DESCRIPTION
Returns details for all returned pipelines. A specific pipeline can be specified by UUID or build number.
.EXAMPLE
C:\PS> Get-BitbucketPipeline -Workspace 'MyWorkspace' -RepoSlug 'my.repo'
# Returns details for the last 20 pipeline run in 'my.repo'
.EXAMPLE
C:\PS> Get-BitbucketPipeline -Workspace 'MyWorkspace' -RepoSlug 'my.repo' -UUID '{d9448101-f9f3-4024-bda8-c412cb17654a}'
# Returns details for the pipeline with the provided UUID - Note that the UUID will take the pipeline build number as well
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER Repo
The repo slug.
.PARAMETER UUID
Either the unique ID of a pipeline or a pipeline build number. If provided only returns results for the pipeline specified.
.PARAMETER State
The state of the pipeline. If provided, filters results to pipelines with the specified state.
.PARAMETER Sort
The property to sort pipelines on prior to returning results. Deafults to created_on.
.PARAMETER Page
The page number of results to return. Defaults to 1.
.PARAMETER Limit
The number of results to return per page. Defaults to 20. Maximum is 100.
#>
function Get-BitbucketPipeline {
param (
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[Parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 1)]
[Alias('Id')]
[string]$UUID,
[string]$State,
[string]$Sort = '-created_on',
[int]$Page = 1,
[int]$Limit = 20
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/pipelines/"
if ($UUID) {
$endpoint += "$UUID"
return @(Invoke-BitbucketAPI -Path $endpoint -Method Get)
}
else {
$endpoint += "?&sort=$Sort&page=$Page&pagelen=$Limit"
if ($State) {
$endpoint += "&status=$($State.ToUpper())"
}
return (Invoke-BitbucketAPI -Path $endpoint -Method Get).values
}
}
}
<#
.SYNOPSIS
Get pipeline step details
.DESCRIPTION
Returns details for all returned pipeline steps. A specific pipeline step can be specified by UUID.
.EXAMPLE
C:\PS> Get-BitbucketPipelineStep -Workspace 'MyWorkspace' -RepoSlug 'my.repo' -PipelineUUID '{d9448101-f9f3-4024-bda8-c412cb17654a}'
# Returns details for the steps in the pipeline specified
.EXAMPLE
C:\PS> Get-BitbucketPipelineStep -Workspace 'MyWorkspace' -RepoSlug 'my.repo' -PipelineUUID '{d9448101-f9f3-4024-bda8-c412cb17654a}' -UUID 'ea664fec-5cc2-4eb6-b864-aa604a2e3918
'
# Returns details for the pipeline step with the provided UUID
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER Repo
The repo slug.
.PARAMETER PipelineUUID
Either the unique ID of a pipeline or a pipeline build number. If provided only returns results for the pipeline specified.
.PARAMETER UUID
The unique ID of a pipeline step. If provided only returns results for the pipeline step specified.
#>
function Get-BitbucketPipelineStep {
param (
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[Parameter( Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
Position = 1)]
[string]$PipelineUUID,
[Parameter( ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 2)]
[Alias('Id')]
[string]$UUID
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/pipelines/$PipelineUUID/steps/$UUID`?pagelen=100"
return (Invoke-BitbucketAPI -Path $endpoint -Method Get).values
}
}