| 1 |
# Most of the functions in this file require the variables `MaestroApiEndPoint`, |
| 2 |
# `MaestroApiVersion` and `MaestroApiAccessToken` to be globally available. |
| 3 |
|
| 4 |
$ErrorActionPreference = 'Stop' |
| 5 |
Set-StrictMode -Version 2.0 |
| 6 |
|
| 7 |
# `tools.ps1` checks $ci to perform some actions. Since the post-build |
| 8 |
# scripts don't necessarily execute in the same agent that run the |
| 9 |
# build.ps1/sh script this variable isn't automatically set. |
| 10 |
$ci = $true |
| 11 |
$disableConfigureToolsetImport = $true |
| 12 |
. $PSScriptRoot\..\tools.ps1 |
| 13 |
|
| 14 |
function Create-MaestroApiRequestHeaders([string]$ContentType = 'application/json') { |
| 15 |
Validate-MaestroVars |
| 16 |
|
| 17 |
$headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]' |
| 18 |
$headers.Add('Accept', $ContentType) |
| 19 |
$headers.Add('Authorization',"Bearer $MaestroApiAccessToken") |
| 20 |
return $headers |
| 21 |
} |
| 22 |
|
| 23 |
function Get-MaestroChannel([int]$ChannelId) { |
| 24 |
Validate-MaestroVars |
| 25 |
|
| 26 |
$apiHeaders = Create-MaestroApiRequestHeaders |
| 27 |
$apiEndpoint = "$MaestroApiEndPoint/api/channels/${ChannelId}?api-version=$MaestroApiVersion" |
| 28 |
|
| 29 |
$result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" } |
| 30 |
return $result |
| 31 |
} |
| 32 |
|
| 33 |
function Get-MaestroBuild([int]$BuildId) { |
| 34 |
Validate-MaestroVars |
| 35 |
|
| 36 |
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 37 |
$apiEndpoint = "$MaestroApiEndPoint/api/builds/${BuildId}?api-version=$MaestroApiVersion" |
| 38 |
|
| 39 |
$result = try { return Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" } |
| 40 |
return $result |
| 41 |
} |
| 42 |
|
| 43 |
function Get-MaestroSubscriptions([string]$SourceRepository, [int]$ChannelId) { |
| 44 |
Validate-MaestroVars |
| 45 |
|
| 46 |
$SourceRepository = [System.Web.HttpUtility]::UrlEncode($SourceRepository) |
| 47 |
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 48 |
$apiEndpoint = "$MaestroApiEndPoint/api/subscriptions?sourceRepository=$SourceRepository&channelId=$ChannelId&api-version=$MaestroApiVersion" |
| 49 |
|
| 50 |
$result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" } |
| 51 |
return $result |
| 52 |
} |
| 53 |
|
| 54 |
function Assign-BuildToChannel([int]$BuildId, [int]$ChannelId) { |
| 55 |
Validate-MaestroVars |
| 56 |
|
| 57 |
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 58 |
$apiEndpoint = "$MaestroApiEndPoint/api/channels/${ChannelId}/builds/${BuildId}?api-version=$MaestroApiVersion" |
| 59 |
Invoke-WebRequest -Method Post -Uri $apiEndpoint -Headers $apiHeaders | Out-Null |
| 60 |
} |
| 61 |
|
| 62 |
function Trigger-Subscription([string]$SubscriptionId) { |
| 63 |
Validate-MaestroVars |
| 64 |
|
| 65 |
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 66 |
$apiEndpoint = "$MaestroApiEndPoint/api/subscriptions/$SubscriptionId/trigger?api-version=$MaestroApiVersion" |
| 67 |
Invoke-WebRequest -Uri $apiEndpoint -Headers $apiHeaders -Method Post | Out-Null |
| 68 |
} |
| 69 |
|
| 70 |
function Validate-MaestroVars { |
| 71 |
try { |
| 72 |
Get-Variable MaestroApiEndPoint -Scope Global | Out-Null |
| 73 |
Get-Variable MaestroApiVersion -Scope Global | Out-Null |
| 74 |
Get-Variable MaestroApiAccessToken -Scope Global | Out-Null |
| 75 |
|
| 76 |
if (!($MaestroApiEndPoint -Match '^http[s]?://maestro-(int|prod).westus2.cloudapp.azure.com$')) { |
| 77 |
Write-PipelineTelemetryError -Category 'MaestroVars' -Message "MaestroApiEndPoint is not a valid Maestro URL. '$MaestroApiEndPoint'" |
| 78 |
ExitWithExitCode 1 |
| 79 |
} |
| 80 |
|
| 81 |
if (!($MaestroApiVersion -Match '^[0-9]{4}-[0-9]{2}-[0-9]{2}$')) { |
| 82 |
Write-PipelineTelemetryError -Category 'MaestroVars' -Message "MaestroApiVersion does not match a version string in the format yyyy-MM-DD. '$MaestroApiVersion'" |
| 83 |
ExitWithExitCode 1 |
| 84 |
} |
| 85 |
} |
| 86 |
catch { |
| 87 |
Write-PipelineTelemetryError -Category 'MaestroVars' -Message 'Error: Variables `MaestroApiEndPoint`, `MaestroApiVersion` and `MaestroApiAccessToken` are required while using this script.' |
| 88 |
Write-Host $_ |
| 89 |
ExitWithExitCode 1 |
| 90 |
} |
| 91 |
} |