[master] Update dependencies from dotnet/arcade dotnet/core-setup dotnet/corefx ...
[mono-project.git] / eng / common / post-build / post-build-utils.ps1
blobaa9a025c0bbd848eaadad2d7d2e55c945baa403a
1 # Most of the functions in this file require the variables `MaestroApiEndPoint`,
2 # `MaestroApiVersion` and `MaestroApiAccessToken` to be globally available.
4 $ErrorActionPreference = "Stop"
5 Set-StrictMode -Version 2.0
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
14 function Create-MaestroApiRequestHeaders([string]$ContentType = "application/json") {
15 Validate-MaestroVars
17 $headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]'
18 $headers.Add('Accept', $ContentType)
19 $headers.Add('Authorization',"Bearer $MaestroApiAccessToken")
20 return $headers
23 function Get-MaestroChannel([int]$ChannelId) {
24 Validate-MaestroVars
26 $apiHeaders = Create-MaestroApiRequestHeaders
27 $apiEndpoint = "$MaestroApiEndPoint/api/channels/${ChannelId}?api-version=$MaestroApiVersion"
29 $result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" }
30 return $result
33 function Get-MaestroBuild([int]$BuildId) {
34 Validate-MaestroVars
36 $apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken
37 $apiEndpoint = "$MaestroApiEndPoint/api/builds/${BuildId}?api-version=$MaestroApiVersion"
39 $result = try { return Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" }
40 return $result
43 function Get-MaestroSubscriptions([string]$SourceRepository, [int]$ChannelId) {
44 Validate-MaestroVars
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"
50 $result = try { Invoke-WebRequest -Method Get -Uri $apiEndpoint -Headers $apiHeaders | ConvertFrom-Json } catch { Write-Host "Error: $_" }
51 return $result
54 function Trigger-Subscription([string]$SubscriptionId) {
55 Validate-MaestroVars
57 $apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken
58 $apiEndpoint = "$MaestroApiEndPoint/api/subscriptions/$SubscriptionId/trigger?api-version=$MaestroApiVersion"
59 Invoke-WebRequest -Uri $apiEndpoint -Headers $apiHeaders -Method Post | Out-Null
62 function Assign-BuildToChannel([int]$BuildId, [int]$ChannelId) {
63 Validate-MaestroVars
65 $apiHeaders = Create-MaestroApiRequestHeaders -AuthToken $MaestroApiAccessToken
66 $apiEndpoint = "$MaestroApiEndPoint/api/channels/${ChannelId}/builds/${BuildId}?api-version=$MaestroApiVersion"
67 Invoke-WebRequest -Method Post -Uri $apiEndpoint -Headers $apiHeaders | Out-Null
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
76 if (!($MaestroApiEndPoint -Match "^http[s]?://maestro-(int|prod).westus2.cloudapp.azure.com$")) {
77 Write-PipelineTaskError "MaestroApiEndPoint is not a valid Maestro URL. '$MaestroApiEndPoint'"
78 ExitWithExitCode 1
81 if (!($MaestroApiVersion -Match "^[0-9]{4}-[0-9]{2}-[0-9]{2}$")) {
82 Write-PipelineTaskError "MaestroApiVersion does not match a version string in the format yyyy-MM-DD. '$MaestroApiVersion'"
83 ExitWithExitCode 1
86 catch {
87 Write-PipelineTaskError "Error: Variables `MaestroApiEndPoint`, `MaestroApiVersion` and `MaestroApiAccessToken` are required while using this script."
88 Write-Host $_
89 ExitWithExitCode 1