Detect GitHub URL-style SSH remotes (#3605)
This commit is contained in:
@@ -47,6 +47,14 @@ describe('github owner/repo resolution', () => {
|
||||
owner: 'TheBoredTeam',
|
||||
repo: 'boring.notch'
|
||||
})
|
||||
expect(parseGitHubOwnerRepo('ssh://git@github.com/stablyai/orca.git')).toEqual({
|
||||
owner: 'stablyai',
|
||||
repo: 'orca'
|
||||
})
|
||||
expect(parseGitHubOwnerRepo('ssh://git@ssh.github.com:443/stablyai/orca.git')).toEqual({
|
||||
owner: 'stablyai',
|
||||
repo: 'orca'
|
||||
})
|
||||
expect(parseGitHubOwnerRepo('git@example.com:stablyai/orca.git')).toBeNull()
|
||||
})
|
||||
|
||||
|
||||
@@ -159,17 +159,49 @@ export function parseGitHubOwnerRepo(remoteUrl: string): OwnerRepo | null {
|
||||
return { owner: identity.owner, repo: identity.repo }
|
||||
}
|
||||
|
||||
function normalizeGitHubRemoteHost(host: string): string {
|
||||
// Why: GitHub documents ssh.github.com:443 as SSH-over-HTTPS for github.com repos.
|
||||
return host.toLowerCase() === 'ssh.github.com' ? 'github.com' : host
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
export function parseGitHubRemoteIdentity(remoteUrl: string): GitHubRemoteIdentity | null {
|
||||
const trimmed = remoteUrl.trim()
|
||||
const httpsMatch = trimmed.match(/^https?:\/\/([^/]+)\/([^/]+)\/([^/]+?)(?:\.git)?\/?$/i)
|
||||
if (httpsMatch) {
|
||||
return { host: httpsMatch[1], owner: httpsMatch[2], repo: httpsMatch[3] }
|
||||
return {
|
||||
host: normalizeGitHubRemoteHost(httpsMatch[1]),
|
||||
owner: httpsMatch[2],
|
||||
repo: httpsMatch[3]
|
||||
}
|
||||
}
|
||||
const sshMatch = trimmed.match(/^git@([^:]+):([^/]+)\/([^/]+?)(?:\.git)?$/i)
|
||||
if (sshMatch) {
|
||||
return { host: sshMatch[1], owner: sshMatch[2], repo: sshMatch[3] }
|
||||
return { host: normalizeGitHubRemoteHost(sshMatch[1]), owner: sshMatch[2], repo: sshMatch[3] }
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(trimmed)
|
||||
if (!['git:', 'git+ssh:', 'ssh:'].includes(url.protocol.toLowerCase())) {
|
||||
return null
|
||||
}
|
||||
const path = parseGitHubRemotePath(url.pathname)
|
||||
return path ? { host: normalizeGitHubRemoteHost(url.hostname), ...path } : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export async function getRemoteUrlForRepo(
|
||||
|
||||
Reference in New Issue
Block a user