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

Fix "closing keyword" logic in clear-pr-merge-commit-message #6328

Merged
merged 10 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"regex-join": "^2.0.0",
"select-dom": "^7.1.1",
"shorten-repo-url": "^3.0.0",
"split-on-first": "^3.0.0",
"strip-indent": "^4.0.0",
"text-field-edit": "^3.1.9001",
"tiny-version-compare": "^4.0.0",
Expand Down
8 changes: 5 additions & 3 deletions source/features/clear-pr-merge-commit-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {set} from 'text-field-edit';
import * as pageDetect from 'github-url-detection';

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

Expand All @@ -19,10 +19,12 @@ async function init(): Promise<void | false> {
}

// Preserve closing issues numbers when a PR is merged into a non-default branch since GitHub doesn't close them #4531
if (getBranches().base !== await getDefaultBranch()) {
if (getBranches().base.branch !== await getDefaultBranch()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

On cross-repo PRs, this comparison was between "user:branch" and "branch", so it would never match.

This caused #6122 to appear on every cross-repo PR instead of just non-default-branch PRs

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

@fregante fregante Feb 10, 2023

Choose a reason for hiding this comment

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

What about it? The review that prompted this change was #6122 (comment), which was correct. It's 4a1a31d (#6122) that introduced the bug

Copy link
Member

Choose a reason for hiding this comment

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

Oh I though you meant something else with your comment.
Never mind

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 issueLink = keyword.nextElementSibling as HTMLAnchorElement; // Account for issues not in the same repo
const sibling = keyword.nextElementSibling!;
// Get the full URL so it works on issues not in the same repo
const issueLink = sibling instanceof HTMLAnchorElement ? sibling : select('a', sibling)!;
Copy link
Member Author

Choose a reason for hiding this comment

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

sibling became a span

Before

Screenshot 2

After

Screenshot 4

preservedContent.add(closingKeyword + ' ' + issueLink.href);
}
}
Expand Down
17 changes: 8 additions & 9 deletions source/features/update-pr-from-base-branch.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import React from 'dom-chef';
import select from 'select-dom';

import {AlertIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';
import delegate, {DelegateEvent} from 'delegate-it';

import features from '../feature-manager';
import observe from '../helpers/selector-observer';
import * as api from '../github-helpers/api';
import {getBranches} from '../github-helpers/pr-branches';
import getPrInfo from '../github-helpers/get-pr-info';
import {getConversationNumber} from '../github-helpers';

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

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

async function mergeBranches(): Promise<AnyObject> {
return api.v3(`pulls/${getConversationNumber()!}/update-branch`, {
method: 'PUT',
Expand All @@ -28,7 +23,7 @@ async function mergeBranches(): Promise<AnyObject> {

async function handler({delegateTarget}: DelegateEvent): Promise<void> {
const {base, head} = getBranches();
if (!confirm(`Merge the ${base} branch into ${head}?`)) {
if (!confirm(`Merge the ${base.local} branch into ${head.local}?`)) {
return;
}

Expand All @@ -48,7 +43,7 @@ async function handler({delegateTarget}: DelegateEvent): Promise<void> {

async function addButton(position: Element): Promise<void> {
const {base, head} = getBranches();
const prInfo = await getPrInfo(base, head);
const prInfo = await getPrInfo(base.local, head.local);
if (!prInfo) {
return;
}
Expand Down Expand Up @@ -92,4 +87,8 @@ https://github.com/refined-github/sandbox/pull/11

Native "Resolve conflicts" button
https://github.com/refined-github/sandbox/pull/9

Cross-repo PR with long branch names
https://github.com/refined-github/sandbox/pull/13

*/
34 changes: 34 additions & 0 deletions source/github-helpers/pr-branches.ts
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')!),
};
}