Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools repository for Tools P…
Browse files Browse the repository at this point in the history
…R 989
  • Loading branch information
azure-sdk committed Sep 21, 2020
1 parent 553cbf4 commit ab18e7a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 97 deletions.
20 changes: 4 additions & 16 deletions eng/common/pipelines/templates/steps/create-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parameters:
GHTeamReviewersVariable: ''
# Multiple labels seperated by comma, e.g. "bug, APIView"
PRLabels: ''
GHAssignessVariable: ''

steps:

Expand Down Expand Up @@ -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 }})"
92 changes: 72 additions & 20 deletions eng/common/scripts/Submit-PullRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
}
61 changes: 0 additions & 61 deletions eng/common/scripts/add-pullrequest-reviewers.ps1

This file was deleted.

26 changes: 26 additions & 0 deletions eng/common/scripts/get-pr-creator.ps1
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ab18e7a

Please sign in to comment.