diff --git a/tools/devops/automation/scripts/VSTS.psm1 b/tools/devops/automation/scripts/VSTS.psm1 index 0ebebd5a7887..f7c43f276c47 100644 --- a/tools/devops/automation/scripts/VSTS.psm1 +++ b/tools/devops/automation/scripts/VSTS.psm1 @@ -332,6 +332,35 @@ class BuildConfiguration { } } + <# + .SYNOPSIS + Retrieve the change id and export it as an enviroment variable. + #> + [string] ExportChangeId ([object] $configuration) { + # This is an interesting step, we do know we are dealing with a PR, but we need the PR id to + # be able to get the labels, the buildSourceBranch follows the pattern: refs/pull/{ChangeId}/merge + # we could use a regexp but then we would have two problems instead of one + $changeId = $null + if ($configuration.PARENT_BUILD_BUILD_SOURCEBRANCH) { + # use the source branch information from the configuration object + $changeId = $configuration.PARENT_BUILD_BUILD_SOURCEBRANCH.Replace("refs/pull/", "").Replace("/merge", "") + } else { + Write-Debug "Retrieving change id from the environment since it could not be found in the config." + # retrieve the change ide form the BUILD_SOURCEBRANCH enviroment variable. + $changeId = "$Env:BUILD_SOURCEBRANCH".Replace("refs/pull/", "").Replace("/merge", "") + } + + # we can always fail (regexp error or not env varaible) + if ($changeId) { + # add a var with the change id, which can be later consumed by some of the old scripts from + # jenkins + Write-Host "##vso[task.setvariable variable=pr_number;isOutput=true]$changeId" + } else { + Write-Debug "Not setting the change id because it could not be calculated." + } + return $changeId + } + [PSCustomObject] Import([string] $configFile) { if (-not (Test-Path -Path $configFile -PathType Leaf)) { throw [System.InvalidOperationException]::new("Configuration file $configFile is missing") @@ -468,15 +497,9 @@ class BuildConfiguration { if ($configuration.BuildReason -eq "PullRequest" -or (($configuration.BuildReason -eq "Manual") -and ($configuration.PARENT_BUILD_BUILD_SOURCEBRANCH -eq "merge")) ) { Write-Host "Configuring build from PR." - # This is an interesting step, we do know we are dealing with a PR, but we need the PR id to - # be able to get the labels, the buildSourceBranch follows the pattern: refs/pull/{ChangeId}/merge - # we could use a regexp but then we would have two problems instead of one - $changeId = $configuration.PARENT_BUILD_BUILD_SOURCEBRANCH.Replace("refs/pull/", "").Replace("/merge", "") - - # add a var with the change id, which can be later consumed by some of the old scripts from - # jenkins - Write-Host "##vso[task.setvariable variable=pr_number;isOutput=true]$changeId" + # retrieve the PR data to be able to fwd the labels from github + $changeId = $this.ExportChangeId($configuration) $prInfo = Get-GitHubPRInfo -ChangeId $changeId Write-Host $prInfo