2019-07-27 12:35:24 +00:00
param (
2019-06-28 13:15:43 +00:00
[ Parameter ( Mandatory = $true ) ] [ string ] $SourceRepo ,
[ Parameter ( Mandatory = $true ) ] [ int ] $ChannelId ,
2019-07-27 12:35:24 +00:00
[ Parameter ( Mandatory = $true ) ] [ string ] $MaestroApiAccessToken ,
2019-11-22 13:41:58 +00:00
[ Parameter ( Mandatory = $false ) ] [ string ] $MaestroApiEndPoint = 'https://maestro-prod.westus2.cloudapp.azure.com' ,
[ Parameter ( Mandatory = $false ) ] [ string ] $MaestroApiVersion = '2019-01-16'
2019-06-28 13:15:43 +00:00
)
2019-11-22 13:41:58 +00:00
try {
. $PSScriptRoot \ post-build -utils . ps1
2019-06-28 13:15:43 +00:00
2019-11-22 13:41:58 +00:00
# Get all the $SourceRepo subscriptions
$normalizedSourceRepo = $SourceRepo . Replace ( 'dnceng@' , '' )
$subscriptions = Get-MaestroSubscriptions -SourceRepository $normalizedSourceRepo -ChannelId $ChannelId
2019-06-28 13:15:43 +00:00
2019-11-22 13:41:58 +00:00
if ( ! $subscriptions ) {
Write-PipelineTelemetryError -Category 'TriggerSubscriptions' -Message " No subscriptions found for source repo ' $normalizedSourceRepo ' in channel ' $ChannelId ' "
ExitWithExitCode 0
}
2019-06-28 13:15:43 +00:00
2019-11-22 13:41:58 +00:00
$subscriptionsToTrigger = New-Object System . Collections . Generic . List [ string ]
$failedTriggeredSubscription = $false
2019-06-28 13:15:43 +00:00
2019-11-22 13:41:58 +00:00
# Get all enabled subscriptions that need dependency flow on 'everyBuild'
foreach ( $subscription in $subscriptions ) {
if ( $subscription . enabled -and $subscription . policy . updateFrequency -like 'everyBuild' -and $subscription . channel . id -eq $ChannelId ) {
Write-Host " Should trigger this subscription: $ { $subscription .id} "
[ void ] $subscriptionsToTrigger . Add ( $subscription . id )
}
2019-06-28 13:15:43 +00:00
}
2019-11-22 13:41:58 +00:00
foreach ( $subscriptionToTrigger in $subscriptionsToTrigger ) {
try {
Write-Host " Triggering subscription ' $subscriptionToTrigger '. "
Trigger-Subscription -SubscriptionId $subscriptionToTrigger
Write-Host 'done.'
}
catch
{
Write-Host " There was an error while triggering subscription ' $subscriptionToTrigger ' "
Write-Host $_
Write-Host $_ . ScriptStackTrace
$failedTriggeredSubscription = $true
}
2019-06-28 13:15:43 +00:00
}
2019-11-22 13:41:58 +00:00
if ( $subscriptionsToTrigger . Count -eq 0 ) {
Write-Host " No subscription matched source repo ' $normalizedSourceRepo ' and channel ID ' $ChannelId '. "
}
elseif ( $failedTriggeredSubscription ) {
Write-PipelineTelemetryError -Category 'TriggerSubscriptions' -Message 'At least one subscription failed to be triggered...'
ExitWithExitCode 1
}
else {
Write-Host 'All subscriptions were triggered successfully!'
}
2019-07-27 12:35:24 +00:00
}
2019-11-22 13:41:58 +00:00
catch {
Write-Host $_ . ScriptStackTrace
Write-PipelineTelemetryError -Category 'TriggerSubscriptions' -Message $_
2019-06-28 13:15:43 +00:00
ExitWithExitCode 1
}