Skip to content

Instantly share code, notes, and snippets.

@reallydontask
Created July 8, 2022 09:48
Show Gist options
  • Save reallydontask/7e7e01695b13635946dafe77c65fab01 to your computer and use it in GitHub Desktop.
Save reallydontask/7e7e01695b13635946dafe77c65fab01 to your computer and use it in GitHub Desktop.
Validate Azure Pipelines
#!/usr/bin/env pwsh
$pipelines = @{}
Import-CSV .githooks/pipelines.csv | ForEach-Object { $pipelines.Add($_.file, $_.url)}
#No point in trying to invoke an api without a token
if ($null -ne $env:AZDO_PAT) {
$token = [convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([string]::Format("{0}:{1}", "", $env:AZDO_PAT)))
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Basic $token"
}
git diff --cached --name-status |
# We only want yml or yaml files in yaml-templates and not in variables.
# We also don't care about deleted pipelines, which start with D
Where-Object { $_ -match 'yaml-templates' `
-and $_ -notmatch '/variables/' `
-and $_ -match '.yml|.yaml' `
-and $_[0] -ne "D" } |
ForEach-Object {
$file = $_.Substring(1, $_.Length - 1).Trim()
#Need Raw to get a string not a string array, which messes up the json
$body = @{"previewRun" = $true; "yamlOverride" = $(Get-Content $file -Raw) } | ConvertTo-Json
if ($pipelines.ContainsKey($file)) {
$url = "$($pipelines[$file])/preview?api-version=7.1-preview.1"
Write-Host $url
try {
Invoke-RestMethod -Method POST -Headers $headers -Uri $url -Body $body
}
catch {
if ($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host "Unathorized. Expired PAT? Wrong Permissions?"
}
elseif ($_.Exception.Response.StatusCode -eq "BadRequest") {
$err = $_ | ConvertFrom-Json
Write-Host $err.message
}
else {
Write-Host "Something has gone wrong. Exception: $_"
}
exit 1
}
}
else {
Write-Host "Pipeline not found in the list. Add it to the list if you want it to be validated."
}
}
exit 0
}
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$configFile="../.githooks/pipelines.csv" # Oh, yeah back to the 90s
)
if ($null -ne $env:AZDO_PAT) {
Write-Host "Backup old file, seems like a good idea ... shrug"
Move-Item -Path $configFile -Destination "$configFile.bak"
$pipelines=@{}
$token = [convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([string]::Format("{0}:{1}", "", $env:AZDO_PAT)))
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Basic $token"
}
$url = 'https://dev.azure.com/MoJ-OFFICIAL/37d05f38-b6af-424d-bad2-fbcd2f10cee6/_apis/pipelines'
Write-Host "Getting All Pipelines"
$ADOPipelines = Invoke-RestMethod -Method GET -Uri $url -Headers $headers
#Headers for csv file
"file,url" | Out-File -FilePath $configFile -Append
$ADOPipelines.value | ForEach-Object {
Write-Host "Processing Pipeline: $($_.name), id: $($_.id)"
$pipeline = Invoke-RestMethod -Method GET -Uri $_.url -Headers $headers;
$url = $pipeline.url -replace '\?revision=\d{0,}', ''
$path = $pipeline.configuration.path.TrimStart('/')
#If this fails it means that we have a bit of an issue, chances are that we have two pipelines refering to the same file
$pipelines.Add($path,$url)
"$path,$url" | Out-File -FilePath $configFile -Append
}
}
else {
Write-Host "No PAT found. Ensure that an environment variable named AZDO_PAT is set."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment