-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathGitHubMergeBranches.ps1
executable file
·454 lines (364 loc) · 14.5 KB
/
GitHubMergeBranches.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env pwsh -c
<#
.DESCRIPTION
Creates a GitHub pull request to merge a head branch into a base branch
.PARAMETER RepoOwner
The GitHub repository owner.
.PARAMETER RepoName
The GitHub repository name.
.PARAMETER BaseBranch
The base branch -- the target branch for the PR
.PARAMETER HeadBranch
The current branch
.PARAMETER Username
The GitHub username
.PARAMETER AuthToken
A personal access token
.PARAMETER Fork
Make PR from a fork
.PARAMETER AllowAutomatedCommits
Create a PR even if the only commits are from dotnet-maestro[bot]
.PARAMETER QuietComments
Do not tag commiters, do not comment on PR updates. Reduces GitHub notifications
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Alias('o')]
[Parameter(Mandatory = $true)]
$RepoOwner,
[Alias('n')]
[Parameter(Mandatory = $true)]
$RepoName,
[Alias('b')]
[Parameter(Mandatory = $true)]
$BaseBranch,
[Alias('h')]
[Parameter(Mandatory = $true)]
$HeadBranch,
[Parameter(Mandatory = $true)]
[Alias('a')]
$AuthToken,
[Obsolete('Unused parameter. AuthToken is used to find the username.')]
[Alias('u')]
$Username,
[switch]$Fork,
[switch]$AllowAutomatedCommits,
[switch]$QuietComments
)
$ErrorActionPreference = 'stop'
Set-StrictMode -Version 1
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Workaround for quirk in how dotnet-maestro-bot triggers this script
if ($RepoName -like "$RepoOwner/*") {
$RepoName = $RepoName.Substring("$RepoOwner/".Length)
}
$headers = @{
Authorization = "bearer $AuthToken"
}
[hashtable] $script:emails = @{}
function Invoke-Block([scriptblock]$cmd) {
$cmd | Out-String | Write-Verbose
& $cmd
# Need to check both of these cases for errors as they represent different items
# - $?: did the powershell script block throw an error
# - $lastexitcode: did a windows command executed by the script block end in error
if ((-not $?) -or ($lastexitcode -ne 0)) {
if ($error -ne $null) {
Write-Warning $error[0]
}
throw "Command failed to execute: $cmd"
}
}
function GetCommitterGitHubName($sha) {
$email = & git show -s --format='%ce' $sha
$key = 'committer'
if ($email -eq '@dotnet-maestro') {
return 'dotnet-maestro'
}
# Exclude [email protected] - these map to https://github.com/web-flow, which is the user account
# added as the 'committer' when users commit via the GitHub web UI on their own PRs
if ((-not $email) -or ($email -eq '[email protected]')) {
$key = 'author'
$email = & git show -s --format='%ae' $sha
}
if ($email -like '*@users.noreply.github.com') {
[string[]] $userNames = ($email -replace '@users.noreply.github.com', '') -split '\+'
return $userNames | select -last 1
}
elseif ($script:emails[$email]) {
return $script:emails[$email]
}
else {
Write-Verbose "Attempting to find GitHub username for $email"
try {
$resp = Invoke-RestMethod -Method GET -Headers $headers `
"https://api.github.com/repos/$RepoOwner/$RepoName/commits/$sha"
$resp | Write-Verbose
$script:emails[$email] = $resp.$key.login
return $resp.$key.login
}
catch {
Write-Warning "Failed to find github user for $email. $_"
}
}
return $null
}
function RepoExists($owner, $name) {
try {
$resp = Invoke-RestMethod -Headers $headers "https://api.github.com/repos/$owner/$name"
$resp | Write-Verbose
return ($resp -ne $null)
}
catch {
return $false
}
}
function GetOrCreateFork() {
$resp = Invoke-RestMethod -Method Post -Headers $Headers `
"https://api.github.com/repos/$RepoOwner/$RepoName/forks"
$resp | Write-Verbose
# This effectively gets the owner based on the PAT.
$owner = $resp.owner.login
# If there are repos in different orgs with the same name, like dotnet/buildtools and
# aspnet/BuildTools, the name of the fork isn't predictable.
$name = $resp.name
$retries = 10
$repoCreated = $false
do {
$retries -= 1
if (RepoExists $owner $name) {
# Fork creation is an async operation. Wait a minute to give GitHub more time to finish fork creation
Start-Sleep -Seconds 90
$repoCreated = $true
break
}
Write-Host "Repo ${owner}/${name} does not exist yet. Waiting to check again..."
Start-Sleep -Seconds 30
} while ($retries -gt 0)
if (-not $repoCreated) {
throw "Could not create a fork ${owner}/${name} for ${RepoOwner}/${RepoName}"
}
return @{
Name = $name
Owner = $owner
}
}
$workDir = "$PSScriptRoot/obj/$RepoOwner/$RepoName"
New-Item "$PSScriptRoot/obj/" -ItemType Directory -ErrorAction Ignore | Out-Null
Invoke-Block { & git config --system core.longpaths true }
$fetch = $true
if (-not (Test-Path $workDir)) {
$fetch = $false
Invoke-Block { & git clone "https://github.com/$RepoOwner/$RepoName.git" $workDir `
--quiet `
--no-tags `
--branch $BaseBranch
}
}
# see https://git-scm.com/docs/pretty-formats
$formatString = '%h %cn <%ce>: %s (%cr)'
Push-Location $workDir
try {
if ($fetch) {
Invoke-Block { & git fetch --quiet origin }
Invoke-Block { & git checkout --quiet $BaseBranch }
Invoke-Block { & git reset --hard origin/$BaseBranch }
}
Write-Host -f Magenta "${BaseBranch}:`t`t$(& git log --format=$formatString -1 HEAD)"
Invoke-Block { & git checkout --quiet $HeadBranch }
Invoke-Block { & git reset --quiet --hard origin/$HeadBranch }
Write-Host -f Magenta "${HeadBranch}:`t$(& git log --format=$formatString -1 HEAD)"
[string[]] $commitsToMerge = & git rev-list "$BaseBranch..$HeadBranch" # find all commits which will be merged
if (-not $commitsToMerge) {
Write-Warning "There were no commits to be merged from $HeadBranch into $BaseBranch"
exit 0
}
$authors = $commitsToMerge `
| % { Write-Host -f Cyan "Merging:`t$(git log --format=$formatString -1 $_)"; $_ } `
| % { GetCommitterGitHubName $_ } `
| ? { $_ -ne $null } `
| select -Unique
if (-not $AllowAutomatedCommits -and (($authors | measure).Count -eq 1) -and ($authors | select -first 1) -eq 'dotnet-maestro[bot]') {
Write-Host -ForegroundColor Yellow 'Skipping PR generation because it appears this PR would only contain automated commits by @dotnet-maestro[bot]'
exit 0
}
if (-not $QuietComments) {
$authors = $authors | % { "* @$_" }
} else {
$authors = $authors | % { "* $_" }
}
$committersList = "This PR merges commits made on $HeadBranch by the following committers:`n`n$($authors -join "`n")"
Write-Host $committersList
$mergeBranchName = "merge/$HeadBranch-to-$BaseBranch"
Invoke-Block { & git checkout -B $mergeBranchName }
$remoteName = 'origin'
$prOwnerName = $RepoOwner
$prRepoName = $RepoName
if ($Fork) {
$remoteName = 'fork'
try {
# remove remote if it already exists and re-configure
Invoke-Block { & git remote remove fork }
}
catch { }
if ($PSCmdlet.ShouldProcess("Finding or creating fork for ${RepoName}")) {
Write-Host -ForegroundColor Yellow "Finding or creating fork for ${RepoName}"
$forkData = GetOrCreateFork
Invoke-Block { & git remote add fork "https://placeholderUser:${AuthToken}@github.com/$($forkData.Owner)/$($forkData.Name).git" }
$prOwnerName = $forkData.Owner
$prRepoName = $forkData.Name
}
}
$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 | Write-Verbose
$matchingPr = $resp.data.repository.pullRequests.nodes `
| ? { $_.headRef.name -eq $mergeBranchName -and $_.headRef.repository.owner.login -eq $prOwnerName } `
| select -First 1
if ($matchingPr) {
$prUpdatedSuccess = $false
try {
if ($PSCmdlet.ShouldProcess("Update remote branch $mergeBranchName on $remoteName")) {
Invoke-Block { & git push $remoteName "${mergeBranchName}:${mergeBranchName}" }
}
$prUpdatedSuccess = $true
}
catch {
Write-Warning "Failed to update existing PR"
}
$prMessage = if ($prUpdatedSuccess) {
"This pull request has been updated.`n`n$committersList"
} else {
@"
:x: Uh oh, this pull request could not be updated automatically. New commits were pushed to $HeadBranch, but I could not automatically push those to $mergeBranchName to update this PR.
You may need to fix this problem by merging branches with this PR. Contact .NET Core Engineering if you are not sure what to do about this.
"@
}
$data = @{
body = $prMessage
}
$prNumber = $matchingPr.number
$prUrl = "https://github.com/$RepoOwner/$RepoName/pull/$prNumber"
if ($PSCmdlet.ShouldProcess("Update $prUrl") -and -not $QuietComments) {
$resp = Invoke-RestMethod -Method Post -Headers $headers `
"https://api.github.com/repos/$RepoOwner/$RepoName/issues/$prNumber/comments" `
-Body ($data | ConvertTo-Json)
$resp | Write-Verbose
Write-Host -f green "Updated pull request $url"
}
}
else {
# Use --force because the merge branch may have been used for a previous PR.
# This should only happen if there is no existing PR for the merge
if ($PSCmdlet.ShouldProcess("Force updating remote branch $mergeBranchName on $remoteName")) {
Invoke-Block { & git push --force $remoteName "${mergeBranchName}:${mergeBranchName}" }
}
$previewHeaders = @{
# Required while this api is in preview: https://developer.github.com/v3/pulls/#create-a-pull-request
Accept = 'application/vnd.github.symmetra-preview+json'
Authorization = "bearer $AuthToken"
}
$prBody = @"
I detected changes in the $HeadBranch branch which have not been merged yet to $BaseBranch. I'm a robot and am [configured](https://github.com/dotnet/versions/blob/main/Maestro/subscriptions.json) to help you automatically keep $BaseBranch up to date, so I've opened this PR.
$committersList
## Instructions for merging from UI
This PR will not be auto-merged. When pull request checks pass, complete this PR by creating a merge commit, *not* a squash or rebase commit.
<img alt="merge button instructions" src="https://i.imgur.com/GepcNJV.png" width="300" />
If this repo does not allow creating merge commits from the GitHub UI, use command line instructions.
## Instructions for merging via command line
Run these commands to merge this pull request from the command line.
`````` sh
git fetch
git checkout ${HeadBranch}
git pull --ff-only
git checkout ${baseBranch}
git pull --ff-only
git merge --no-ff ${HeadBranch}
# If there are merge conflicts, resolve them and then run `git merge --continue` to complete the merge
# Pushing the changes to the PR branch will re-trigger PR validation.
git push https://github.com/$prOwnerName/$prRepoName HEAD:${mergeBranchName}
``````
<details>
<summary>or if you are using SSH</summary>
``````
git push [email protected]:$prOwnerName/$prRepoName HEAD:${mergeBranchName}
``````
</details>
After PR checks are complete push the branch
``````
git push
``````
## Instructions for resolving conflicts
:warning: If there are merge conflicts, you will need to resolve them manually before merging. You can do this [using GitHub][resolve-github] or using the [command line][resolve-cli].
[resolve-github]: https://help.github.com/articles/resolving-a-merge-conflict-on-github/
[resolve-cli]: https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/
## Instructions for updating this pull request
Contributors to this repo have permission update this pull request by pushing to the branch '$mergeBranchName'. This can be done to resolve conflicts or make other changes to this pull request before it is merged.
``````
git checkout -b ${mergeBranchName} $BaseBranch
git pull https://github.com/$prOwnerName/$prRepoName ${mergeBranchName}
(make changes)
git commit -m "Updated PR with my changes"
git push https://github.com/$prOwnerName/$prRepoName HEAD:${mergeBranchName}
``````
<details>
<summary>or if you are using SSH</summary>
``````
git checkout -b ${mergeBranchName} $BaseBranch
git pull [email protected]:$prOwnerName/$prRepoName ${mergeBranchName}
(make changes)
git commit -m "Updated PR with my changes"
git push [email protected]:$prOwnerName/$prRepoName HEAD:${mergeBranchName}
``````
</details>
Contact .NET Core Engineering if you have questions or issues.
Also, if this PR was generated incorrectly, help us fix it. See https://github.com/dotnet/arcade/blob/master/scripts/GitHubMergeBranches.ps1.
"@;
$data = @{
title = "[automated] Merge branch '$HeadBranch' => '$BaseBranch'"
head = "${prOwnerName}:${mergeBranchName}"
base = $BaseBranch
body = $prBody
maintainer_can_modify = $true
}
if ($PSCmdlet.ShouldProcess("Create PR from ${prOwnerName}:${mergeBranchName} to $BaseBranch on $Reponame")) {
$resp = Invoke-RestMethod -Method POST -Headers $previewHeaders `
https://api.github.com/repos/$RepoOwner/$RepoName/pulls `
-Body ($data | ConvertTo-Json)
$resp | Write-Verbose
Write-Host -f green "Created pull request https://github.com/$RepoOwner/$RepoName/pull/$($resp.number)"
}
}
}
finally {
Pop-Location
}