diff --git a/eng/common/pipelines/templates/steps/create-pull-request.yml b/eng/common/pipelines/templates/steps/create-pull-request.yml index 3ccd4c956..b22d5dda2 100644 --- a/eng/common/pipelines/templates/steps/create-pull-request.yml +++ b/eng/common/pipelines/templates/steps/create-pull-request.yml @@ -11,7 +11,7 @@ parameters: PushArgs: WorkingDirectory: $(System.DefaultWorkingDirectory) PRTitle: not-specified - PRBody: not-specified + PRBody: '' ScriptDirectory: eng/common/scripts GHReviewersVariable: '' GHTeamReviewersVariable: '' @@ -68,12 +68,13 @@ steps: -PRBranch "${{ parameters.PRBranchName }}" -AuthToken "$(azuresdk-github-pat)" -PRTitle "${{ parameters.PRTitle }}" + -PRBody "${{ coalesce(parameters.PRBody, parameters.CommitMsg, parameters.PRTitle) }}" -PRLabels "${{ parameters.PRLabels}}" - -PRBody "${{ parameters.PRBody }}" - task: PowerShell@2 displayName: Tag a Reviewer on PR condition: and(succeeded(), eq(variables['HasChanges'], 'true')) + continueOnError: true inputs: pwsh: true workingDirectory: ${{ parameters.WorkingDirectory }} diff --git a/eng/common/pipelines/templates/steps/get-pr-owners.yml b/eng/common/pipelines/templates/steps/get-pr-owners.yml index a80d5b83b..e3739602b 100644 --- a/eng/common/pipelines/templates/steps/get-pr-owners.yml +++ b/eng/common/pipelines/templates/steps/get-pr-owners.yml @@ -18,12 +18,20 @@ steps: --kusto-database-var KUSTO_DB ` --kusto-table-var KUSTO_TABLE ` --identity "$(Build.QueuedBy)" - $resolvedIdentity = $result[-1] | ConvertFrom-Json - Write-Host $resolvedIdentity + $resolvedIdentity = "" + try { $resolvedIdentity = $result[-1] | ConvertFrom-Json } catch {} - Write-Output "##vso[task.setvariable variable=${{ parameters.TargetVariable }}]$($resolvedIdentity.GithubUserName)" + if($resolvedIdentity) { + Write-Host $resolvedIdentity + + Write-Host "##vso[task.setvariable variable=${{ parameters.TargetVariable }}]$($resolvedIdentity.GithubUserName)" + } + else { + Write-Host "Unable to locate a github user for identity $(Build.QueuedBy)" + } displayName: 'Resolving Queuing User' + continueOnError: true workingDirectory: $(Build.SourcesDirectory)/tools_repo/tools/notification-configuration/identity-resolver env: APP_ID: $(notification-aad-app-id) @@ -41,6 +49,6 @@ steps: $originalValue = "$(${{ parameters.TargetVariable }})" $result = $(Build.SourcesDirectory)/eng/common/scripts/get-codeowners.ps1 -TargetDirectory /sdk/${{ parameters.ServiceDirectory }}/ -RootDirectory $(Build.SourcesDirectory) if ($result) { - Write-Output "##vso[task.setvariable variable=${{ parameters.TargetVariable }}]$originalValue,$result" + Write-Host "##vso[task.setvariable variable=${{ parameters.TargetVariable }}]$originalValue,$result" } displayName: Add CodeOwners if Present \ No newline at end of file diff --git a/eng/common/scripts/Submit-PullRequest.ps1 b/eng/common/scripts/Submit-PullRequest.ps1 index 68a4db9c2..7f3f0a544 100644 --- a/eng/common/scripts/Submit-PullRequest.ps1 +++ b/eng/common/scripts/Submit-PullRequest.ps1 @@ -17,6 +17,8 @@ The branch which we want to create a pull request for. A personal access token .PARAMETER PRTitle The title of the pull request. +.PARAMETER PRBody +The body message for the pull request. .PARAMETER PRLabels The labels added to the PRs. Multple labels seperated by comma, e.g "bug, service" #> @@ -42,7 +44,9 @@ param( [Parameter(Mandatory = $true)] [string]$PRTitle, - $PRBody = $PRTitle, + + [Parameter(Mandatory = $false)] + [string]$PRBody = $PRTitle, [Parameter(Mandatory = $false)] [string]$PRLabels diff --git a/eng/common/scripts/add-pullrequest-reviewers.ps1 b/eng/common/scripts/add-pullrequest-reviewers.ps1 index a80d79485..3198dcb40 100644 --- a/eng/common/scripts/add-pullrequest-reviewers.ps1 +++ b/eng/common/scripts/add-pullrequest-reviewers.ps1 @@ -18,6 +18,32 @@ param( $AuthToken ) +function AddMembers($memberName, $additionSet) { + $headers = @{ + Authorization = "bearer $AuthToken" + } + $uri = "https://api.github.com/repos/$RepoOwner/$RepoName/pulls/$PRNumber/requested_reviewers" + $errorOccurred = $false + + foreach ($id in $additionSet) { + try { + $postResp = @{} + $postResp[$memberName] = @($id) + $postResp = $postResp | ConvertTo-Json + + Write-Host $postResp + $resp = Invoke-RestMethod -Method Post -Headers $headers -Body $postResp -Uri $uri -MaximumRetryCount 3 + $resp | Write-Verbose + } + catch { + Write-Host "Error attempting to add $user `n$_" + $errorOccurred = $true + } + } + + return $errorOccurred +} + # at least one of these needs to be populated if (-not $GitHubUsers -and -not $GitHubTeams) { Write-Host "No user provided for addition, exiting." @@ -27,54 +53,9 @@ if (-not $GitHubUsers -and -not $GitHubTeams) { $userAdditions = @($GitHubUsers.Split(",") | % { $_.Trim() } | ? { return $_ }) $teamAdditions = @($GitHubTeams.Split(",") | % { $_.Trim() } | ? { return $_ }) -$headers = @{ - Authorization = "bearer $AuthToken" -} -$uri = "https://api.github.com/repos/$RepoOwner/$RepoName/pulls/$PRNumber/requested_reviewers" +$errorsOccurredAddingUsers = AddMembers -memberName "reviewers" -additionSet $userAdditions +$errorsOccurredAddingTeams = AddMembers -memberName "team_reviewers" -additionSet $teamAdditions -try { - $resp = Invoke-RestMethod -Headers $headers $uri -MaximumRetryCount 3 -} -catch { - Write-Error "Invoke-RestMethod [$uri] failed with exception:`n$_" +if ($errorsOccurredAddingUsers -or $errorsOccurredAddingTeams) { exit 1 } - -# the response object takes this form: https://developer.github.com/v3/pulls/review_requests/#response-1 -# before we can push a new reviewer, we need to pull the simple Ids out of the complex objects that came back in the response -$userReviewers = @($resp.users | % { return $_.login }) -$teamReviewers = @($resp.teams | % { return $_.slug }) - -if (!$userReviewers) { $modifiedUserReviewers = @() } else { $modifiedUserReviewers = $userReviewers.Clone() } -$modifiedUserReviewers += ($userAdditions | ? { !$modifiedUserReviewers.Contains($_) }) - -if ($teamReviewers) { $modifiedTeamReviewers = @() } else { $modifiedTeamReviewers = $teamReviewers.Clone() } -$modifiedTeamReviewers += ($teamAdditions | ? { !$modifiedTeamReviewers.Contains($_) }) - -$detectedUserDiffs = Compare-Object -ReferenceObject $userReviewers -DifferenceObject $modifiedUserReviewers -$detectedTeamDiffs = Compare-Object -ReferenceObject $teamReviewers -DifferenceObject $modifiedTeamReviewers - -# Compare-Object returns values when there is a difference between the comparied objects. -# we only want to run the update if there IS a difference. -if ($detectedUserDiffs -or $detectedTeamDiffs) { - $postResp = @{} - - if ($modifiedUserReviewers) { $postResp["reviewers"] = $modifiedUserReviewers } - if ($modifiedTeamReviewers) { $postResp["team_reviewers"] = $modifiedTeamReviewers } - - $postResp = $postResp | ConvertTo-Json - - try { - Write-Host $postResp - $resp = Invoke-RestMethod -Method Post -Headers $headers -Body $postResp -Uri $uri -MaximumRetryCount 3 - $resp | Write-Verbose - } - catch { - Write-Error "Unable to update PR reviewers. `n$_" - } -} -else { - $results = $GitHubUsers + $GitHubTeams - Write-Host "Reviewers $results already added. Exiting." - exit(0) -}