summaryrefslogtreecommitdiff
path: root/source/helpers/clean-commit-message.ts
blob: 10958eb3c892e50970a0dc6dfeb7d9771a64039e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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');
}