Skip to content

Commit

Permalink
bugfix: Handle pipeline and job variables better
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-peterson committed Oct 3, 2023
1 parent 47b31aa commit 61892d6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@{
ModuleVersion = '1.101.2'
ModuleVersion = '1.101.3'

PrivateData = @{
PSData = @{
LicenseUri = 'https://github.com/chris-peterson/pwsh-gitlab/blob/main/LICENSE'
ProjectUri = 'https://github.com/chris-peterson/pwsh-gitlab'
ReleaseNotes = 'Move global variables to init script'
ReleaseNotes = 'Better handling for pipeline and job variables'
}
}

Expand Down Expand Up @@ -242,6 +242,7 @@
'ConvertTo-PascalCase'
'ConvertTo-SnakeCase'
'ConvertTo-UrlEncoded'
'ConvertTo-GitlabVariables'
'Get-FilteredObject'
'Get-GitlabVersion'
'Invoke-GitlabApi'
Expand Down
14 changes: 3 additions & 11 deletions src/GitlabCli/Jobs.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,14 @@ function Start-GitlabJob {
HttpMethod = "POST"
Path = "projects/$($Project.Id)/jobs/$JobId/play"
SiteUrl = $SiteUrl
Body = @{
job_variables_attributes = @{}
}
Body = @{}
}

if ($Variables) {
$ReformattedVariables = $Variables.GetEnumerator() | ForEach-Object {
@{
key = $_.Name
value = $_.Value
}
}
$GitlabApiArguments.Body.job_variables_attributes = @($ReformattedVariables)
$GitlabApiArguments.Body.job_variables_attributes = $Variables | ConvertTo-GitlabVariables
}

if ($PSCmdlet.ShouldProcess("start job $($Project.PathWithNamespace)", "$($GitlabApiArguments | ConvertTo-Json)")) {
if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)", "start job $($GitlabApiArguments | ConvertTo-Json)")) {
try {
# https://docs.gitlab.com/ee/api/jobs.html#run-a-job
Invoke-GitlabApi @GitlabApiArguments | New-WrapperObject "Gitlab.Job"
Expand Down
11 changes: 2 additions & 9 deletions src/GitlabCli/Pipelines.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -809,14 +809,7 @@ function New-GitlabPipeline {
}

if ($Variables) {
$ReformattedVariables = $Variables.GetEnumerator() | ForEach-Object {
@{
variable_type = 'env_var'
key = $_.Name
value = $_.Value
}
}
$Request.variables = @($ReformattedVariables)
$Request.variables = $Variables | ConvertTo-GitlabVariables -Type 'env_var'
}

$GitlabApiArguments = @{
Expand All @@ -826,7 +819,7 @@ function New-GitlabPipeline {
SiteUrl = $SiteUrl
}

if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)", "create new pipeline ($($Request | ConvertTo-Json))")) {
if ($PSCmdlet.ShouldProcess("$($Project.PathWithNamespace)", "create new pipeline $($Request | ConvertTo-Json)")) {
$Pipeline = Invoke-GitlabApi @GitlabApiArguments | New-WrapperObject 'Gitlab.Pipeline'

if ($Wait) {
Expand Down
36 changes: 36 additions & 0 deletions src/GitlabCli/Variables.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,39 @@ function Resolve-GitlabVariable {
}
}
}
function ConvertTo-GitlabVariables {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
$Object,

[Parameter()]
[Alias('Type')]
[string]
$VariableType
)

if ($Object) {
$Enumerator = switch ($Object.GetType().Name) {
hashtable {
$Object.GetEnumerator()
}
pscustomobject {
$Object.PSObject.Properties
}
default {
throw "Unsupported type for '-Object' (expected [hashtable] or [pscustomobject])"
}
}
$Enumerator | ForEach-Object {
$Var = @{
key = $_.Name
value = $_.Value
}
if ($VariableType) {
$Var.variable_type = $VariableType
}
$Var
}
}
}

0 comments on commit 61892d6

Please sign in to comment.