From d2e2c8db1bd85e0e63f29e3178707cd691fbe07b Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Mon, 24 Aug 2020 14:09:51 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools repository for Tools PR https://github.com/Azure/azure-sdk-tools/pull/881 (#484) --- .../templates/steps/create-pull-request.yml | 3 ++ eng/common/scripts/Submit-PullRequest.ps1 | 50 ++++++++++++++++--- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/eng/common/pipelines/templates/steps/create-pull-request.yml b/eng/common/pipelines/templates/steps/create-pull-request.yml index 7b3e3a75f..3ccd4c956 100644 --- a/eng/common/pipelines/templates/steps/create-pull-request.yml +++ b/eng/common/pipelines/templates/steps/create-pull-request.yml @@ -15,6 +15,8 @@ parameters: ScriptDirectory: eng/common/scripts GHReviewersVariable: '' GHTeamReviewersVariable: '' + # Multiple labels seperated by comma, e.g. "bug, APIView" + PRLabels: '' steps: @@ -66,6 +68,7 @@ steps: -PRBranch "${{ parameters.PRBranchName }}" -AuthToken "$(azuresdk-github-pat)" -PRTitle "${{ parameters.PRTitle }}" + -PRLabels "${{ parameters.PRLabels}}" -PRBody "${{ parameters.PRBody }}" - task: PowerShell@2 diff --git a/eng/common/scripts/Submit-PullRequest.ps1 b/eng/common/scripts/Submit-PullRequest.ps1 index f80033dfa..68a4db9c2 100644 --- a/eng/common/scripts/Submit-PullRequest.ps1 +++ b/eng/common/scripts/Submit-PullRequest.ps1 @@ -15,31 +15,37 @@ The owner of the branch we want to create a pull request for. The branch which we want to create a pull request for. .PARAMETER AuthToken A personal access token +.PARAMETER PRTitle +The title of the pull request. +.PARAMETER PRLabels +The labels added to the PRs. Multple labels seperated by comma, e.g "bug, service" #> [CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] - $RepoOwner, + [string]$RepoOwner, [Parameter(Mandatory = $true)] - $RepoName, + [string]$RepoName, [Parameter(Mandatory = $true)] - $BaseBranch, + [string]$BaseBranch, [Parameter(Mandatory = $true)] - $PROwner, + [string]$PROwner, [Parameter(Mandatory = $true)] - $PRBranch, + [string]$PRBranch, [Parameter(Mandatory = $true)] - $AuthToken, + [string]$AuthToken, [Parameter(Mandatory = $true)] - $PRTitle, + [string]$PRTitle, + $PRBody = $PRTitle, - $PRBody=$PRTitle + [Parameter(Mandatory = $false)] + [string]$PRLabels ) $headers = @{ @@ -48,6 +54,31 @@ $headers = @{ $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 + } + + # 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) + } + catch { + Write-Error "Invoke-RestMethod $prLabelUri failed with exception:`n$_" + } + + $resp | Write-Verbose + Write-Host -f green "Label(s) [$prLabelArray] added to pull request: https://github.com/$RepoOwner/$RepoName/pull/$prNumber" +} + try { $resp = Invoke-RestMethod -Headers $headers "https://api.github.com/repos/$RepoOwner/$RepoName/pulls?$query" } @@ -62,6 +93,7 @@ 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 } else { $data = @{ @@ -87,4 +119,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 }