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 pr-base-commit style on non-mergeable PRs #6703

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions source/features/pr-base-commit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pluralize from '../helpers/pluralize.js';
import {buildRepoURL} from '../github-helpers/index.js';
import {linkifyCommit} from '../github-helpers/dom-formatters.js';
import {removeTextNodeContaining} from '../helpers/dom-utils.js';
import {isTextNodeContaining} from '../helpers/dom-utils.js';

function getBaseCommitNotice(prInfo: PullRequestInfo): JSX.Element {
const {base} = getBranches();
Expand All @@ -23,7 +23,7 @@
</a>
);
return (
<>It’s {countLink} behind (base commit: {commit})</>
<div>It’s {countLink} behind (base commit: {commit})</div>
);
}

Expand All @@ -43,7 +43,9 @@

const previousMessage = statusMeta.firstChild!; // Extract now because it won't be the first child anymore
statusMeta.prepend(getBaseCommitNotice(prInfo));
removeTextNodeContaining(previousMessage, 'Merging can be performed automatically.');
if (isTextNodeContaining(previousMessage, 'Merging can be performed automatically.')) {
previousMessage.remove();
}
}

async function init(signal: AbortSignal): Promise<false | void> {
Expand Down Expand Up @@ -82,4 +84,4 @@
Cross-repo PR with long branch names
https://github.com/refined-github/sandbox/pull/13

*/

Check notice on line 87 in source/features/pr-base-commit.tsx

View workflow job for this annotation

GitHub Actions / TestURLs

18 changes: 12 additions & 6 deletions source/helpers/dom-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import select from 'select-dom';
import {setFetch} from 'push-form';
// Nodes may be exactly `null`
import {type Nullable} from 'vitest';

// `content.fetch` is Firefox’s way to make fetches from the page instead of from a different context
// This will set the correct `origin` header without having to use XMLHttpRequest
Expand Down Expand Up @@ -84,21 +86,25 @@ const escapeMatcher = (matcher: RegExp | string): string =>
const isTextNode = (node: Text | ChildNode): boolean =>
node instanceof Text || ([...node.childNodes].every(childNode => childNode instanceof Text));

// eslint-disable-next-line @typescript-eslint/ban-types -- Nodes may be exactly `null`
export const assertNodeContent = <N extends Text | ChildNode>(node: N | null, expectation: RegExp | string): N => {
export const isTextNodeContaining = (node: Nullable<Text | ChildNode>, expectation: RegExp | string): boolean => {
// Make sure only text is being considered, not links, icons, etc
if (!node || !isTextNode(node)) {
console.warn('TypeError', node);
throw new TypeError(`Expected Text node, received ${String(node?.nodeName)}`);
}

const content = node.textContent!.trim();
if (!matchString(expectation, content)) {
console.warn('Error', node.parentElement);
throw new Error(`Expected node matching ${escapeMatcher(expectation)}, found ${escapeMatcher(content)}`);
return matchString(expectation, content);
};

export const assertNodeContent = <N extends Text | ChildNode>(node: Nullable<N>, expectation: RegExp | string): N => {
if (isTextNodeContaining(node, expectation)) {
return node!;
}

return node;
console.warn('Error', node!.parentElement);
const content = node!.textContent!.trim();
throw new Error(`Expected node matching ${escapeMatcher(expectation)}, found ${escapeMatcher(content)}`);
};

export const removeTextNodeContaining = (node: Text | ChildNode, expectation: RegExp | string): void => {
Expand Down