2019-07-27 12:35:24 +00:00
# Most of the functions in this file require the variables `MaestroApiEndPoint`,
# `MaestroApiVersion` and `MaestroApiAccessToken` to be globally available.
2019-11-22 13:41:58 +00:00
$ErrorActionPreference = 'Stop'
2019-07-27 12:35:24 +00:00
Set-StrictMode -Version 2.0
# `tools.ps1` checks $ci to perform some actions. Since the post-build
# scripts don't necessarily execute in the same agent that run the
# build.ps1/sh script this variable isn't automatically set.
$ci = $true
2019-11-22 13:41:58 +00:00
$disableConfigureToolsetImport = $true
2019-07-27 12:35:24 +00:00
. $PSScriptRoot \ . . \ tools . ps1
2019-11-22 13:41:58 +00:00
function Create-MaestroApiRequestHeaders([string]$ContentType = 'application/json' ) {
2019-07-27 12:35:24 +00:00
Validate-MaestroVars
$headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]'
$headers . Add ( 'Accept' , $ContentType )
$headers . Add ( 'Authorization' , " Bearer $MaestroApiAccessToken " )
return $headers
}
function Get-MaestroChannel([int]$ChannelId ) {
Validate-MaestroVars
$apiHeaders = Create-MaestroApiRequestHeaders
$apiEndpoint = " $MaestroApiEndPoint /api/channels/ ${ChannelId} ?api-version= $MaestroApiVersion "
$result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host " Error: $_ " }
return $result
}
function Get-MaestroBuild([int]$BuildId ) {
Validate-MaestroVars
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken
$apiEndpoint = " $MaestroApiEndPoint /api/builds/ ${BuildId} ?api-version= $MaestroApiVersion "
$result = try { return Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host " Error: $_ " }
return $result
}
function Get-MaestroSubscriptions([string]$SourceRepository , [ int ] $ChannelId ) {
Validate-MaestroVars
$SourceRepository = [ System.Web.HttpUtility ] :: UrlEncode ( $SourceRepository )
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken
$apiEndpoint = " $MaestroApiEndPoint /api/subscriptions?sourceRepository= $SourceRepository &channelId= $ChannelId &api-version= $MaestroApiVersion "
$result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host " Error: $_ " }
return $result
}
2019-11-22 13:41:58 +00:00
function Assign-BuildToChannel([int]$BuildId , [ int ] $ChannelId ) {
2019-07-27 12:35:24 +00:00
Validate-MaestroVars
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken
2019-11-22 13:41:58 +00:00
$apiEndpoint = " $MaestroApiEndPoint /api/channels/ ${ChannelId} /builds/ ${BuildId} ?api-version= $MaestroApiVersion "
Invoke-WebRequest -Method Post -Uri $apiEndpoint -Headers $apiHeaders | Out-Null
2019-07-27 12:35:24 +00:00
}
2019-11-22 13:41:58 +00:00
function Trigger-Subscription([string]$SubscriptionId ) {
2019-07-27 12:35:24 +00:00
Validate-MaestroVars
$apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken
2019-11-22 13:41:58 +00:00
$apiEndpoint = " $MaestroApiEndPoint /api/subscriptions/ $SubscriptionId /trigger?api-version= $MaestroApiVersion "
Invoke-WebRequest -Uri $apiEndpoint -Headers $apiHeaders -Method Post | Out-Null
2019-07-27 12:35:24 +00:00
}
function Validate-MaestroVars {
try {
Get-Variable MaestroApiEndPoint -Scope Global | Out-Null
Get-Variable MaestroApiVersion -Scope Global | Out-Null
Get-Variable MaestroApiAccessToken -Scope Global | Out-Null
2019-11-22 13:41:58 +00:00
if ( ! ( $MaestroApiEndPoint -Match '^http[s]?://maestro-(int|prod).westus2.cloudapp.azure.com$' ) ) {
Write-PipelineTelemetryError -Category 'MaestroVars' -Message " MaestroApiEndPoint is not a valid Maestro URL. ' $MaestroApiEndPoint ' "
2019-07-27 12:35:24 +00:00
ExitWithExitCode 1
}
2019-11-22 13:41:58 +00:00
if ( ! ( $MaestroApiVersion -Match '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' ) ) {
Write-PipelineTelemetryError -Category 'MaestroVars' -Message " MaestroApiVersion does not match a version string in the format yyyy-MM-DD. ' $MaestroApiVersion ' "
2019-07-27 12:35:24 +00:00
ExitWithExitCode 1
}
}
catch {
2019-11-22 13:41:58 +00:00
Write-PipelineTelemetryError -Category 'MaestroVars' -Message 'Error: Variables `MaestroApiEndPoint`, `MaestroApiVersion` and `MaestroApiAccessToken` are required while using this script.'
2019-07-27 12:35:24 +00:00
Write-Host $_
ExitWithExitCode 1
}
}