From 22855251f933e185c37e774e74e92aafcb4234a3 Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Fri, 13 Nov 2026 22:58:07 +0100 Subject: [PATCH] fix(github): find a pasted issue or PR URL in the From GitHub picker GitHub's search does not match an item's URL, so pasting a link into the picker found nothing while its bare number worked. Search by the URL's number instead, and keep only the item the URL names, so text matches of that number and URLs of other repositories list nothing. Co-Authored-By: Claude Opus 5.5 --- electron/github/work-items.test.ts | 67 +++++++++++++++++++++++++++++- electron/github/work-items.ts | 41 +++++++++++++++++- 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/electron/github/work-items.test.ts b/electron/github/work-items.test.ts index 306105ad6..6958d45e6 100644 --- a/electron/github/work-items.test.ts +++ b/electron/github/work-items.test.ts @@ -1,5 +1,68 @@ -import { describe, expect, it } from 'vitest'; -import { parseWorkItems, scopeToRepo } from './work-items.js'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { runGhJson } from './gh.js'; +import { + listGitHubWorkItems, + parseWorkItemRef, + parseWorkItems, + scopeToRepo, +} from './work-items.js'; + +vi.mock('./gh.js', async (original) => ({ + ...(await original()), + runGhJson: vi.fn(), +})); + +const item = (number: number, url: string) => ({ + number, + title: `Item ${number}`, + url, + updatedAt: '2026-10-01T00:00:00Z', + author: { login: 'dev' }, + labels: [], +}); + +describe('listGitHubWorkItems', () => { + beforeEach(() => vi.mocked(runGhJson).mockReset()); + + it('finds a pasted issue or PR URL through its number', async () => { + vi.mocked(runGhJson) + .mockResolvedValueOnce([item(120, 'https://github.com/o/r/issues/120')]) + .mockResolvedValueOnce([ + item(12, 'https://github.com/o/r/pull/12'), + item(13, 'https://github.com/o/r/pull/13'), + ]); + const items = await listGitHubWorkItems('/p', 'https://github.com/O/R/pull/12/files'); + expect(items.map((i) => i.number)).toEqual([12]); + for (const [args] of vi.mocked(runGhJson).mock.calls) { + expect(args[args.indexOf('--search') + 1]).toBe('12'); + } + }); + + it('lists nothing for a URL of another repository', async () => { + vi.mocked(runGhJson) + .mockResolvedValueOnce([item(12, 'https://github.com/o/r/issues/12')]) + .mockResolvedValueOnce([]); + expect(await listGitHubWorkItems('/p', 'https://github.com/other/r/issues/12')).toEqual([]); + }); +}); + +describe('parseWorkItemRef', () => { + it('reads issue and PR URLs, with or without a scheme', () => { + const ref = { host: 'github.com', repo: 'o/r', number: 12 }; + expect(parseWorkItemRef('https://github.com/o/r/issues/12')).toEqual(ref); + expect(parseWorkItemRef(' https://www.github.com/o/r/pull/12#discussion_r1 ')).toEqual(ref); + expect(parseWorkItemRef('github.com/o/r/pull/12/files')).toEqual(ref); + expect(parseWorkItemRef('https://ghe.example.com/o/r/issues/12')?.host).toBe('ghe.example.com'); + }); + + it('leaves ordinary searches alone', () => { + expect(parseWorkItemRef('12')).toBeNull(); + expect(parseWorkItemRef('crash on save')).toBeNull(); + expect(parseWorkItemRef('https://github.com/o/r')).toBeNull(); + expect(parseWorkItemRef('https://github.com/o/r/issues/new')).toBeNull(); + expect(parseWorkItemRef('https://u:p@github.com/o/r/issues/12')).toBeNull(); + }); +}); describe('scopeToRepo', () => { it('strips qualifiers that widen the search beyond the project repo', () => { diff --git a/electron/github/work-items.ts b/electron/github/work-items.ts index 8bb22dfbc..e5a03e626 100644 --- a/electron/github/work-items.ts +++ b/electron/github/work-items.ts @@ -12,7 +12,9 @@ export async function listGitHubWorkItems( search?: string, ): Promise { const common = ['--state', 'open', '--limit', String(LIST_LIMIT)]; - const query = search ? scopeToRepo(search) : ''; + // GitHub's search does not match an item's URL, but it does match its number. + const ref = search ? parseWorkItemRef(search) : null; + const query = ref ? String(ref.number) : search ? scopeToRepo(search) : ''; if (query) common.push('--search', query); const [issues, prs] = await Promise.all([ runGhJson( @@ -30,9 +32,44 @@ export async function listGitHubWorkItems( projectRoot, ), ]); - return [...parseWorkItems(issues, 'issue'), ...parseWorkItems(prs, 'pr')].sort((a, b) => + const items = [...parseWorkItems(issues, 'issue'), ...parseWorkItems(prs, 'pr')].sort((a, b) => b.updatedAt.localeCompare(a.updatedAt), ); + // The number also matches text; keep only the pasted item, and nothing when + // the URL names another repository. + return ref ? items.filter((item) => sameWorkItem(parseWorkItemRef(item.url), ref)) : items; +} + +export interface WorkItemRef { + host: string; + repo: string; + number: number; +} + +/** Reads an issue or PR URL (`…/owner/repo/issues/12`, `…/pull/12/files`, with or + * without `https://`); null for anything else. */ +export function parseWorkItemRef(text: string): WorkItemRef | null { + const trimmed = text.trim(); + if (/\s/.test(trimmed)) return null; + let url: URL; + try { + url = new URL(/^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`); + } catch { + return null; + } + if (url.username || url.password) return null; + const match = /^\/([\w.-]+)\/([\w.-]+)\/(?:issues|pull)\/(\d+)(?:\/|$)/.exec(url.pathname); + if (!match) return null; + return { + host: url.host.toLowerCase().replace(/^www\./, ''), + repo: `${match[1]}/${match[2]}`.toLowerCase(), + number: Number(match[3]), + }; +} + +/** GitHub serves a PR under `/issues/N` too, so the kind in the path does not count. */ +function sameWorkItem(a: WorkItemRef | null, b: WorkItemRef): boolean { + return !!a && a.host === b.host && a.repo === b.repo && a.number === b.number; } /** Drops qualifiers that would list another repo's items: a picked PR is