Skip to content
Closed
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 "$env:${{ parameters.GHReviewersVariable }}"
-TeamReviewers "$env:${{ parameters.GHTeamReviewersVariable }}"
-Assignees "$env:${{ parameters.GHAssignessVariable }}"
112 changes: 79 additions & 33 deletions eng/common/scripts/Submit-PullRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,79 @@ 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
)

. "${PSScriptRoot}\logging.ps1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not import common.ps1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far we only need the logging logic here, so I did not feel the need to import common.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I'm fine with this for now but at some point we might want to consider always importing common.ps1 just to avoid any differences for scripts we use in our eng-system. The only time we should avoid it is if we want to limit our dependencies in a script to use it in other contexts.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding a check for empty like:

if (!$memberString) { return $null }

That prevents any cases were we have an empty/null value and turn it into and empty array.

}

$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
LogDebug "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) {
if (!$users -and !$teams)
{
exit 0
}

# 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
else {
$uri = "$baseURI/$RepoOwner/$RepoName/pulls/$prNumber/requested_reviewers"
$userAdditions = SplitMembers -membersString $users
$teamAdditions = SplitMembers -membersString $teams
$postResp = @{}
if ($userAdditions) {
$postResp["reviewers"] = @($userAdditions)
}
if ($teamAdditions) {
$postResp["team_reviewers"] = @($teamAdditions)
}
return InvokeGitHubAPI -apiURI $uri -method 'Post' -body $postResp
}
try {
$resp = Invoke-RestMethod -Method PATCH -Headers $headers $prLabelUri -Body ($labelRequestData | ConvertTo-Json)
}

function AddLabelsAndOrAssignees ($prNumber, $labels, $assignees) {
if (!$labels -and !$assignees)
{
exit 0
}
catch {
Write-Error "Invoke-RestMethod $prLabelUri failed with exception:`n$_"
else {
$uri = "$baseURI/$RepoOwner/$RepoName/issues/$prNumber"
$labelAdditions = SplitMembers -membersString $labels
$assigneeAdditions = SplitMembers -membersString $assignees
$postResp = @{}
if ($assigneeAdditions) {
$postResp["assignees"] = @($assigneeAdditions)
}
if ($labelAdditions) {
$postResp["labels"] = @($labelAdditions)
}
return InvokeGitHubAPI -apiURI $uri -method 'Post' -body $postResp
}

$resp | Write-Verbose
Write-Host -f green "Label(s) [$prLabelArray] added to pull request: https://github.com/$RepoOwner/$RepoName/pull/$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 @@ -93,11 +132,18 @@ catch {
$resp | Write-Verbose

if ($resp.Count -gt 0) {
Write-Host -f green "Pull request already exists $($resp[0].html_url)"
try {
LogDebug "Pull request already exists $($resp[0].html_url)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep this a normal log message otherwise it will not show up in Devops logging unless you set System.Debug = true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# 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 {
LogError "Call to GitHub API failed with exception:`n$_"
exit 1
}
}
else {
$data = @{
Expand All @@ -112,17 +158,17 @@ else {
$resp = Invoke-RestMethod -Method POST -Headers $headers `
"https://api.github.com/repos/$RepoOwner/$RepoName/pulls" `
-Body ($data | ConvertTo-Json)

$resp | Write-Verbose
LogDebug "Pull request created https://github.com/$RepoOwner/$RepoName/pull/$($resp.number)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets keep this a normal log


# 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$_"
LogError "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
}

4 changes: 2 additions & 2 deletions eng/common/scripts/logging.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function LogDebug
{
if ($isDevOpsRun)
{
Write-Host "##vso[task.LogIssue type=debug;]$args"
Write-Host "[debug]$args"
Copy link
Member

@weshaggard weshaggard Sep 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh on my other comments I assume this was still debug logging. I think I would keep this as is so we have support for that debug logging. Perhaps we need a LogInfo or something similar that will just do a Write-Host.

}
else
{
Write-Debug "$args"
Write-Debug "$args" -Debug
}
}
14 changes: 14 additions & 0 deletions eng/pipelines/eng-common-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ stages:
displayName: Sync ${{ parameters.DirectoryToSync }} Directory

steps:
- ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
- task: PowerShell@2
displayName: 'Get alias of the Tools PR Creator'
inputs:
targetType: filePath
filePath: $(Build.SourcesDirectory)/eng/common/scripts/get-pr-creator.ps1
arguments: >
-RepoOwner "Azure"
-RepoName "azure-sdk-tools"
-PullRequestNumber "$(System.PullRequest.PullRequestNumber)"
-VsoPRCreatorVariable "System.PullRequest.Creator"
-AuthToken "$(azuresdk-github-pat)"
pwsh: true

- template: ./templates/steps/sync-directory.yml
parameters:
${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
Expand Down
2 changes: 2 additions & 0 deletions eng/pipelines/templates/steps/sync-directory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ steps:
PushArgs: -f
WorkingDirectory: $(System.DefaultWorkingDirectory)/${{ repo }}
ScriptDirectory: $(System.DefaultWorkingDirectory)/eng/common/scripts
GHReviewersVariable: "System.PullRequest.Creator"
GHAssignessVariable: "System.PullRequest.Creator"

- pwsh: |
$PRData = "Azure;${{ repo }};$(Submitted.PullRequest.Number)"
Expand Down