-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "closing keyword" logic in
clear-pr-merge-commit-message
(#6328)
- Loading branch information
Showing
7 changed files
with
150 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import select from 'select-dom'; | ||
import splitOnFirst from 'split-on-first'; | ||
|
||
export type PrReference = { | ||
/** @example fregante/mem:main */ | ||
full: string; | ||
|
||
/** @example "main" on same-repo PRs, "fregante:main" on cross-repo PRs */ | ||
local: string; | ||
|
||
/** @example fregante */ | ||
owner: string; | ||
|
||
/** @example mem */ | ||
name: string; | ||
|
||
/** @example main */ | ||
branch: string; | ||
}; | ||
|
||
function parseReference(referenceElement: HTMLElement): PrReference { | ||
const {title: full, textContent: local} = referenceElement; | ||
const [nameWithOwner, branch] = splitOnFirst(full, ':') as [string, string]; | ||
const [owner, name] = nameWithOwner.split(':'); | ||
return {full, owner, name, branch, local: local!.trim()}; | ||
} | ||
|
||
// TODO: Use in more places, like anywhere '.base-ref' appears | ||
export function getBranches(): {base: PrReference; head: PrReference} { | ||
return { | ||
base: parseReference(select('.base-ref')!), | ||
head: parseReference(select('.head-ref')!), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {test, assert} from 'vitest'; | ||
|
||
import cleanCommitMessage from './clean-commit-message'; | ||
|
||
test('cleanCommitMessage', () => { | ||
const coauthors = [ | ||
'Co-authored-by: Me <[email protected]>', | ||
'co-authored-by: Me <[email protected]>', | ||
'Co-authored-by: You <[email protected]>', | ||
]; | ||
assert.isEmpty(cleanCommitMessage('')); | ||
assert.isEmpty(cleanCommitMessage('clean me')); | ||
assert.isEmpty(cleanCommitMessage(` | ||
multi-line | ||
`)); | ||
|
||
assert.equal(cleanCommitMessage(` | ||
Some stuff happened | ||
${coauthors[0]} | ||
`), coauthors[0], 'Should preserve just the co-authors'); | ||
|
||
assert.equal(cleanCommitMessage(` | ||
Some stuff happened | ||
${coauthors[0]} | ||
Fixes #112345 | ||
${coauthors[2]} | ||
`), coauthors[0] + '\n' + coauthors[2], 'Should preserve multiple co-authors'); | ||
|
||
assert.equal(cleanCommitMessage(` | ||
Some stuff happened | ||
${coauthors[0]} | ||
More stuff | ||
${coauthors[1]} | ||
`), coauthors[0], 'Should de-duplicate inconsistent co-authored-by casing'); | ||
|
||
assert.isEmpty(cleanCommitMessage(` | ||
Fixes #1345 | ||
`), 'Should drop closing keywords'); | ||
|
||
assert.equal(cleanCommitMessage(` | ||
Fixes #1345 | ||
`, true), 'Fixes #1345', 'Should keep closing keywords when asked'); | ||
assert.equal(cleanCommitMessage(` | ||
Fixes #1 | ||
${coauthors[0]} | ||
closes https://github.com/refined-github/refined-github/pull/6328 | ||
`, true), [ | ||
coauthors[0], | ||
'Fixes #1', | ||
'closes https://github.com/refined-github/refined-github/pull/6328', | ||
].join('\n'), 'Should keep multiple closing keywords'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export default function cleanCommitMessage(message: string, closingKeywords = false): string { | ||
const preservedContent = new Set(); | ||
|
||
// This method ensures that "Co-authored-by" capitalization doesn't affect deduplication | ||
for (const [, author] of message.matchAll(/co-authored-by: ([^\n]+)/gi)) { | ||
preservedContent.add('Co-authored-by: ' + author); | ||
} | ||
|
||
if (!closingKeywords) { | ||
return [...preservedContent].join('\n'); | ||
} | ||
|
||
// Preserve closing issues numbers when a PR is merged into a non-default branch since GitHub doesn't close them #4531 | ||
// https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue | ||
for (const [line] of message.matchAll(/(fix(es|ed)?|close[sd]?|resolve[sd]?)([^\n]+)/gi)) { | ||
// Ensure it includes a reference or URL | ||
if (/#\d+/.test(line) || line.includes('http')) { | ||
preservedContent.add(line); | ||
} | ||
} | ||
|
||
return [...preservedContent].join('\n'); | ||
} |