diff --git a/src/renderer/src/lib/github-links.test.ts b/src/renderer/src/lib/github-links.test.ts index 8928674ed..eeb4d90cd 100644 --- a/src/renderer/src/lib/github-links.test.ts +++ b/src/renderer/src/lib/github-links.test.ts @@ -26,6 +26,9 @@ describe('parseGitHubIssueOrPRNumber', () => { expect(parseGitHubIssueOrPRNumber('#42')).toBe(42) expect(parseGitHubIssueOrPRNumber('https://github.com/stablyai/orca/pull/123')).toBe(123) expect(parseGitHubIssueOrPRNumber('https://github.com/stablyai/orca/issues/923')).toBe(923) + expect(parseGitHubIssueOrPRNumber('https://github.my-company.net/MyOrg/my_repo/pull/395')).toBe( + 395 + ) }) it('parses GitHub item URLs with trailing page segments', () => { @@ -47,10 +50,6 @@ describe('parseGitHubIssueOrPRNumber', () => { }) it('rejects invalid GitHub item URLs', () => { - expect(parseGitHubIssueOrPRNumber('https://example.com/stablyai/orca/pull/123')).toBeNull() - expect( - parseGitHubIssueOrPRNumber('https://github.example.com/stablyai/orca/pull/123') - ).toBeNull() expect( parseGitHubIssueOrPRNumber('https://github.com/o/r/pull/not-a-number/changes') ).toBeNull() @@ -67,6 +66,20 @@ describe('parseGitHubIssueOrPRLink', () => { number: 123, type: 'pr' }) + + expect( + parseGitHubIssueOrPRLink('https://github.my-company.net/MyOrg/my_repo/pull/395') + ).toEqual({ + slug: { owner: 'MyOrg', repo: 'my_repo' }, + number: 395, + type: 'pr' + }) + + expect(parseGitHubIssueOrPRLink('https://git.corp.com/MyOrg/my_repo/pull/395')).toEqual({ + slug: { owner: 'MyOrg', repo: 'my_repo' }, + number: 395, + type: 'pr' + }) expect(parseGitHubIssueOrPRLink('https://github.com/stablyai/orca/issues/923')).toEqual({ slug: { owner: 'stablyai', repo: 'orca' }, number: 923, @@ -103,7 +116,6 @@ describe('parseGitHubIssueOrPRLink', () => { }) it('rejects non-GitHub and malformed item URLs', () => { - expect(parseGitHubIssueOrPRLink('https://example.com/o/r/pull/1965/changes')).toBeNull() expect(parseGitHubIssueOrPRLink('https://github.com/o/r/pull/not-a-number/changes')).toBeNull() expect(parseGitHubIssueOrPRLink('https://github.com/o/r/pull/')).toBeNull() expect(parseGitHubIssueOrPRLink('https://github.com/o/r/issues/123abc')).toBeNull() diff --git a/src/renderer/src/lib/github-links.ts b/src/renderer/src/lib/github-links.ts index cc379cc99..3ab7a4279 100644 --- a/src/renderer/src/lib/github-links.ts +++ b/src/renderer/src/lib/github-links.ts @@ -43,7 +43,7 @@ export function parseGitHubIssueOrPRNumber(input: string): number | null { return null } - if (!/^(?:www\.)?github\.com$/i.test(url.hostname)) { + if (url.protocol !== 'https:' && url.protocol !== 'http:') { return null } @@ -57,7 +57,7 @@ export function parseGitHubIssueOrPRNumber(input: string): number | null { /** * Parses an owner/repo slug plus issue/PR number from a GitHub URL. Returns - * null for anything that isn't a recognizable github.com issue or pull URL. + * null for anything that isn't a recognizable GitHub-shaped issue or pull URL. */ export function parseGitHubIssueOrPRLink(input: string): { slug: RepoSlug @@ -76,7 +76,7 @@ export function parseGitHubIssueOrPRLink(input: string): { return null } - if (!/^(?:www\.)?github\.com$/i.test(url.hostname)) { + if (url.protocol !== 'https:' && url.protocol !== 'http:') { return null } @@ -112,7 +112,7 @@ export function normalizeGitHubLinkQuery(raw: string): GitHubLinkQuery { return { query: trimmed, directNumber: null } } - // Why: any github.com issue/pull URL is accepted by number regardless of + // Why: any GitHub-shaped issue/pull URL is accepted by number regardless of // slug, since fork checkouts can legitimately target upstream issues whose // slug differs from the origin remote. return { diff --git a/src/renderer/src/lib/terminal-github-pr-link-detector.test.ts b/src/renderer/src/lib/terminal-github-pr-link-detector.test.ts index cc6b74ee6..c43919f9d 100644 --- a/src/renderer/src/lib/terminal-github-pr-link-detector.test.ts +++ b/src/renderer/src/lib/terminal-github-pr-link-detector.test.ts @@ -55,13 +55,45 @@ describe('createTerminalGitHubPRLinkDetector', () => { expect(observe('more output\n')).toEqual([]) }) - it('ignores non-PR and non-GitHub links', () => { + it('ignores non-PR GitHub-shaped links', () => { const observe = createTerminalGitHubPRLinkDetector() - expect( - observe( - 'https://github.com/acme/orca/issues/42 https://github.example.com/acme/orca/pull/42\n' - ) - ).toEqual([]) + expect(observe('https://github.com/acme/orca/issues/42\n')).toEqual([]) + }) + + it('extracts GitHub Enterprise pull request URLs from terminal output', () => { + const observe = createTerminalGitHubPRLinkDetector() + + expect(observe('Created https://github.my-company.net/MyOrg/my_repo/pull/395\r\n')).toEqual([ + { + url: 'https://github.my-company.net/MyOrg/my_repo/pull/395', + slug: { owner: 'MyOrg', repo: 'my_repo' }, + number: 395 + } + ]) + }) + + it('extracts HTTP GitHub Enterprise pull request URLs from terminal output', () => { + const observe = createTerminalGitHubPRLinkDetector() + + expect(observe('Created http://github.internal/MyOrg/my_repo/pull/395\r\n')).toEqual([ + { + url: 'http://github.internal/MyOrg/my_repo/pull/395', + slug: { owner: 'MyOrg', repo: 'my_repo' }, + number: 395 + } + ]) + }) + + it('extracts GitHub Enterprise pull request URLs with a custom port', () => { + const observe = createTerminalGitHubPRLinkDetector() + + expect(observe('Created https://github.internal:8443/MyOrg/my_repo/pull/397\r\n')).toEqual([ + { + url: 'https://github.internal:8443/MyOrg/my_repo/pull/397', + slug: { owner: 'MyOrg', repo: 'my_repo' }, + number: 397 + } + ]) }) }) diff --git a/src/renderer/src/lib/terminal-github-pr-link-detector.ts b/src/renderer/src/lib/terminal-github-pr-link-detector.ts index c0b226299..c9471b82a 100644 --- a/src/renderer/src/lib/terminal-github-pr-link-detector.ts +++ b/src/renderer/src/lib/terminal-github-pr-link-detector.ts @@ -2,11 +2,9 @@ import type { RepoSlug } from './github-links' import { parseGitHubIssueOrPRLink } from './github-links' const GITHUB_PR_URL_RE = - /\bhttps:\/\/(?:www\.)?github\.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+\/pull\/\d+(?:[/?#][^\s"'<>]*)?/gi -const GITHUB_HOST_MARKER = 'github.com/' + /\bhttps?:\/\/[A-Za-z0-9][A-Za-z0-9_.-]*(?::\d+)?\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+\/pull\/\d+(?:[/?#][^\s"'<>]*)?/gi const GITHUB_PR_PATH_MARKER = '/pull/' -const HTTPS_SCHEME_PREFIX = 'https://' -const HTTPS_SCHEME_FRAGMENT_LAST_CHARS = new Set('https:/'.split('')) +const HTTP_SCHEME_PREFIXES = ['https://', 'http://'] as const const TRAILING_TERMINAL_PUNCTUATION_RE = /[),.;\]}]+$/ const MAX_CARRY_LENGTH = 512 @@ -29,39 +27,25 @@ function parseTerminalGitHubPRUrl(candidate: string): TerminalGitHubPRLink | nul return { url, slug: parsed.slug, number: parsed.number } } -function endsWithHttpsSchemePrefixFragment(value: string): string { - for (let length = Math.min(HTTPS_SCHEME_PREFIX.length - 1, value.length); length > 0; length--) { - if (value.endsWith(HTTPS_SCHEME_PREFIX.slice(0, length))) { - return value.slice(value.length - length) +function endsWithHttpSchemePrefixFragment(value: string): string { + for (const prefix of HTTP_SCHEME_PREFIXES) { + for (let length = Math.min(prefix.length - 1, value.length); length > 0; length--) { + if (value.endsWith(prefix.slice(0, length))) { + return value.slice(value.length - length) + } } } return '' } -function getPotentialGitHubPRCarry(value: string, hasGitHubHost: boolean): string { - if (hasGitHubHost) { - const hostIndex = value.lastIndexOf(GITHUB_HOST_MARKER) - const schemeIndex = value.lastIndexOf('https://', hostIndex) - if (schemeIndex === -1) { - return '' - } - - const tail = value.slice(schemeIndex) - return /\s/.test(tail) ? '' : tail.slice(-MAX_CARRY_LENGTH) - } - - const schemeIndex = value.lastIndexOf('https://') +function getPotentialGitHubPRCarry(value: string): string { + const schemeIndex = Math.max(...HTTP_SCHEME_PREFIXES.map((prefix) => value.lastIndexOf(prefix))) if (schemeIndex !== -1) { const tail = value.slice(schemeIndex) return /\s/.test(tail) ? '' : tail.slice(-MAX_CARRY_LENGTH) } - const lastChar = value.at(-1) - if (!lastChar || !HTTPS_SCHEME_FRAGMENT_LAST_CHARS.has(lastChar)) { - return '' - } - - return endsWithHttpsSchemePrefixFragment(value) + return endsWithHttpSchemePrefixFragment(value) } export function createTerminalGitHubPRLinkDetector(): (data: string) => TerminalGitHubPRLink[] { @@ -70,10 +54,9 @@ export function createTerminalGitHubPRLinkDetector(): (data: string) => Terminal return (data: string): TerminalGitHubPRLink[] => { const combined = carry ? carry + data : data - const hasGitHubHost = combined.includes(GITHUB_HOST_MARKER) - if (!hasGitHubHost || !combined.includes(GITHUB_PR_PATH_MARKER)) { - carry = getPotentialGitHubPRCarry(combined, hasGitHubHost) + if (!combined.includes(GITHUB_PR_PATH_MARKER)) { + carry = getPotentialGitHubPRCarry(combined) return [] } @@ -95,7 +78,7 @@ export function createTerminalGitHubPRLinkDetector(): (data: string) => Terminal links.push(parsed) } - carry = getPotentialGitHubPRCarry(combined, true) + carry = getPotentialGitHubPRCarry(combined) return links } }