diff --git a/eng/common/pipelines/templates/steps/create-pull-request.yml b/eng/common/pipelines/templates/steps/create-pull-request.yml index a60d26c8fe98..e006999d44f1 100644 --- a/eng/common/pipelines/templates/steps/create-pull-request.yml +++ b/eng/common/pipelines/templates/steps/create-pull-request.yml @@ -17,6 +17,7 @@ parameters: GHTeamReviewersVariable: '' # Multiple labels seperated by comma, e.g. "bug, APIView" PRLabels: '' + GHAssignessVariable: '' steps: @@ -75,19 +76,6 @@ steps: -PRTitle "${{ parameters.PRTitle }}" -PRBody "${{ coalesce(parameters.PRBody, parameters.CommitMsg, parameters.PRTitle) }}" -PRLabels "${{ parameters.PRLabels}}" - -- 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)" + -UserReviewers "$(${{ parameters.GHReviewersVariable }})" + -TeamReviewers "$(${{ parameters.GHTeamReviewersVariable }})" + -Assignees "$(${{ parameters.GHAssignessVariable }})" \ No newline at end of file diff --git a/eng/common/scripts/Submit-PullRequest.ps1 b/eng/common/scripts/Submit-PullRequest.ps1 index 7f3f0a544e9a..2588b3b7297c 100644 --- a/eng/common/scripts/Submit-PullRequest.ps1 +++ b/eng/common/scripts/Submit-PullRequest.ps1 @@ -49,40 +49,90 @@ param( [string]$PRBody = $PRTitle, [Parameter(Mandatory = $false)] - [string]$PRLabels + [string]$PRLabels, + + [Parameter(Mandatory = $false)] + [string]$UserReviewers, + + [Parameter(Mandatory = $false)] + [string]$TeamReviewers, + + [Parameter(Mandatory = $false)] + [string]$Assignees ) +$baseURI = "https://api.github.com/repos" +function SplitMembers ($membersString) +{ + return @($membersString.Split(",") | % { $_.Trim() } | ? { return $_ }) +} + +$userAdditions = SplitMembers -membersString $UserReviewers +$teamAdditions = SplitMembers -membersString $TeamReviewers +$labelAdditions = SplitMembers -membersString $PRLabels +$assigneeAdditions = SplitMembers -membersString $Assignees + $headers = @{ Authorization = "bearer $AuthToken" } -$query = "state=open&head=${PROwner}:${PRBranch}&base=${BaseBranch}" - -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 +function AddMembers($apiURI, $memberName, $additionSet) { + $headers = @{ + Authorization = "bearer $AuthToken" } + $errorOccurred = $false - # 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 - } try { - $resp = Invoke-RestMethod -Method PATCH -Headers $headers $prLabelUri -Body ($labelRequestData | ConvertTo-Json) + $postResp = @{} + $postResp[$memberName] = @($additionSet) + $postResp = $postResp | ConvertTo-Json + + Write-Host $postResp + $resp = Invoke-RestMethod -Method 'Post' -Headers $headers -Body $postResp -Uri $apiURI -MaximumRetryCount 3 + $resp | Write-Verbose } catch { - Write-Error "Invoke-RestMethod $prLabelUri failed with exception:`n$_" + Write-Error "Invoke-RestMethod $apiURI failed with exception:`n$_" + $errorOccurred = $true } - $resp | Write-Verbose - Write-Host -f green "Label(s) [$prLabelArray] added to pull request: https://github.com/$RepoOwner/$RepoName/pull/$prNumber" + return $errorOccurred +} + +function AddReviewers ($prNumber) { + $uri = "$baseURI/$RepoOwner/$RepoName/pulls/$prNumber/requested_reviewers" + if ($userAdditions) { + $errorsOccurredAddingUsers = AddMembers -apiURI $uri -memberName "reviewers" -additionSet $userAdditions + if ($errorsOccurredAddingUsers) { exit 1 } + Write-Host -f green "User(s) [$userAdditions] added to: https://github.com/$RepoOwner/$RepoName/issue/$prNumber" + } + if ($teamAdditions) { + $errorsOccurredAddingTeams = AddMembers -apiURI $uri -memberName "team_reviewers" -additionSet $teamAdditions + if ($errorsOccurredAddingTeams) { exit 1 } + Write-Host -f green "Team(s) [$teamAdditions] added to: https://github.com/$RepoOwner/$RepoName/issue/$prNumber" + } +} + +function AddAssignees ($prNumber) { + $uri = "$baseURI/$RepoOwner/$RepoName/issues/$prNumber" + if ($assigneeAdditions) { + $errorsOccurredAddingUsers = AddMembers -apiURI $uri -memberName "assignees" -additionSet $assigneeAdditions + if ($errorsOccurredAddingUsers) { exit 1 } + Write-Host -f green "Users(s) [$assigneeAdditions] added to: https://github.com/$RepoOwner/$RepoName/issue/$prNumber" + } } +function AddLabels ($prNumber) { + $uri = "$baseURI/$RepoOwner/$RepoName/issues/$prNumber" + if ($labelAdditions) { + $errorsOccurredAddingUsers = AddMembers -apiURI $uri -memberName "labels" -additionSet $labelAdditions + if ($errorsOccurredAddingUsers) { exit 1 } + Write-Host -f green "Label(s) [$labelAdditions] added to: https://github.com/$RepoOwner/$RepoName/issue/$prNumber" + } +} + +$query = "state=open&head=${PROwner}:${PRBranch}&base=${BaseBranch}" + try { $resp = Invoke-RestMethod -Headers $headers "https://api.github.com/repos/$RepoOwner/$RepoName/pulls?$query" } @@ -124,5 +174,7 @@ else { # setting variable to reference the pull request by number Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp.number)" - AddLabels $resp.number $PRLabels + AddReviewers -prNumber $resp.number + AddLabels -prNumber $resp.number + AddAssignees -prNumber $resp.number } diff --git a/eng/common/scripts/add-pullrequest-reviewers.ps1 b/eng/common/scripts/add-pullrequest-reviewers.ps1 deleted file mode 100644 index 3198dcb40d2c..000000000000 --- a/eng/common/scripts/add-pullrequest-reviewers.ps1 +++ /dev/null @@ -1,61 +0,0 @@ -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 new file mode 100644 index 000000000000..128f6ffe7fa2 --- /dev/null +++ b/eng/common/scripts/get-pr-creator.ps1 @@ -0,0 +1,26 @@ +param ( + $PullRequestNumber, + $VsoPRCreatorVariable, + $AuthToken +) + +$headers = @{ } + +if ($AuthToken) { + $headers = @{ + Authorization = "bearer $AuthToken" + } +} + +try +{ + $prApiUrl = "https://api.github.com/repos/Azure/azure-sdk-tools/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 +} +