-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Repository.BranchModel.psm1
199 lines (168 loc) · 6.84 KB
/
Atlassian.Bitbucket.Repository.BranchModel.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
using module .\Atlassian.Bitbucket.Authentication.psm1
<#
.SYNOPSIS
Returns the current Branch Model Configuration for a given repository.
.DESCRIPTION
Returns the current Branch Model Configuration for a given repository.
.EXAMPLE
C:\ PS> Get-BitbucketRepositoryBranchModel -RepoSlug 'repo'
Returns the Branch Model Configuration for the Repository '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-BitbucketRepositoryBranchModel {
[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,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/branching-model"
return Invoke-BitbucketAPI -Path $endpoint
}
}
<#
.SYNOPSIS
Modifies the Branch Model Configuration for a given Repository
.DESCRIPTION
Use this Function to modify the Branch Model Configuration for a given Repository.
Specify the Production and Development Branches as well as Branch Prefixes.
.EXAMPLE
C:\ PS> Set-BitbucketRepositoryBranchModel -RepoSlug 'repo' -Branch 'development' -TargetBranch 'develop'
Specifies the Branch named 'develop' as the development branch.
.EXAMPLE
C:\ PS> Set-BitbucketRepositoryBranchModel -RepoSlug 'repo' -Branch 'production' -UseMainBranch
Specifies the current main branch as the production branch.
.EXAMPLE
C:\ PS> Set-BitbucketRepositoryBranchModel -RepoSlug 'repo' -Branch 'production' -UseMainBranch -Enabled:$false
Disables the production branch
.EXAMPLE
C:\ PS> Set-BitbucketRepositoryBranchModel -RepoSlug 'repo' -BranchTypePrefix 'feature' -BranchPrefix 'new-feature/' -Enabled
Enables the 'feature' Branch Prefix and sets the Prefix to 'new-feature/'
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER RepoSlug
Name of the repo in Bitbucket.
.PARAMETER Branch
Name of the Branch to re-target
.PARAMETER UseMainBranch
Target the current Main Branch
.PARAMETER TargetBranch
Target the specified Branch
.PARAMETER BranchTypePrefix
Name of the Branch Prefix to Modify
.PARAMETER BranchPrefix
The Prefix to apply to the specified BranchTypePrefix
.PARAMETER Enabled
Enables the production or specified BranchTypePrefix
#>
function Set-BitbucketRepositoryBranchModel {
[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( Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[Parameter( ParameterSetName = 'branchmodel',
HelpMessage = 'Which Branch to Modify')]
[ValidateSet(
'development',
'production'
)]
[string]$Branch,
[Parameter( ParameterSetName = 'branchmodel',
HelpMessage = 'Set Branch to Target Main Branch')]
[switch]$UseMainBranch,
[Parameter( ParameterSetName = 'branchmodel',
HelpMessage = 'Set Branch to Target a Named Branch')]
[ValidateNotNullOrEmpty()]
[string]$TargetBranch,
[Parameter( ParameterSetName = 'prefix',
HelpMessage = 'Modify the specified Branch Type Prefix')]
[ValidateSet(
'bugfix',
'feature',
'hotfix',
'release'
)]
[string]$BranchTypePrefix,
[Parameter( ParameterSetName = 'prefix',
HelpMessage = 'Set the Prefix for the specified Branch Type')]
[ValidateNotNullOrEmpty()]
[string]$BranchPrefix,
[Parameter(HelpMessage = 'Enable the specified item (development is always enabled)')]
[switch]$Enabled
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/branching-model/settings"
if ($Branch -eq 'development') {
if ($UseMainBranch) {
$body = [ordered]@{
development = [ordered]@{
name = $null
use_mainbranch = $true
}
} | ConvertTo-Json -Depth 2 -Compress
}
else {
$body = [ordered]@{
development = [ordered]@{
name = $TargetBranch
use_mainbranch = $false
}
} | ConvertTo-Json -Depth 2 -Compress
}
$target = $Branch
}
elseif ($Branch -eq 'production') {
if ($UseMainBranch) {
$body = [ordered]@{
production = [ordered]@{
name = $null
use_mainbranch = $true
enabled = if ($Enabled) { $true } else { $false }
}
} | ConvertTo-Json -Depth 2 -Compress
}
else {
$body = [ordered]@{
production = [ordered]@{
name = $TargetBranch
use_mainbranch = $false
enabled = if ($Enabled) { $true } else { $false }
}
} | ConvertTo-Json -Depth 2 -Compress
}
$target = $Branch
}
elseif ($BranchTypePrefix.Length -gt 0) {
$body = [ordered]@{
branch_types = @([ordered]@{
kind = $BranchTypePrefix
enabled = if ($Enabled) { $true } else { $false }
prefix = $BranchPrefix
})
} | ConvertTo-Json -Depth 3 -Compress
$target = $BranchTypePrefix
}
if ($PSCmdlet.ShouldProcess("$target", 'Update Branching Model')) {
return Invoke-BitbucketAPI -Path $endpoint -Body $body -Method Put
}
}
}