( message: string, closingKeywords = false, excludeUsers: string[] = [], )
| 3 | } |
| 4 | |
| 5 | export default function cleanCommitMessage( |
| 6 | message: string, |
| 7 | closingKeywords = false, |
| 8 | excludeUsers: string[] = [], |
| 9 | ): string { |
| 10 | const preservedContent = new Set(); |
| 11 | |
| 12 | // This method ensures that "Co-authored-by" capitalization doesn't affect deduplication. |
| 13 | // Also drops co-authors whose GitHub privacy email username is in `excludeUsers`. |
| 14 | for (const match of message.matchAll(/co-authored-by: (?<author>[^\n]+)/gi)) { |
| 15 | const {author} = match.groups!; |
| 16 | const username = parseUserFromEmail(author); |
| 17 | if (username && excludeUsers.includes(username)) { |
| 18 | continue; |
| 19 | } |
| 20 | |
| 21 | preservedContent.add('Co-authored-by: ' + author); |
| 22 | } |
| 23 | |
| 24 | // Preserve "Signed-off-by" lines (DCO signoffs) |
| 25 | // https://github.com/refined-github/refined-github/issues/9330#issuecomment-4361024401 |
| 26 | for (const match of message.matchAll(/signed-off-by: (?<signer>[^\n]+)/gi)) { |
| 27 | const {signer} = match.groups!; |
| 28 | preservedContent.add('Signed-off-by: ' + signer); |
| 29 | } |
| 30 | |
| 31 | if (!closingKeywords) { |
| 32 | return [...preservedContent].join('\n'); |
| 33 | } |
| 34 | |
| 35 | // Preserve closing issues numbers when a PR is merged into a non-default branch since GitHub doesn't close them #4531 |
| 36 | // 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 |
| 37 | for (const [line] of message.matchAll(/(?:fix|fixe|close|resolve)(?:s|d)?\s+(?:#\d+|https?:\S+)/gi)) { |
| 38 | preservedContent.add(line); |
| 39 | } |
| 40 | |
| 41 | return [...preservedContent].join('\n'); |
| 42 | } |
no test coverage detected