forked from beyondcomputing-org/Atlassian.Bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtlassian.Bitbucket.Repository.Environment.Variable.psm1
142 lines (130 loc) · 6.11 KB
/
Atlassian.Bitbucket.Repository.Environment.Variable.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
using module .\Atlassian.Bitbucket.Authentication.psm1
using module .\Atlassian.Bitbucket.Repository.Environment.psm1
function Get-BitbucketRepositoryEnvironmentVariable {
[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,
[Parameter( Mandatory=$true,
Position=1,
ValueFromPipelineByPropertyName=$true,
HelpMessage='Name of the environment.')]
[string]$Environment
)
Begin {
Write-Warning 'This functionality uses an internal Bitbucket API. The functionality required is not present in the Public API.'
Write-Warning 'Because this is using an internal API it may break in the future and require an update.'
}
Process {
$_environments = Get-BitbucketRepositoryEnvironment -Team $Team -RepoSlug $RepoSlug
$_uuid = ($_environments | Where-Object {$_.name -eq $Environment}).uuid
if($_uuid){
$endpoint = "repositories/$Team/$RepoSlug/deployments_config/environments/$_uuid/variables/"
return Invoke-BitbucketAPI -Path $endpoint -Paginated -InternalAPI
}else{
Throw "Couldn't find the environment: $Environment"
}
}
}
function New-BitbucketRepositoryEnvironmentVariable {
[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( Mandatory=$true,
Position=1,
ValueFromPipelineByPropertyName=$true,
HelpMessage='Name of the environment.')]
[string]$Environment,
[Parameter( Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage='Variable key')]
[string]$Key,
[Parameter( Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage='Variable value')]
[string]$Value,
[Parameter( ValueFromPipelineByPropertyName=$true,
HelpMessage='Obscure the variable value')]
[switch]$Secured
)
Begin {
Write-Warning 'This functionality uses an internal Bitbucket API. The functionality required is not present in the Public API.'
Write-Warning 'Because this is using an internal API it may break in the future and require an update.'
}
Process {
$_environments = Get-BitbucketRepositoryEnvironment -Team $Team -RepoSlug $RepoSlug
$_uuid = ($_environments | Where-Object {$_.name -eq $Environment}).uuid
if($_uuid){
$body = [ordered]@{
key = $Key
secured = $Secured.IsPresent
value = $Value
} | ConvertTo-Json -Depth 1 -Compress
$endpoint = "repositories/$Team/$RepoSlug/deployments_config/environments/$_uuid/variables/"
if ($pscmdlet.ShouldProcess("$Key in the environment $Environment in the repo $RepoSlug", 'create')){
return Invoke-BitbucketAPI -Path $endpoint -Method Post -Body $body -InternalAPI
}
}else{
Throw "Couldn't find the environment: $Environment"
}
}
}
function Remove-BitbucketRepositoryEnvironmentVariable {
[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 environment.')]
[string]$Environment,
[Parameter( Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage='Variable key')]
[string]$Key
)
Begin {
Write-Warning 'This functionality uses an internal Bitbucket API. The functionality required is not present in the Public API.'
Write-Warning 'Because this is using an internal API it may break in the future and require an update.'
}
Process {
$_uuidEnv = (Get-BitbucketRepositoryEnvironment -Team $Team -RepoSlug $RepoSlug | Where-Object {$_.name -eq $Environment}).uuid
if($_uuidEnv){
$_uuidVar = (Get-BitbucketRepositoryEnvironmentVariable -Team $Team -RepoSlug $RepoSlug -Environment $Environment | Where-Object {$_.key -eq $Key}).uuid
if($_uuidVar){
$endpoint = "repositories/$Team/$RepoSlug/deployments_config/environments/$_uuidEnv/variables/$_uuidVar"
if ($pscmdlet.ShouldProcess("$Key in the environment $Environment in the repo $RepoSlug", 'delete')){
return Invoke-BitbucketAPI -Path $endpoint -Method Delete -InternalAPI
}
}
}else{
Throw "Couldn't find the environment: $Environment"
}
}
}