diff --git a/eng/common/pipelines/templates/steps/create-pull-request.yml b/eng/common/pipelines/templates/steps/create-pull-request.yml index 836ad4b3604b..a60d26c8fe98 100644 --- a/eng/common/pipelines/templates/steps/create-pull-request.yml +++ b/eng/common/pipelines/templates/steps/create-pull-request.yml @@ -15,7 +15,6 @@ parameters: ScriptDirectory: eng/common/scripts GHReviewersVariable: '' GHTeamReviewersVariable: '' - GHAssignessVariable: '' # Multiple labels seperated by comma, e.g. "bug, APIView" PRLabels: '' @@ -76,6 +75,19 @@ steps: -PRTitle "${{ parameters.PRTitle }}" -PRBody "${{ coalesce(parameters.PRBody, parameters.CommitMsg, parameters.PRTitle) }}" -PRLabels "${{ parameters.PRLabels}}" - -UserReviewers "$(${{ parameters.GHReviewersVariable }})" - -TeamReviewers "$(${{ parameters.GHTeamReviewersVariable }})" - -Assignees "$(${{ parameters.GHAssignessVariable }})" \ No newline at end of file + +- task: PowerShell@2 + displayName: Tag a Reviewer on PR + condition: and(succeeded(), eq(variables['HasChanges'], 'true')) + continueOnError: true + inputs: + pwsh: true + workingDirectory: ${{ parameters.WorkingDirectory }} + filePath: ${{ parameters.ScriptDirectory }}/add-pullrequest-reviewers.ps1 + arguments: > + -RepoOwner "${{ parameters.RepoOwner }}" + -RepoName "$(RepoNameWithoutOwner)" + -AuthToken "$(azuresdk-github-pat)" + -GitHubUsers "$(${{ parameters.GHReviewersVariable }})" + -GitHubTeams "$(${{ parameters.GHTeamReviewersVariable }})" + -PRNumber "$(Submitted.PullRequest.Number)" diff --git a/eng/common/scripts/Submit-PullRequest.ps1 b/eng/common/scripts/Submit-PullRequest.ps1 index c5eb19b3ca45..7f3f0a544e9a 100644 --- a/eng/common/scripts/Submit-PullRequest.ps1 +++ b/eng/common/scripts/Submit-PullRequest.ps1 @@ -49,63 +49,39 @@ param( [string]$PRBody = $PRTitle, [Parameter(Mandatory = $false)] - [string]$PRLabels, - - [Parameter(Mandatory = $false)] - [string]$UserReviewers, - - [Parameter(Mandatory = $false)] - [string]$TeamReviewers, - - [Parameter(Mandatory = $false)] - [string]$Assignees + [string]$PRLabels ) $headers = @{ Authorization = "bearer $AuthToken" } -$baseURI = "https://api.github.com/repos" -function SplitMembers ($membersString) -{ - return @($membersString.Split(",") | % { $_.Trim() } | ? { return $_ }) -} -function InvokeGitHubAPI($apiURI, $method, $body) { - $resp = Invoke-RestMethod -Method $method -Headers $headers -Body ($body | ConvertTo-Json) -Uri $apiURI -MaximumRetryCount 3 - Write-Host -f green "These members have been added to: https://github.com/$RepoOwner/$RepoName/pull/$prNumber" - ($body | Format-List | Write-Output) - $resp | Write-Verbose -} +$query = "state=open&head=${PROwner}:${PRBranch}&base=${BaseBranch}" -function AddReviewers ($prNumber, $users, $teams) { - $uri = "$baseURI/$RepoOwner/$RepoName/pulls/$prNumber/requested_reviewers" - $userAdditions = SplitMembers -membersString $users - $teamAdditions = SplitMembers -membersString $teams - $postResp = @{} - if ($userAdditions) { - $postResp["reviewers"] = @($userAdditions) - } - if ($teamAdditions) { - $postResp["team_reviewers"] = @($teamAdditions) +function AddLabels([int] $prNumber, [string] $prLabelString) +{ + # Adding labels to the pr. + if (-not $prLabelString) { + Write-Verbose "There are no labels added to the PR." + return } - return InvokeGitHubAPI -apiURI $uri -method 'Post' -body $postResp -} -function AddLabelsAndOrAssignees ($prNumber, $labels, $assignees) { - $uri = "$baseURI/$RepoOwner/$RepoName/issues/$prNumber" - $labelAdditions = SplitMembers -membersString $labels - $assigneeAdditions = SplitMembers -membersString $assignees - $postResp = @{} - if ($assigneeAdditions) { - $postResp["assignees"] = @($assigneeAdditions) + # Parse the labels from string to array + $prLabelArray = @($prLabelString.Split(",") | % { $_.Trim() } | ? { return $_ }) + $prLabelUri = "https://api.github.com/repos/$RepoOwner/$RepoName/issues/$prNumber" + $labelRequestData = @{ + labels = $prLabelArray } - if ($labelAdditions) { - $postResp["labels"] = @($labelAdditions) + try { + $resp = Invoke-RestMethod -Method PATCH -Headers $headers $prLabelUri -Body ($labelRequestData | ConvertTo-Json) + } + catch { + Write-Error "Invoke-RestMethod $prLabelUri failed with exception:`n$_" } - return InvokeGitHubAPI -apiURI $uri -method 'Post' -body $postResp -} -$query = "state=open&head=${PROwner}:${PRBranch}&base=${BaseBranch}" + $resp | Write-Verbose + Write-Host -f green "Label(s) [$prLabelArray] added to pull request: https://github.com/$RepoOwner/$RepoName/pull/$prNumber" +} try { $resp = Invoke-RestMethod -Headers $headers "https://api.github.com/repos/$RepoOwner/$RepoName/pulls?$query" @@ -117,18 +93,11 @@ catch { $resp | Write-Verbose if ($resp.Count -gt 0) { - try { Write-Host -f green "Pull request already exists $($resp[0].html_url)" # setting variable to reference the pull request by number Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp[0].number)" - AddReviewers -prNumber $resp[0].number -users $UserReviewers -teams $TeamReviewers - AddLabelsAndOrAssignees -prNumber $resp[0].number -labels $PRLabels -assignees $Assignees - } - catch { - Write-Error "Call to GitHub API failed with exception:`n$_" - exit 1 - } + AddLabels $resp[0].number $PRLabels } else { $data = @{ @@ -143,17 +112,17 @@ else { $resp = Invoke-RestMethod -Method POST -Headers $headers ` "https://api.github.com/repos/$RepoOwner/$RepoName/pulls" ` -Body ($data | ConvertTo-Json) - - $resp | Write-Verbose - Write-Host -f green "Pull request created https://github.com/$RepoOwner/$RepoName/pull/$($resp.number)" - - # setting variable to reference the pull request by number - Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp.number)" - AddReviewers -prNumber $resp.number -users $UserReviewers -teams $TeamReviewers - AddLabelsAndOrAssignees -prNumber $resp.number -labels $PRLabels -assignees $Assignees } catch { - Write-Error "Call to GitHub API failed with exception:`n$_" + Write-Error "Invoke-RestMethod [https://api.github.com/repos/$RepoOwner/$RepoName/pulls] failed with exception:`n$_" exit 1 } -} \ No newline at end of file + + $resp | Write-Verbose + Write-Host -f green "Pull request created https://github.com/$RepoOwner/$RepoName/pull/$($resp.number)" + + # setting variable to reference the pull request by number + Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp.number)" + + AddLabels $resp.number $PRLabels +} diff --git a/eng/common/scripts/add-pullrequest-reviewers.ps1 b/eng/common/scripts/add-pullrequest-reviewers.ps1 new file mode 100644 index 000000000000..3198dcb40d2c --- /dev/null +++ b/eng/common/scripts/add-pullrequest-reviewers.ps1 @@ -0,0 +1,61 @@ +param( + [Parameter(Mandatory = $true)] + $RepoOwner, + + [Parameter(Mandatory = $true)] + $RepoName, + + [Parameter(Mandatory = $false)] + $GitHubUsers = "", + + [Parameter(Mandatory = $false)] + $GitHubTeams = "", + + [Parameter(Mandatory = $true)] + $PRNumber, + + [Parameter(Mandatory = $true)] + $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." + exit 0 +} + +$userAdditions = @($GitHubUsers.Split(",") | % { $_.Trim() } | ? { return $_ }) +$teamAdditions = @($GitHubTeams.Split(",") | % { $_.Trim() } | ? { return $_ }) + +$errorsOccurredAddingUsers = AddMembers -memberName "reviewers" -additionSet $userAdditions +$errorsOccurredAddingTeams = AddMembers -memberName "team_reviewers" -additionSet $teamAdditions + +if ($errorsOccurredAddingUsers -or $errorsOccurredAddingTeams) { + exit 1 +} diff --git a/eng/common/scripts/get-pr-creator.ps1 b/eng/common/scripts/get-pr-creator.ps1 deleted file mode 100644 index 23a1f28c6e21..000000000000 --- a/eng/common/scripts/get-pr-creator.ps1 +++ /dev/null @@ -1,37 +0,0 @@ -param ( - [Parameter(Mandatory = $true)] - [string]$RepoOwner, - - [Parameter(Mandatory = $true)] - [string]$RepoName, - - [Parameter(Mandatory = $true)] - $PullRequestNumber, - - [Parameter(Mandatory = $true)] - $VsoPRCreatorVariable, - - [Parameter(Mandatory = $false)] - $AuthToken -) - -$headers = @{ } - -if ($AuthToken) { - $headers = @{ - Authorization = "bearer $AuthToken" - } -} - -try -{ - $prApiUrl = "https://api.github.com/repos/$RepoOwner/$RepoName/pulls/${PullRequestNumber}" - $response = Invoke-RestMethod -Headers $headers $prApiUrl - Write-Host "##vso[task.setvariable variable=$VsoPRCreatorVariable;]$($response.user.login)" -} -catch -{ - Write-Error "Invoke-RestMethod ${prApiUrl} failed with exception:`n$_" - exit 1 -} -