Files
orca/src/main/github/github-remote-identity-parsing.ts
T
OrcaWinandOrcaWin 4687e90a16 [comment audit] Continue main-process comment cleanup (#32946)
* [comment audit] Shorten daemon, hook, and emulator comments [skip ci]

* [comment audit] Shorten VM, conversation, SQLite, and theme-import comments [skip ci]

* [comment audit] Tighten Git execution and source-control comment contracts [skip ci]

* [comment audit] Clarify repository probes and maintenance comments [skip ci]

* [comment audit] Shorten status, diff-cache, and checkout comments [skip ci]

* [comment audit] Clarify worktree lifecycle and WSL routing comments [skip ci]

* [comment audit] Shorten forge identity and review-refresh comments [skip ci]

* [comment audit] Tighten hosted-review client comment contracts [skip ci]

* [comment audit] Clarify GitLab compatibility and review comments [skip ci]

* [comment audit] Tighten hook ownership and watchdog rationale [skip ci]

* [comment audit] Condense host and integration compatibility comments [skip ci]

* [comment audit] Clarify IPC authority and remote-routing rationale [skip ci]

* [comment audit] Finalize file-import and containment comment cleanup

* [comment audit] Fix stale Jira type imports blocking full CI

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-23 16:27:37 -07:00

128 lines
4.1 KiB
TypeScript

import { normalizeGitHubRemoteHost } from '../../shared/git-remote-host-alias'
import type { GitHubOwnerRepo } from '../../shared/github/pull-request-types'
export type GitHubRemoteIdentity = GitHubOwnerRepo & { host: string }
// HTTP ports identify API endpoints; SSH/git ports are transport-only.
function hostFromRemoteUrl(url: URL): string {
const protocol = url.protocol.toLowerCase()
return protocol === 'http:' || protocol === 'https:' ? url.host : url.hostname
}
function parseGitHubRemotePath(path: string): Pick<GitHubRemoteIdentity, 'owner' | 'repo'> | null {
const parts = path.replace(/^\/+/, '').replace(/\/+$/, '').split('/')
if (parts.length !== 2) {
return null
}
const [owner, repoWithSuffix] = parts
const repo = repoWithSuffix.replace(/\.git$/i, '')
if (!owner || !repo) {
return null
}
return { owner, repo }
}
/** SCP-style / ssh:// / git+ssh:// remotes may use an OpenSSH Host alias. */
export function remoteUrlUsesSshTransport(remoteUrl: string): boolean {
const trimmed = remoteUrl.trim().toLowerCase()
return (
isScpStyleRemote(trimmed) || trimmed.startsWith('ssh://') || trimmed.startsWith('git+ssh://')
)
}
/** GitHub SSH certificate users can be org-<id>, not just git. */
function isScpStyleRemote(remoteUrl: string): boolean {
return /^[^@\s/:]+@[^:\s/]+:[^\s]+$/.test(remoteUrl)
}
/** SSH transport host as written, preserving SCP alias case. */
export function rawSshTransportHost(remoteUrl: string): string | null {
const trimmed = remoteUrl.trim()
const scpMatch = trimmed.match(/^[^@\s/:]+@([^:]+):/i)
if (scpMatch) {
return scpMatch[1]
}
try {
const url = new URL(trimmed)
if (!['ssh:', 'git+ssh:'].includes(url.protocol.toLowerCase())) {
return null
}
return url.hostname || null
} catch {
return null
}
}
/** Non-GitHub SSH host that may need OpenSSH alias expansion. */
export function gitHubSshConfigHostAlias(remoteUrl: string): string | null {
if (!remoteUrlUsesSshTransport(remoteUrl)) {
return null
}
const identity = parseGitHubRemoteIdentity(remoteUrl)
if (!identity || identity.host === 'github.com') {
return null
}
return rawSshTransportHost(remoteUrl) ?? identity.host
}
export function parseGitHubRemoteIdentity(remoteUrl: string): GitHubRemoteIdentity | null {
const trimmed = remoteUrl.trim()
const sshMatch = trimmed.match(/^[^@\s/:]+@([^:]+):([^/]+)\/([^/]+?)(?:\.git)?$/i)
if (sshMatch) {
return { host: normalizeGitHubRemoteHost(sshMatch[1]), owner: sshMatch[2], repo: sshMatch[3] }
}
try {
const url = new URL(trimmed)
if (!['git:', 'git+ssh:', 'http:', 'https:', 'ssh:'].includes(url.protocol.toLowerCase())) {
return null
}
const path = parseGitHubRemotePath(url.pathname)
return path ? { host: normalizeGitHubRemoteHost(hostFromRemoteUrl(url)), ...path } : null
} catch {
return null
}
}
export function parseGitHubOwnerRepo(remoteUrl: string): GitHubOwnerRepo | null {
const identity = parseGitHubRemoteIdentity(remoteUrl)
if (!identity || identity.host.toLowerCase() !== 'github.com') {
return null
}
return { owner: identity.owner, repo: identity.repo }
}
/** Parse github.com identity using an expanded SSH HostName. */
export function parseGitHubOwnerRepoWithResolvedSshHostname(
remoteUrl: string,
resolvedSshHostname: string | null | undefined
): GitHubOwnerRepo | null {
const direct = parseGitHubOwnerRepo(remoteUrl)
if (direct) {
return direct
}
if (!remoteUrlUsesSshTransport(remoteUrl)) {
return null
}
if (!resolvedSshHostname?.trim()) {
return null
}
const identity = parseGitHubRemoteIdentity(remoteUrl)
if (!identity) {
return null
}
if (normalizeGitHubRemoteHost(resolvedSshHostname.trim()) !== 'github.com') {
return null
}
return { owner: identity.owner, repo: identity.repo }
}
/** Effective forge host after optional SSH config HostName expansion. */
export function effectiveGitHubRemoteHost(
parsedHost: string,
resolvedSshHostname?: string | null
): string {
const candidate = resolvedSshHostname?.trim() || parsedHost
return normalizeGitHubRemoteHost(candidate)
}