From dfe6db61490cffc962575aca27e8ffa5f4e38c6c Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 4 Nov 2026 19:40:27 +0000 Subject: [PATCH] fix(web): build issue popup request from parsed link (#40207) The issue reference popup built its `/info` request from the raw link path, so a link ending in `/owner/repo/issues/N` queried an unrelated location. It now builds the request from the owner, repo, type and index parsed from the link, with `parseIssueHref` anchored to the pathname under `appSubUrl`. --- web_src/js/features/ref-issue.ts | 11 ++++++----- web_src/js/utils.test.ts | 13 +++---------- web_src/js/utils.ts | 12 +++++++++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/web_src/js/features/ref-issue.ts b/web_src/js/features/ref-issue.ts index 3abb2f43823..9143f990577 100644 --- a/web_src/js/features/ref-issue.ts +++ b/web_src/js/features/ref-issue.ts @@ -3,7 +3,7 @@ import {GET} from '../modules/fetch.ts'; import {createApp} from 'vue'; import {createTippy, getAttachedTippyInstance} from '../modules/tippy.ts'; import {addDelegatedEventListener} from '../utils/dom.ts'; -import type {Issue, TimeoutId} from '../types.ts'; +import type {Issue, IssuePathInfo, TimeoutId} from '../types.ts'; type IssueInfo = { convertedIssue: Issue, @@ -21,9 +21,9 @@ async function getIssueInfo(url: string): Promise { return data; } -async function showRefIssuePopup(link: HTMLAnchorElement) { +async function showRefIssuePopup(link: HTMLAnchorElement, info: IssuePathInfo) { const [data, {default: ContextPopup}] = await Promise.all([ - getIssueInfo(`${link.pathname}/info`), + getIssueInfo(`${window.config.appSubUrl}/${info.ownerName}/${info.repoName}/${info.pathType}/${info.indexString}/info`), import('../components/ContextPopup.vue'), ]); const el = document.createElement('div'); @@ -49,7 +49,8 @@ async function showRefIssuePopup(link: HTMLAnchorElement) { export function initRefIssueContextPopup() { const selector = 'a[href]:not([data-ref-issue-popup]):not(.ref-external-issue)'; addDelegatedEventListener(document, 'mouseover', selector, (link) => { - if (!parseIssueHref(link.getAttribute('href')!)) return; + const issuePathInfo = parseIssueHref(link.getAttribute('href')!); + if (!issuePathInfo) return; if (!link.classList.contains('ref-issue') && !link.closest('[data-ref-issue-container]')) return; if (getAttachedTippyInstance(link)) return; link.setAttribute('data-ref-issue-popup', ''); @@ -64,7 +65,7 @@ export function initRefIssueContextPopup() { timer = setTimeout(async () => { link.removeEventListener('mouseleave', cancel); try { - await showRefIssuePopup(link); + await showRefIssuePopup(link, issuePathInfo); } catch (err) { console.error('Failed to load issue info:', err); link.removeAttribute('data-ref-issue-popup'); diff --git a/web_src/js/utils.test.ts b/web_src/js/utils.test.ts index 8c0a6c1318f..fa3435a0936 100644 --- a/web_src/js/utils.test.ts +++ b/web_src/js/utils.test.ts @@ -40,17 +40,10 @@ test('parseIssueHref', () => { expect(parseIssueHref('/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); expect(parseIssueHref('/owner/repo/pulls/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'}); expect(parseIssueHref('/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); - expect(parseIssueHref('/sub/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); - expect(parseIssueHref('/sub/sub2/owner/repo/pulls/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'}); - expect(parseIssueHref('/sub/sub2/owner/repo/issues/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); - expect(parseIssueHref('/sub/sub2/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); + expect(parseIssueHref('/owner/repo/pulls/1/files')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'}); expect(parseIssueHref('https://example.com/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); - expect(parseIssueHref('https://example.com/owner/repo/pulls/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'}); - expect(parseIssueHref('https://example.com/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); - expect(parseIssueHref('https://example.com/sub/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); - expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/pulls/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'}); - expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); - expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'}); + expect(parseIssueHref('/owner/repo/raw/branch/main/sub/dir/issues/1')).toBeNull(); + expect(parseIssueHref('/sub/owner/repo/issues/1')).toBeNull(); expect(parseIssueHref('')).toBeNull(); }); diff --git a/web_src/js/utils.ts b/web_src/js/utils.ts index b2a4427bc6b..0b2a43ab354 100644 --- a/web_src/js/utils.ts +++ b/web_src/js/utils.ts @@ -50,9 +50,15 @@ export function stripTags(text: string): string { } export function parseIssueHref(href: string): IssuePathInfo | null { - // FIXME: it should use pathname and trim the appSubUrl ahead - const path = (href || '').replace(/[#?].*$/, ''); - const match = /([^/]+)\/([^/]+)\/(issues|pulls)\/([0-9]+)/.exec(path); + const {appSubUrl} = window.config; + let pathname: string; + try { + pathname = new URL(href || '', window.location.href).pathname; + } catch { + return null; + } + if (!pathname.startsWith(appSubUrl)) return null; + const match = /^\/([-.\w]+)\/([-.\w]+)\/(issues|pulls)\/([0-9]+)(?:\/|$)/.exec(pathname.slice(appSubUrl.length)); if (!match) return null; const [, ownerName, repoName, pathType, indexString] = match; return {ownerName, repoName, pathType, indexString};