Skip to content

Commit dc42b96

Browse files
authored
Reuse same branch name for previews of a PR (#8324)
* Reuse same branch name for previews of a PR * Default AddBuildSummary parameter using pipeline variable existence
1 parent 7a9d73e commit dc42b96

3 files changed

Lines changed: 63 additions & 20 deletions

File tree

eng/common/scripts/Submit-PullRequest.ps1

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ param(
7171

7272
[boolean]$CloseAfterOpenForTesting=$false,
7373

74-
[boolean]$OpenAsDraft=$false
74+
[boolean]$OpenAsDraft=$false,
75+
76+
[boolean]$AddBuildSummary=($null -ne $env:SYSTEM_TEAMPROJECTID)
7577
)
7678

7779
. (Join-Path $PSScriptRoot common.ps1)
@@ -88,9 +90,20 @@ catch {
8890
$resp | Write-Verbose
8991

9092
if ($resp.Count -gt 0) {
91-
LogDebug "Pull request already exists $($resp[0].html_url)"
93+
$existingPr = $resp[0]
94+
$existingUrl = $existingPr.html_url
95+
$existingNumber = $existingPr.number
96+
$existingTitle = $existingPr.title
97+
LogDebug "Pull request already exists $existingUrl"
9298
# setting variable to reference the pull request by number
93-
Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp[0].number)"
99+
Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$existingNumber"
100+
if ($AddBuildSummary) {
101+
$summaryPath = New-TemporaryFile
102+
$summaryMarkdown = "**PR:** [Azure/$RepoName#$existingNumber]($existingUrl)"
103+
$summaryMarkdown += "`n**Title:** $existingTitle"
104+
$summaryMarkdown | Out-File $summaryPath
105+
Write-Host "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Existing Pull Request;]$summaryPath"
106+
}
94107
}
95108
else {
96109
try {
@@ -106,33 +119,43 @@ else {
106119
-AuthToken $AuthToken
107120

108121
$resp | Write-Verbose
109-
LogDebug "Pull request created https://github.com/$RepoOwner/$RepoName/pull/$($resp.number)"
122+
$prNumber = $resp.number
123+
$prUrl = $resp.html_url
124+
LogDebug "Pull request created $prUrl"
110125

111126
$prOwnerUser = $resp.user.login
112127

113128
# setting variable to reference the pull request by number
114-
Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp.number)"
129+
Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$prNumber"
115130

116131
# ensure that the user that was used to create the PR is not attempted to add as a reviewer
117132
# we cast to an array to ensure that length-1 arrays actually stay as array values
118133
$cleanedUsers = @(SplitParameterArray -members $UserReviewers) | ? { $_ -ne $prOwnerUser -and $null -ne $_ }
119134
$cleanedTeamReviewers = @(SplitParameterArray -members $TeamReviewers) | ? { $_ -ne $prOwnerUser -and $null -ne $_ }
120135

121136
if ($cleanedUsers -or $cleanedTeamReviewers) {
122-
Add-GitHubPullRequestReviewers -RepoOwner $RepoOwner -RepoName $RepoName -PrNumber $resp.number `
137+
Add-GitHubPullRequestReviewers -RepoOwner $RepoOwner -RepoName $RepoName -PrNumber $prNumber `
123138
-Users $cleanedUsers -Teams $cleanedTeamReviewers -AuthToken $AuthToken
124139
}
125140

126141
if ($CloseAfterOpenForTesting) {
127142
$prState = "closed"
128-
LogDebug "Updating https://github.com/$RepoOwner/$RepoName/pull/$($resp.number) state to closed because this was only testing."
143+
LogDebug "Updating $prUrl state to closed because this was only testing."
129144
}
130145
else {
131146
$prState = "open"
132147
}
133148

134-
Update-GitHubIssue -RepoOwner $RepoOwner -RepoName $RepoName -IssueNumber $resp.number `
149+
Update-GitHubIssue -RepoOwner $RepoOwner -RepoName $RepoName -IssueNumber $prNumber `
135150
-State $prState -Labels $PRLabels -Assignees $Assignees -AuthToken $AuthToken
151+
152+
if ($AddBuildSummary) {
153+
$summaryPath = New-TemporaryFile
154+
$summaryMarkdown = "**PR:** [Azure/$RepoName#$prNumber]($prUrl)"
155+
$summaryMarkdown += "`n**Title:** $PRTitle"
156+
$summaryMarkdown | Out-File $summaryPath
157+
Write-Host "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Pull Request Created;]$summaryPath"
158+
}
136159
}
137160
catch {
138161
LogError "Call to GitHub API failed with exception:`n$_"

eng/pipelines/branch-cleanup.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ jobs:
109109
arguments: >
110110
-RepoId "${{ repo }}"
111111
-BranchRegex "^(increment-package-version-|fluent-lite-generation-|auto-update-|version-increment-build-|post-release-automation-|restapi_auto_|t2-).*$"
112+
-LastCommitOlderThan ((Get-Date).AddDays(-1))
112113
-AuthToken $(azuresdk-github-pat)
113114
-WhatIf:$${{parameters.WhatIfPreference}}
114115

eng/pipelines/templates/stages/archetype-autorest-preview.yml

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,26 @@ stages:
136136
artifactPath: $(Build.ArtifactStagingDirectory)
137137

138138
- pwsh: |
139-
$branchName = 'autorest-failed-build-$(Build.BuildId)'
140-
$repositoryName = '$(repositories.self.name)'
139+
$sourceBranch = '$(Build.SourceBranch)'
140+
$buildReason = '$(Build.Reason)'
141+
$buildNumber = '$(Build.BuildNumber)'
142+
143+
if ($buildReason -eq 'Schedule') {
144+
$branchName = 'auto-update-autorest-scheduled'
145+
} elseif ($sourceBranch -match "^refs/pull/(\d+)/(head|merge)$") {
146+
$branchName = "auto-update-autorest-pr-$($Matches[1])"
147+
} else {
148+
$branchName = "auto-update-autorest-$buildNumber"
149+
}
150+
151+
Write-Host "Setting variable 'branchName' to '$branchName'"
152+
Write-Host "##vso[task.setvariable variable=branchName;isOutput=true]$branchName"
153+
displayName: Set branch name
154+
name: set_branch_name
141155
156+
- pwsh: |
157+
$repositoryName = '$(repositories.self.name)'
158+
$branchName = '$(set_branch_name.branchName)'
142159
. $(toolsRepositoryPath)/eng/common/scripts/git-branch-push.ps1 `
143160
-PRBranchName $branchName `
144161
-CommitMsg 'Update repo on failing build`n`nBuild url: $(System.CollectionUri)_build/results?buildId=$(Build.BuildId)' `
@@ -293,10 +310,7 @@ stages:
293310
sdkRepositoryCommitSha: $[stageDependencies.Build.Build.outputs['repositories.sdk-repository.version']]
294311
buildArtifactsPath: $(Pipeline.Workspace)/build_artifacts
295312
publishArtifactsPath: $(Pipeline.Workspace)/publish_artifacts
296-
${{ if eq(variables['Build.Reason'], 'Schedule') }}:
297-
branchName: auto-update-autorest-scheduled
298-
${{ else }}:
299-
branchName: auto-update-autorest-$(Build.BuildNumber)
313+
branchName: $[stageDependencies.Build.Build.outputs['set_branch_name.branchName']]
300314
jobs:
301315
- job: Initialize
302316
steps:
@@ -421,21 +435,26 @@ stages:
421435
- pwsh: |
422436
$generateJobResult = '$(generateJobResult)'
423437
$generatorVersion = '$(generatorVersion)'
438+
$collectionUri = '$(System.CollectionUri)'
439+
$project = '$(System.TeamProject)'
440+
$definitionName = '$(Build.DefinitionName)'
424441
$repoUrl = '$(Build.Repository.Uri)'
425442
$sourceBranch = '$(Build.SourceBranch)'
426-
$buildNumber = '$(Build.SourceBranch)'
427-
$queuedBy = '$(Build.SourceBranch)'
428443
$reason = '$(Build.Reason)'
444+
$buildId = '$(Build.BuildId)'
445+
$buildNumber = '$(Build.BuildNumber)'
429446
$preRelease = '${{ parameters.BuildPrereleaseVersion }}' -eq 'true'
447+
448+
$prBody = "Generated by $definitionName build [$buildNumber]($collectionUri/$project/_build/results?buildId=$buildId)<br/>"
430449
431450
if ($sourceBranch -match "^refs/heads/(.+)$") {
432-
$prBody = "Triggered from branch: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
451+
$prBody += "Triggered from branch: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
433452
} elseif ($sourceBranch -match "^refs/tags/(.+)$") {
434-
$prBody = "Triggered from tag: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
453+
$prBody += "Triggered from tag: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
435454
} elseif ($sourceBranch -match "^refs/pull/(\d+)/(head|merge)$") {
436-
$prBody = "Triggered from pull request: $repoUrl/pull/$($Matches[1])"
455+
$prBody += "Triggered from pull request: $repoUrl/pull/$($Matches[1])"
437456
} else {
438-
$prBody = "Triggered from [$sourceBranch]($repoUrl/tree/$sourceBranch)"
457+
$prBody += "Triggered from [$sourceBranch]($repoUrl/tree/$sourceBranch)"
439458
}
440459
441460
if ($reason -eq 'Schedule') {

0 commit comments

Comments
 (0)