Skip to content

Commit

Permalink
Reuse same branch name for previews of a PR (#8324)
Browse files Browse the repository at this point in the history
* Reuse same branch name for previews of a PR

* Default AddBuildSummary parameter using pipeline variable existence
  • Loading branch information
hallipr authored May 30, 2024
1 parent 7a9d73e commit dc42b96
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
39 changes: 31 additions & 8 deletions eng/common/scripts/Submit-PullRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ param(

[boolean]$CloseAfterOpenForTesting=$false,

[boolean]$OpenAsDraft=$false
[boolean]$OpenAsDraft=$false,

[boolean]$AddBuildSummary=($null -ne $env:SYSTEM_TEAMPROJECTID)
)

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

if ($resp.Count -gt 0) {
LogDebug "Pull request already exists $($resp[0].html_url)"
$existingPr = $resp[0]
$existingUrl = $existingPr.html_url
$existingNumber = $existingPr.number
$existingTitle = $existingPr.title
LogDebug "Pull request already exists $existingUrl"
# setting variable to reference the pull request by number
Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp[0].number)"
Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$existingNumber"
if ($AddBuildSummary) {
$summaryPath = New-TemporaryFile
$summaryMarkdown = "**PR:** [Azure/$RepoName#$existingNumber]($existingUrl)"
$summaryMarkdown += "`n**Title:** $existingTitle"
$summaryMarkdown | Out-File $summaryPath
Write-Host "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Existing Pull Request;]$summaryPath"
}
}
else {
try {
Expand All @@ -106,33 +119,43 @@ else {
-AuthToken $AuthToken

$resp | Write-Verbose
LogDebug "Pull request created https://github.com/$RepoOwner/$RepoName/pull/$($resp.number)"
$prNumber = $resp.number
$prUrl = $resp.html_url
LogDebug "Pull request created $prUrl"

$prOwnerUser = $resp.user.login

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

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

if ($cleanedUsers -or $cleanedTeamReviewers) {
Add-GitHubPullRequestReviewers -RepoOwner $RepoOwner -RepoName $RepoName -PrNumber $resp.number `
Add-GitHubPullRequestReviewers -RepoOwner $RepoOwner -RepoName $RepoName -PrNumber $prNumber `
-Users $cleanedUsers -Teams $cleanedTeamReviewers -AuthToken $AuthToken
}

if ($CloseAfterOpenForTesting) {
$prState = "closed"
LogDebug "Updating https://github.com/$RepoOwner/$RepoName/pull/$($resp.number) state to closed because this was only testing."
LogDebug "Updating $prUrl state to closed because this was only testing."
}
else {
$prState = "open"
}

Update-GitHubIssue -RepoOwner $RepoOwner -RepoName $RepoName -IssueNumber $resp.number `
Update-GitHubIssue -RepoOwner $RepoOwner -RepoName $RepoName -IssueNumber $prNumber `
-State $prState -Labels $PRLabels -Assignees $Assignees -AuthToken $AuthToken

if ($AddBuildSummary) {
$summaryPath = New-TemporaryFile
$summaryMarkdown = "**PR:** [Azure/$RepoName#$prNumber]($prUrl)"
$summaryMarkdown += "`n**Title:** $PRTitle"
$summaryMarkdown | Out-File $summaryPath
Write-Host "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Pull Request Created;]$summaryPath"
}
}
catch {
LogError "Call to GitHub API failed with exception:`n$_"
Expand Down
1 change: 1 addition & 0 deletions eng/pipelines/branch-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jobs:
arguments: >
-RepoId "${{ repo }}"
-BranchRegex "^(increment-package-version-|fluent-lite-generation-|auto-update-|version-increment-build-|post-release-automation-|restapi_auto_|t2-).*$"
-LastCommitOlderThan ((Get-Date).AddDays(-1))
-AuthToken $(azuresdk-github-pat)
-WhatIf:$${{parameters.WhatIfPreference}}
Expand Down
43 changes: 31 additions & 12 deletions eng/pipelines/templates/stages/archetype-autorest-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,26 @@ stages:
artifactPath: $(Build.ArtifactStagingDirectory)

- pwsh: |
$branchName = 'autorest-failed-build-$(Build.BuildId)'
$repositoryName = '$(repositories.self.name)'
$sourceBranch = '$(Build.SourceBranch)'
$buildReason = '$(Build.Reason)'
$buildNumber = '$(Build.BuildNumber)'
if ($buildReason -eq 'Schedule') {
$branchName = 'auto-update-autorest-scheduled'
} elseif ($sourceBranch -match "^refs/pull/(\d+)/(head|merge)$") {
$branchName = "auto-update-autorest-pr-$($Matches[1])"
} else {
$branchName = "auto-update-autorest-$buildNumber"
}
Write-Host "Setting variable 'branchName' to '$branchName'"
Write-Host "##vso[task.setvariable variable=branchName;isOutput=true]$branchName"
displayName: Set branch name
name: set_branch_name
- pwsh: |
$repositoryName = '$(repositories.self.name)'
$branchName = '$(set_branch_name.branchName)'
. $(toolsRepositoryPath)/eng/common/scripts/git-branch-push.ps1 `
-PRBranchName $branchName `
-CommitMsg 'Update repo on failing build`n`nBuild url: $(System.CollectionUri)_build/results?buildId=$(Build.BuildId)' `
Expand Down Expand Up @@ -293,10 +310,7 @@ stages:
sdkRepositoryCommitSha: $[stageDependencies.Build.Build.outputs['repositories.sdk-repository.version']]
buildArtifactsPath: $(Pipeline.Workspace)/build_artifacts
publishArtifactsPath: $(Pipeline.Workspace)/publish_artifacts
${{ if eq(variables['Build.Reason'], 'Schedule') }}:
branchName: auto-update-autorest-scheduled
${{ else }}:
branchName: auto-update-autorest-$(Build.BuildNumber)
branchName: $[stageDependencies.Build.Build.outputs['set_branch_name.branchName']]
jobs:
- job: Initialize
steps:
Expand Down Expand Up @@ -421,21 +435,26 @@ stages:
- pwsh: |
$generateJobResult = '$(generateJobResult)'
$generatorVersion = '$(generatorVersion)'
$collectionUri = '$(System.CollectionUri)'
$project = '$(System.TeamProject)'
$definitionName = '$(Build.DefinitionName)'
$repoUrl = '$(Build.Repository.Uri)'
$sourceBranch = '$(Build.SourceBranch)'
$buildNumber = '$(Build.SourceBranch)'
$queuedBy = '$(Build.SourceBranch)'
$reason = '$(Build.Reason)'
$buildId = '$(Build.BuildId)'
$buildNumber = '$(Build.BuildNumber)'
$preRelease = '${{ parameters.BuildPrereleaseVersion }}' -eq 'true'
$prBody = "Generated by $definitionName build [$buildNumber]($collectionUri/$project/_build/results?buildId=$buildId)<br/>"
if ($sourceBranch -match "^refs/heads/(.+)$") {
$prBody = "Triggered from branch: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
$prBody += "Triggered from branch: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
} elseif ($sourceBranch -match "^refs/tags/(.+)$") {
$prBody = "Triggered from tag: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
$prBody += "Triggered from tag: [$($Matches[1])]($repoUrl/tree/$sourceBranch)"
} elseif ($sourceBranch -match "^refs/pull/(\d+)/(head|merge)$") {
$prBody = "Triggered from pull request: $repoUrl/pull/$($Matches[1])"
$prBody += "Triggered from pull request: $repoUrl/pull/$($Matches[1])"
} else {
$prBody = "Triggered from [$sourceBranch]($repoUrl/tree/$sourceBranch)"
$prBody += "Triggered from [$sourceBranch]($repoUrl/tree/$sourceBranch)"
}
if ($reason -eq 'Schedule') {
Expand Down

0 comments on commit dc42b96

Please sign in to comment.