From bb3cd7966666c9120ce284de6cefdf74ba1de44c Mon Sep 17 00:00:00 2001 From: Federico Date: Sun, 9 Nov 2025 16:34:04 +0700 Subject: [PATCH] `pr-first-commit-title` - Fix feature on long commit lists --- source/features/pr-first-commit-title.tsx | 39 ++++++++++++----------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/source/features/pr-first-commit-title.tsx b/source/features/pr-first-commit-title.tsx index 392f34b33f60..c1278dad29e3 100644 --- a/source/features/pr-first-commit-title.tsx +++ b/source/features/pr-first-commit-title.tsx @@ -1,11 +1,11 @@ import {elementExists} from 'select-dom'; import {$, $optional} from 'select-dom/strict.js'; -import elementReady from 'element-ready'; import * as pageDetect from 'github-url-detection'; import {insertTextIntoField, setFieldText} from 'text-field-edit'; import features from '../feature-manager.js'; import looseParseInt from '../helpers/loose-parse-int.js'; +import observe from '../helpers/selector-observer.js'; function interpretNode(node: ChildNode): string | void { switch (node instanceof Element && node.tagName) { @@ -24,14 +24,12 @@ function interpretNode(node: ChildNode): string | void { } } -function getFirstCommit(): {title: string; body: string | undefined} { - const titleParts = $('.js-commits-list-item:first-child p').childNodes; - - const body = $optional('.js-commits-list-item:first-child .Details-content--hidden pre') +function getFirstCommit(firstCommit: HTMLElement): {title: string; body: string | undefined} { + const body = $optional('.Details-content--hidden pre', firstCommit) ?.textContent .trim() ?? undefined; - const title = [...titleParts] + const title = [...firstCommit.childNodes] .map(node => interpretNode(node)) .join('') .trim(); @@ -39,23 +37,16 @@ function getFirstCommit(): {title: string; body: string | undefined} { return {title, body}; } -async function init(): Promise { - const sessionResumeId = $optional('meta[name="session-resume-id"]')?.content; - const previousTitle = sessionResumeId && sessionStorage.getItem(`session-resume:${sessionResumeId}`); - if (previousTitle) { - // The user already altered the PR title/body in a previous load, don't overwrite it - // https://github.com/refined-github/refined-github/issues/7191 - return false; - } - +function useCommitTitle(firstCommitElement: HTMLElement): void { const requestedContent = new URL(location.href).searchParams; - const commitCountIcon = await elementReady('div.Box.mb-3 .octicon-git-commit'); + const commitCountIcon = $(['div.Box.mb-3 .octicon-git-commit', 'a[href="#commits_bucket"] .octicon-git-commit']); const commitCount = commitCountIcon?.nextElementSibling; if (!commitCount || looseParseInt(commitCount) < 2 || !elementExists('#new_pull_request')) { - return false; + return; } - const firstCommit = getFirstCommit(); + const firstCommit = getFirstCommit(firstCommitElement); + if (!requestedContent.has('pull_request[title]')) { setFieldText( $('#pull_request_title'), @@ -71,12 +62,24 @@ async function init(): Promise { } } +function init(signal: AbortSignal): void { + observe('#commits_bucket > :first-child .js-commits-list-item:first-child p', useCommitTitle, {signal}); +} + +// The user already altered the PR title/body in a previous load, don't overwrite it +// https://github.com/refined-github/refined-github/issues/7191 +function hasUserAlteredThePR(): boolean { + const sessionResumeId = $optional('meta[name="session-resume-id"]')?.content; + return Boolean(sessionResumeId && sessionStorage.getItem(`session-resume:${sessionResumeId}`)); +} + void features.add(import.meta.url, { include: [ pageDetect.isCompare, ], exclude: [ () => new URLSearchParams(location.search).has('title'), + hasUserAlteredThePR, ], deduplicate: 'has-rgh', init,