Skip to content

Commit 8447283

Browse files
authored
[release/v7.5] Update Get-ChangeLog to handle backport PRs correctly (#26824)
1 parent 54b881d commit 8447283

File tree

1 file changed

+87
-26
lines changed

1 file changed

+87
-26
lines changed

tools/releaseTools.psm1

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ $Script:powershell_team = @(
3737
"dependabot-preview[bot]"
3838
"dependabot[bot]"
3939
"github-actions[bot]"
40+
"Copilot"
4041
"Anam Navied"
4142
"Andrew Schwartzmeyer"
4243
"Jason Helmick"
@@ -46,6 +47,27 @@ $Script:powershell_team = @(
4647
"Justin Chung"
4748
)
4849

50+
# The powershell team members GitHub logins. We use them to decide if the original author of a backport PR is from the team.
51+
$script:psteam_logins = @(
52+
'andyleejordan'
53+
'TravisEz13'
54+
'daxian-dbw'
55+
'adityapatwardhan'
56+
'SteveL-MSFT'
57+
'dependabot[bot]'
58+
'pwshBot'
59+
'jshigetomi'
60+
'SeeminglyScience'
61+
'anamnavi'
62+
'sdwheeler'
63+
'Copilot'
64+
'copilot-swe-agent'
65+
'app/copilot-swe-agent'
66+
'StevenBucher98'
67+
'alerickson'
68+
'tgauth'
69+
)
70+
4971
# They are very active contributors, so we keep their email-login mappings here to save a few queries to Github.
5072
$Script:community_login_map = @{
5173
"darpa@yandex.ru" = "iSazonov"
@@ -54,11 +76,6 @@ $Script:community_login_map = @{
5476
"info@powercode-consulting.se" = "powercode"
5577
}
5678

57-
# Ignore dependency bumping bot (Dependabot):
58-
$Script:attribution_ignore_list = @(
59-
'dependabot[bot]@users.noreply.github.com'
60-
)
61-
6279
##############################
6380
#.SYNOPSIS
6481
#In the release workflow, the release branch will be merged back to master after the release is done,
@@ -262,25 +279,76 @@ function Get-ChangeLog
262279
$clExperimental = @()
263280

264281
foreach ($commit in $new_commits) {
282+
$commitSubject = $commit.Subject
283+
$prNumber = $commit.PullRequest
284+
Write-Verbose "subject: $commitSubject"
265285
Write-Verbose "authorname: $($commit.AuthorName)"
266-
if ($commit.AuthorEmail.EndsWith("@microsoft.com") -or $powershell_team -contains $commit.AuthorName -or $Script:attribution_ignore_list -contains $commit.AuthorEmail) {
267-
$commit.ChangeLogMessage = "- {0}" -f (Get-ChangeLogMessage $commit.Subject)
286+
287+
try {
288+
$pr = Invoke-RestMethod `
289+
-Uri "https://api.github.com/repos/PowerShell/PowerShell/pulls/$prNumber" `
290+
-Headers $header `
291+
-ErrorAction Stop `
292+
-Verbose:$false ## Always disable verbose to avoid noise when we debug this function.
293+
} catch {
294+
## A commit may not have corresponding GitHub PRs. In that case, we will get status code 404 (Not Found).
295+
## Otherwise, let the error bubble up.
296+
if ($_.Exception.Response.StatusCode -ne 404) {
297+
throw
298+
}
299+
}
300+
301+
if ($commitSubject -match '^\[release/v\d\.\d\] ') {
302+
## The commit was from a backport PR. We need to get the real author in this case.
303+
if (-not $pr) {
304+
throw "The commit is from a backport PR (#$prNumber), but the PR cannot be found.`nPR Title: $commitSubject"
305+
}
306+
307+
$userPattern = 'Triggered by @.+ on behalf of @(.+)'
308+
if ($pr.body -match $userPattern) {
309+
$commit.AuthorGitHubLogin = ($Matches.1).Trim()
310+
Write-Verbose "backport PR. real author login: $($commit.AuthorGitHubLogin)"
311+
} else {
312+
throw "The commit is from a backport PR (#$prNumber), but the PR description failed to match the pattern '$userPattern'. Was the template for backport PRs changed?`nPR Title: $commitSubject"
313+
}
314+
}
315+
316+
if ($commit.AuthorGitHubLogin) {
317+
if ($script:psteam_logins -contains $commit.AuthorGitHubLogin) {
318+
$commit.ChangeLogMessage = "- {0}" -f (Get-ChangeLogMessage $commitSubject)
319+
} else {
320+
$commit.ChangeLogMessage = ("- {0} (Thanks @{1}!)" -f (Get-ChangeLogMessage $commitSubject), $commit.AuthorGitHubLogin)
321+
$commit.ThankYouMessage = ("@{0}" -f ($commit.AuthorGitHubLogin))
322+
}
323+
} elseif ($commit.AuthorEmail.EndsWith("@microsoft.com") -or $powershell_team -contains $commit.AuthorName) {
324+
$commit.ChangeLogMessage = "- {0}" -f (Get-ChangeLogMessage $commitSubject)
268325
} else {
269326
if ($community_login_map.ContainsKey($commit.AuthorEmail)) {
270327
$commit.AuthorGitHubLogin = $community_login_map[$commit.AuthorEmail]
271328
} else {
272-
$uri = "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)"
273329
try{
274-
$response = Invoke-WebRequest -Uri $uri -Method Get -Headers $header -ErrorAction Ignore
275-
} catch{}
330+
## Always disable verbose to avoid noise when we debug this function.
331+
$response = Invoke-RestMethod `
332+
-Uri "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)" `
333+
-Headers $header `
334+
-ErrorAction Stop `
335+
-Verbose:$false
336+
} catch {
337+
## A commit could be available in ADO only. In that case, we will get status code 422 (UnprocessableEntity).
338+
## Otherwise, let the error bubble up.
339+
if ($_.Exception.Response.StatusCode -ne 422) {
340+
throw
341+
}
342+
}
343+
276344
if($response)
277345
{
278-
$content = ConvertFrom-Json -InputObject $response.Content
279-
$commit.AuthorGitHubLogin = $content.author.login
346+
$commit.AuthorGitHubLogin = $response.author.login
280347
$community_login_map[$commit.AuthorEmail] = $commit.AuthorGitHubLogin
281348
}
282349
}
283-
$commit.ChangeLogMessage = ("- {0} (Thanks @{1}!)" -f (Get-ChangeLogMessage $commit.Subject), $commit.AuthorGitHubLogin)
350+
351+
$commit.ChangeLogMessage = ("- {0} (Thanks @{1}!)" -f (Get-ChangeLogMessage $commitSubject), $commit.AuthorGitHubLogin)
284352
$commit.ThankYouMessage = ("@{0}" -f ($commit.AuthorGitHubLogin))
285353
}
286354

@@ -289,16 +357,6 @@ function Get-ChangeLog
289357
}
290358

291359
## Get the labels for the PR
292-
try {
293-
$pr = Invoke-RestMethod -Uri "https://api.github.com/repos/PowerShell/PowerShell/pulls/$($commit.PullRequest)" -Headers $header -ErrorAction SilentlyContinue
294-
}
295-
catch {
296-
if ($_.Exception.Response.StatusCode -eq '404') {
297-
$pr = $null
298-
#continue
299-
}
300-
}
301-
302360
if($pr)
303361
{
304362
$clLabel = $pr.labels | Where-Object { $_.Name -match "^CL-"}
@@ -328,7 +386,7 @@ function Get-ChangeLog
328386
"CL-Tools" { $clTools += $commit }
329387
"CL-Untagged" { $clUntagged += $commit }
330388
"CL-NotInBuild" { continue }
331-
Default { throw "unknown tag '$cLabel' for PR: '$($commit.PullRequest)'" }
389+
Default { throw "unknown tag '$cLabel' for PR: '$prNumber'" }
332390
}
333391
}
334392
}
@@ -426,6 +484,9 @@ function Get-ChangeLogMessage
426484
'^Build\(deps\): ' {
427485
return $OriginalMessage.replace($Matches.0,'')
428486
}
487+
'^\[release/v\d\.\d\] ' {
488+
return $OriginalMessage.replace($Matches.0,'')
489+
}
429490
default {
430491
return $OriginalMessage
431492
}
@@ -867,8 +928,8 @@ function Invoke-PRBackport {
867928
)
868929
$continue = $false
869930
while(!$continue) {
870-
$input= Read-Host -Prompt ($Message + "`nType 'Yes<enter>' to continue 'No<enter>' to exit")
871-
switch($input) {
931+
$value = Read-Host -Prompt ($Message + "`nType 'Yes<enter>' to continue 'No<enter>' to exit")
932+
switch($value) {
872933
'yes' {
873934
$continue= $true
874935
}

0 commit comments

Comments
 (0)