forked from beyondcomputing-org/Atlassian.Bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atlassian.Bitbucket.Pipeline.psm1
122 lines (108 loc) · 4.11 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
using module .\Atlassian.Bitbucket.Authentication.psm1
function Start-BitbucketPipeline {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Low')]
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( Position=1,
ValueFromPipelineByPropertyName=$true)]
[string]$Branch = 'master',
[Parameter( Position=2,
ValueFromPipelineByPropertyName=$true)]
[string]$CustomPipe,
[Parameter( Position=3,
ValueFromPipelineByPropertyName=$true)]
[HashTable[]]$Variables
)
Process {
$endpoint = "repositories/$Team/$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 team in Bitbucket. Defaults to selected team if not provided.')]
[string]$Team = (Get-BitbucketSelectedTeam),
[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/$Team/$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
}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
}
}