From 13e78efdb3b7472ccc5a5c1d79c287fc863edeff Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Tue, 3 Sep 2024 11:48:10 -0400 Subject: [PATCH 1/2] [CI] Move ExportChangeID out of the configure function. We are tryihng to sort out all the scripts to make it re-usable in order to use them in cascading pipelines. --- tools/devops/automation/scripts/VSTS.psm1 | 39 ++++++++++++++++++----- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/tools/devops/automation/scripts/VSTS.psm1 b/tools/devops/automation/scripts/VSTS.psm1 index 0ebebd5a7887..9cbc312ac3b5 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 configuraiton object + $changeId = $configuration.PARENT_BUILD_BUILD_SOURCEBRANCH.Replace("refs/pull/", "").Replace("/merge", "") + } else { + Write-Debug "Retrieving change id from the enviroment 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 From ec2dd4ad719672dcf9f0cfcb5147d7b727a041cc Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Tue, 3 Sep 2024 16:27:09 -0400 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Rolf Bjarne Kvinge --- tools/devops/automation/scripts/VSTS.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/devops/automation/scripts/VSTS.psm1 b/tools/devops/automation/scripts/VSTS.psm1 index 9cbc312ac3b5..f7c43f276c47 100644 --- a/tools/devops/automation/scripts/VSTS.psm1 +++ b/tools/devops/automation/scripts/VSTS.psm1 @@ -342,10 +342,10 @@ class BuildConfiguration { # 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 configuraiton object + # 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 enviroment since it could not be found in the config." + 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", "") }