Files

67 lines
2.1 KiB
PowerShell

param(
[string]$ProjectId = "COMPANY/techci/openeng/powershell/vmware-tools",
[string]$GitLabBase = "https://URL"
)
# GitLab API requires URL-encoded project identifier; encode '/' as %2F
#function Format-ProjectId ([string]$path) {
# return [uri]::EscapeDataString($path).Replace("/", "%2F")
#}
#$encodedId = Format-ProjectId $ProjectPath
$Headers = @{ "JOB-TOKEN" = $env:CI_JOB_TOKEN }
#$Uri = "$GitLabBase/api/v4/projects/$encodedId/jobs?per_page=100"
$Uri = "$GitLabBase/api/v4/projects/$([uri]::EscapeDataString($ProjectId))/jobs?per_page=100"
#$resp = Invoke-WebRequest -UseBasicParsing $Uri -Headers $Headers -Method Get -ErrorAction Stop
#$jobs = $resp.Content | ConvertFrom-Json
$jobs = Invoke-RestMethod -UseBasicParsing $Uri -Headers $Headers -Method Get
$Array = $jobs | Where-Object { $_.Stage -eq "Deploy" } | Select-Object -ExpandProperty id | Sort-Object -Descending
$Number_of_Jobs = $Array.Count
$Version = "1.0.0"
for ($i = 1; $i -le $Number_of_Jobs; $i++) {
$New_Version = Update-Version -Version $Version
$Version = $New_Version
Write-Output "New Version: $Version"
}
Function Update-Version {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Version,
# Which segment to increment: -1 = last (default), 0 = first, etc.
[int]$SegmentIndex = -1,
# Max value for non-major segments before rollover
[int]$SegmentMax = 9
)
$Parts = $Version.Split('.')
# negative index normalization trick
If ($SegmentIndex -lt 0) { $SegmentIndex = $Parts.Count + $SegmentIndex }
# Convert to integer
$Numbers = foreach ($Part in $Parts) { [int]$Part }
# Increment with carry; major (index 0) has no cap
$i = $SegmentIndex
while ($i -ge 0) {
$Numbers[$i]++
# Major segment: unlimited growth; stop carrying
if ($i -eq 0) { break }
if ($Numbers[$i] -le $SegmentMax) { break }
# rollover this non-major segment and carry left
$Numbers[$i] = 0
$i--
}
($Numbers -join '.')
}