Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools repository (#9655)
Browse files Browse the repository at this point in the history
  • Loading branch information
azure-sdk authored Mar 30, 2020
1 parent 42a1a59 commit 2ba19d6
Showing 1 changed file with 36 additions and 74 deletions.
110 changes: 36 additions & 74 deletions eng/common/scripts/Submit-PullRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,91 +18,53 @@ A personal access token
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoOwner,

[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$RepoName,

[Parameter(Mandatory = $true)]
$BaseBranch,
[Parameter(Mandatory = $true)]
$BaseBranch,

[Parameter(Mandatory = $true)]
$PROwner,
[Parameter(Mandatory = $true)]
$PROwner,

[Parameter(Mandatory = $true)]
$PRBranch,
[Parameter(Mandatory = $true)]
$PRBranch,

[Parameter(Mandatory = $true)]
$AuthToken,
[Parameter(Mandatory = $true)]
$AuthToken,

[Parameter(Mandatory = $true)]
$PRTitle,
$PRBody = $PRTitle
[Parameter(Mandatory = $true)]
$PRTitle,
$PRBody = $PRTitle
)

$ErrorActionPreference = 'stop'
Set-StrictMode -Version 1
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$query = "state=open&head=${PROwner}:${PRBranch}&base=${BaseBranch}"

$headers = @{
Authorization = "bearer $AuthToken"
}

$query = 'query ($repoOwner: String!, $repoName: String!, $baseRefName: String!) {
repository(owner: $repoOwner, name: $repoName) {
pullRequests(baseRefName: $baseRefName, states: OPEN, first: 100) {
totalCount
nodes {
number
headRef {
name
repository {
name
owner {
login
}
}
}
}
}
}
}'


$data = @{
query = $query
variables = @{
repoOwner = $RepoOwner
repoName = $RepoName
baseRefName = $BaseBranch
}
}

$resp = Invoke-RestMethod -Method Post -Headers $headers `
https://api.github.com/graphql `
-Body ($data | ConvertTo-Json)
$resp = Invoke-RestMethod "https://api.github.com/repos/$RepoOwner/$RepoName/pulls?$query"
$resp | Write-Verbose

$matchingPr = $resp.data.repository.pullRequests.nodes `
| ? { $_.headRef.name -eq $PRBranch -and $_.headRef.repository.owner.login -eq $PROwner } `
| select -First 1

if ($matchingPr) {
Write-Host -f green "Pull request already exists https://github.com/$RepoOwner/$RepoName/pull/$($matchingPr.number)"
if ($resp.Count -gt 0) {
Write-Host -f green "Pull request already exists $($resp[0].html_url)"
}
else {
$data = @{
title = $PRTitle
head = "${PROwner}:${PRBranch}"
base = $BaseBranch
body = $PRBody
maintainer_can_modify = $true
}

$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)"
$headers = @{
Authorization = "bearer $AuthToken"
}

$data = @{
title = $PRTitle
head = "${PROwner}:${PRBranch}"
base = $BaseBranch
body = $PRBody
maintainer_can_modify = $true
}

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

0 comments on commit 2ba19d6

Please sign in to comment.