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 3f29c59 commit b08db9f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 99 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 }})"
96 changes: 74 additions & 22 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,

$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"
}
Expand All @@ -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 = @{
Expand All @@ -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
}
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 b08db9f

Please sign in to comment.