Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] Move ExportChangeID out of the configure function. #21170

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions tools/devops/automation/scripts/VSTS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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

Expand Down