|
| 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 | +. $PSScriptRoot\..\tools.ps1 |
| 12 | + |
| 13 | +function Create-MaestroApiRequestHeaders([string]$ContentType = "application/json") { |
| 14 | + Validate-MaestroVars |
| 15 | + |
| 16 | + $headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]' |
| 17 | + $headers.Add('Accept', $ContentType) |
| 18 | + $headers.Add('Authorization',"Bearer $MaestroApiAccessToken") |
| 19 | + return $headers |
| 20 | +} |
| 21 | + |
| 22 | +function Get-MaestroChannel([int]$ChannelId) { |
| 23 | + Validate-MaestroVars |
| 24 | + |
| 25 | + $apiHeaders = Create-MaestroApiRequestHeaders |
| 26 | + $apiEndpoint = "$MaestroApiEndPoint/api/channels/${ChannelId}?api-version=$MaestroApiVersion" |
| 27 | + |
| 28 | + $result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" } |
| 29 | + return $result |
| 30 | +} |
| 31 | + |
| 32 | +function Get-MaestroBuild([int]$BuildId) { |
| 33 | + Validate-MaestroVars |
| 34 | + |
| 35 | + $apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 36 | + $apiEndpoint = "$MaestroApiEndPoint/api/builds/${BuildId}?api-version=$MaestroApiVersion" |
| 37 | + |
| 38 | + $result = try { return Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" } |
| 39 | + return $result |
| 40 | +} |
| 41 | + |
| 42 | +function Get-MaestroSubscriptions([string]$SourceRepository, [int]$ChannelId) { |
| 43 | + Validate-MaestroVars |
| 44 | + |
| 45 | + $SourceRepository = [System.Web.HttpUtility]::UrlEncode($SourceRepository) |
| 46 | + $apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 47 | + $apiEndpoint = "$MaestroApiEndPoint/api/subscriptions?sourceRepository=$SourceRepository&channelId=$ChannelId&api-version=$MaestroApiVersion" |
| 48 | + |
| 49 | + $result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" } |
| 50 | + return $result |
| 51 | +} |
| 52 | + |
| 53 | +function Trigger-Subscription([string]$SubscriptionId) { |
| 54 | + Validate-MaestroVars |
| 55 | + |
| 56 | + $apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 57 | + $apiEndpoint = "$MaestroApiEndPoint/api/subscriptions/$SubscriptionId/trigger?api-version=$MaestroApiVersion" |
| 58 | + Invoke-WebRequest -Uri $apiEndpoint -Headers $apiHeaders -Method Post | Out-Null |
| 59 | +} |
| 60 | + |
| 61 | +function Assign-BuildToChannel([int]$BuildId, [int]$ChannelId) { |
| 62 | + Validate-MaestroVars |
| 63 | + |
| 64 | + $apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken |
| 65 | + $apiEndpoint = "$MaestroApiEndPoint/api/channels/${ChannelId}/builds/${BuildId}?api-version=$MaestroApiVersion" |
| 66 | + Invoke-WebRequest -Method Post -Uri $apiEndpoint -Headers $apiHeaders | Out-Null |
| 67 | +} |
| 68 | + |
| 69 | +function Validate-MaestroVars { |
| 70 | + try { |
| 71 | + Get-Variable MaestroApiEndPoint -Scope Global | Out-Null |
| 72 | + Get-Variable MaestroApiVersion -Scope Global | Out-Null |
| 73 | + Get-Variable MaestroApiAccessToken -Scope Global | Out-Null |
| 74 | + |
| 75 | + if (!($MaestroApiEndPoint -Match "^http[s]?://maestro-(int|prod).westus2.cloudapp.azure.com$")) { |
| 76 | + Write-PipelineTaskError "MaestroApiEndPoint is not a valid Maestro URL. '$MaestroApiEndPoint'" |
| 77 | + ExitWithExitCode 1 |
| 78 | + } |
| 79 | + |
| 80 | + if (!($MaestroApiVersion -Match "^[0-9]{4}-[0-9]{2}-[0-9]{2}$")) { |
| 81 | + Write-PipelineTaskError "MaestroApiVersion does not match a version string in the format yyyy-MM-DD. '$MaestroApiVersion'" |
| 82 | + ExitWithExitCode 1 |
| 83 | + } |
| 84 | + } |
| 85 | + catch { |
| 86 | + Write-PipelineTaskError "Error: Variables `MaestroApiEndPoint`, `MaestroApiVersion` and `MaestroApiAccessToken` are required while using this script." |
| 87 | + Write-Host $_ |
| 88 | + ExitWithExitCode 1 |
| 89 | + } |
| 90 | +} |
0 commit comments