From b08db9f9107e0f775be553601f939a712393b2d8 Mon Sep 17 00:00:00 2001 From: azure-sdk Date: Mon, 21 Sep 2020 23:09:09 +0000 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools repository for Tools PR 989 --- .../templates/steps/create-pull-request.yml | 20 +--- eng/common/scripts/Submit-PullRequest.ps1 | 96 ++++++++++++++----- .../scripts/add-pullrequest-reviewers.ps1 | 61 ------------ eng/common/scripts/get-pr-creator.ps1 | 26 +++++ 4 files changed, 104 insertions(+), 99 deletions(-) delete mode 100644 eng/common/scripts/add-pullrequest-reviewers.ps1 create mode 100644 eng/common/scripts/get-pr-creator.ps1 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..c78ab340e425 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, + + $PRNumber ) +$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, $body) { + $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) + if (-not $body) + { + $body = @{} + $body[$memberName] = @($additionSet) + } + + $resp = Invoke-RestMethod -Method 'Post' -Headers $headers -Body ($body | ConvertTo-Json) -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 AddLabelsAndOrAssignees ($prNumber) { + $uri = "$baseURI/$RepoOwner/$RepoName/issues/$prNumber" + $postResp = @{} + if ($assigneeAdditions) { + $postResp["assignees"] = @($assigneeAdditions) + } + if ($labelAdditions) { + $postResp["labels"] = @($labelAdditions) + } + $errorsOccurredAddingUsers = AddMembers -apiURI $uri -body $postResp + if ($errorsOccurredAddingUsers) { exit 1 } + Write-Host -f green "Members(s) [$postResp] 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" } @@ -97,7 +147,8 @@ if ($resp.Count -gt 0) { # setting variable to reference the pull request by number Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp[0].number)" - AddLabels $resp[0].number $PRLabels + AddReviewers -prNumber $resp[0].number + AddLabelsAndOrAssignees -prNumber $resp[0].number } else { $data = @{ @@ -124,5 +175,6 @@ 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 + AddLabelsAndOrAssignees -prNumber $resp.number +} \ No newline at end of file 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 +} +