Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync eng/common directory with azure-sdk-tools for PR 989 #11214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -15,6 +15,7 @@ parameters:
ScriptDirectory: eng/common/scripts
GHReviewersVariable: ''
GHTeamReviewersVariable: ''
GHAssignessVariable: ''
# Multiple labels seperated by comma, e.g. "bug, APIView"
PRLabels: ''

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 }})"
95 changes: 63 additions & 32 deletions eng/common/scripts/Submit-PullRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,64 @@ 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
)

$headers = @{
Authorization = "bearer $AuthToken"
}
$baseURI = "https://api.github.com/repos"
function SplitMembers ($membersString)
{
return @($membersString.Split(",") | % { $_.Trim() } | ? { return $_ })
}

$query = "state=open&head=${PROwner}:${PRBranch}&base=${BaseBranch}"
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
}

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 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)
}

# 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 ($teamAdditions) {
$postResp["team_reviewers"] = @($teamAdditions)
}
try {
$resp = Invoke-RestMethod -Method PATCH -Headers $headers $prLabelUri -Body ($labelRequestData | ConvertTo-Json)
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)
}
catch {
Write-Error "Invoke-RestMethod $prLabelUri failed with exception:`n$_"
if ($labelAdditions) {
$postResp["labels"] = @($labelAdditions)
}

$resp | Write-Verbose
Write-Host -f green "Label(s) [$prLabelArray] added to pull request: https://github.com/$RepoOwner/$RepoName/pull/$prNumber"
return InvokeGitHubAPI -apiURI $uri -method 'Post' -body $postResp
}

$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 @@ -93,11 +117,18 @@ 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)"
AddLabels $resp[0].number $PRLabels
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
}
}
else {
$data = @{
Expand All @@ -112,17 +143,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 "Invoke-RestMethod [https://api.github.com/repos/$RepoOwner/$RepoName/pulls] failed with exception:`n$_"
Write-Error "Call to GitHub API failed with exception:`n$_"
exit 1
}

$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
}
}
61 changes: 0 additions & 61 deletions eng/common/scripts/add-pullrequest-reviewers.ps1

This file was deleted.

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