Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear-pr-merge-commit-message preserve closing issues on non-default branch #6122

Merged
merged 12 commits into from
Nov 1, 2022
23 changes: 19 additions & 4 deletions source/features/clear-pr-merge-commit-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@ import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager';
import {getRepo} from '../github-helpers';
import {getBranches} from './update-pr-from-base-branch';
import getDefaultBranch from '../github-helpers/get-default-branch';
import onPrMergePanelOpen from '../github-events/on-pr-merge-panel-open';

function init(): void | false {
async function init(): Promise<void | false> {
const messageField = select('textarea#merge_message_field')!;
const originalMessage = messageField.value;
const deduplicatedAuthors = new Set();
const preservedContent = new Set();

// This method ensures that "Co-authored-by" capitalization doesn't affect deduplication
for (const [, author] of originalMessage.matchAll(/co-authored-by: ([^\n]+)/gi)) {
deduplicatedAuthors.add('Co-authored-by: ' + author);
preservedContent.add('Co-authored-by: ' + author);
}

const cleanedMessage = [...deduplicatedAuthors].join('\n');
// Preserve closing issues numbers when a PR is merged into a non-default branch since GitHub doesn't close them #4531
if (getBranches().head !== await getDefaultBranch()) {
yakov116 marked this conversation as resolved.
Show resolved Hide resolved
for (const keyword of select.all('.comment-body .issue-keyword[aria-label^="This pull request closes"]')) {
const closingKeyword = keyword.textContent!.trim(); // Keep the keyword as-is (closes, fixes, etc.)

const issueNumberElement = keyword.nextElementSibling as HTMLAnchorElement;
const isCrossRepo = getRepo(issueNumberElement)!.nameWithOwner !== getRepo()!.nameWithOwner;
const issueNumber = isCrossRepo ? issueNumberElement.href : issueNumberElement.textContent!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we just get the whole URL? fixes https://GitHub.com/user/repo/issues/123

It works regardless of the repo. I'm not particularly worried about this scenario to be honest because the users who want this specific behavior can just disable the feature and user GitHub's own repo config.

Fixing it is ok, but having a clean fixes #124 isn't worth the complexity here.

preservedContent.add(closingKeyword + ' ' + issueNumber);
}
}

const cleanedMessage = [...preservedContent].join('\n');
if (cleanedMessage === originalMessage.trim()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion source/features/update-pr-from-base-branch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {getConversationNumber} from '../github-helpers';

const selectorForPushablePRNotice = '.merge-pr > .color-fg-muted:first-child';

function getBranches(): {base: string; head: string} {
export function getBranches(): {base: string; head: string} {
return {
base: select('.base-ref')!.textContent!.trim(),
head: select('.head-ref')!.textContent!.trim(),
Expand Down