...
1 #!/usr/bin/env pwsh -c
2
3<#
4.DESCRIPTION
5Creates a GitHub pull request for a given branch if it doesn't already exist
6.PARAMETER RepoOwner
7The GitHub repository owner to create the pull request against.
8.PARAMETER RepoName
9The GitHub repository name to create the pull request against.
10.PARAMETER BaseBranch
11The base or target branch we want the pull request to be against.
12.PARAMETER PROwner
13The owner of the branch we want to create a pull request for.
14.PARAMETER PRBranch
15The branch which we want to create a pull request for.
16.PARAMETER AuthToken
17A personal access token
18.PARAMETER PRTitle
19The title of the pull request.
20.PARAMETER PRBody
21The body message for the pull request.
22.PARAMETER PRLabels
23The labels added to the PRs. Multple labels seperated by comma, e.g "bug, service"
24.PARAMETER UserReviewers
25User reviewers to request after opening the PR. Users should be a comma-
26separated list with no preceeding `@` symbol (e.g. "user1,usertwo,user3")
27.PARAMETER TeamReviewers
28List of github teams to add as reviewers
29.PARAMETER Assignees
30Users to assign to the PR after opening. Users should be a comma-separated list
31with no preceeding `@` symbol (e.g. "user1,usertwo,user3")
32.PARAMETER CloseAfterOpenForTesting
33Close the PR after opening to save on CI resources and prevent alerts to code
34owners, assignees, requested reviewers, or others.
35.PARAMETER OpenAsDraft
36Opens the PR as a draft
37#>
38[CmdletBinding(SupportsShouldProcess = $true)]
39param(
40 [Parameter(Mandatory = $true)]
41 [string]$RepoOwner,
42
43 [Parameter(Mandatory = $true)]
44 [string]$RepoName,
45
46 [Parameter(Mandatory = $true)]
47 [string]$BaseBranch,
48
49 [Parameter(Mandatory = $true)]
50 [string]$PROwner,
51
52 [Parameter(Mandatory = $true)]
53 [string]$PRBranch,
54
55 [Parameter(Mandatory = $true)]
56 [string]$AuthToken,
57
58 [Parameter(Mandatory = $true)]
59 [string]$PRTitle,
60
61 [Parameter(Mandatory = $false)]
62 [string]$PRBody = $PRTitle,
63
64 [string]$PRLabels,
65
66 [string]$UserReviewers,
67
68 [string]$TeamReviewers,
69
70 [string]$Assignees,
71
72 [boolean]$CloseAfterOpenForTesting=$false,
73
74 [boolean]$OpenAsDraft=$false
75)
76
77. (Join-Path $PSScriptRoot common.ps1)
78
79try {
80 $resp = Get-GitHubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName `
81 -Head "${PROwner}:${PRBranch}" -Base $BaseBranch -AuthToken $AuthToken
82}
83catch {
84 LogError "Get-GitHubPullRequests failed with exception:`n$_"
85 exit 1
86}
87
88$resp | Write-Verbose
89
90if ($resp.Count -gt 0) {
91 LogDebug "Pull request already exists $($resp[0].html_url)"
92 # setting variable to reference the pull request by number
93 Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp[0].number)"
94}
95else {
96 try {
97 $resp = New-GitHubPullRequest `
98 -RepoOwner $RepoOwner `
99 -RepoName $RepoName `
100 -Title $PRTitle `
101 -Head "${PROwner}:${PRBranch}" `
102 -Base $BaseBranch `
103 -Body $PRBody `
104 -Maintainer_Can_Modify $true `
105 -Draft:$OpenAsDraft `
106 -AuthToken $AuthToken
107
108 $resp | Write-Verbose
109 LogDebug "Pull request created https://github.com/$RepoOwner/$RepoName/pull/$($resp.number)"
110
111 $prOwnerUser = $resp.user.login
112
113 # setting variable to reference the pull request by number
114 Write-Host "##vso[task.setvariable variable=Submitted.PullRequest.Number]$($resp.number)"
115
116 # ensure that the user that was used to create the PR is not attempted to add as a reviewer
117 # we cast to an array to ensure that length-1 arrays actually stay as array values
118 $cleanedUsers = @(SplitParameterArray -members $UserReviewers) | ? { $_ -ne $prOwnerUser -and $null -ne $_ }
119 $cleanedTeamReviewers = @(SplitParameterArray -members $TeamReviewers) | ? { $_ -ne $prOwnerUser -and $null -ne $_ }
120
121 if ($cleanedUsers -or $cleanedTeamReviewers) {
122 Add-GitHubPullRequestReviewers -RepoOwner $RepoOwner -RepoName $RepoName -PrNumber $resp.number `
123 -Users $cleanedUsers -Teams $cleanedTeamReviewers -AuthToken $AuthToken
124 }
125
126 if ($CloseAfterOpenForTesting) {
127 $prState = "closed"
128 LogDebug "Updating https://github.com/$RepoOwner/$RepoName/pull/$($resp.number) state to closed because this was only testing."
129 }
130 else {
131 $prState = "open"
132 }
133
134 Update-GitHubIssue -RepoOwner $RepoOwner -RepoName $RepoName -IssueNumber $resp.number `
135 -State $prState -Labels $PRLabels -Assignees $Assignees -AuthToken $AuthToken
136 }
137 catch {
138 LogError "Call to GitHub API failed with exception:`n$_"
139 exit 1
140 }
141}
View as plain text