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`.
This commit is contained in:
silverwind authored and GitHub committed 2026-11-04 19:40:27 +00:00
1 parent d1c980cf8b
commit dfe6db6149
3 files changed
+18 -18

No files matched your search

+6 -5
View File
@@ -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<IssueInfo> {
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<HTMLAnchorElement, MouseEvent>(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');
+3 -10
View File
@@ -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();
});
+9 -3
View File
@@ -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};