mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(repos): parse and preserve custom-user SSH remotes (#337)
* fix(repos): parse and preserve custom-user SSH remotes SSH remotes with a non-git user (e.g. company@company.ghe.com:orga/repo.git) were misparsed as GitHub shorthand and stored as https://github.com/company@company.ghe.com:orga/repo. The scp-style matcher hardcoded the git@ user, so custom-user remotes fell through to the owner/repo shorthand branch. SSH URL parsing is now unified in shared/utils/repo.ts and applied across backend and frontend: any user@host:path remote is detected, preserved, and deduplicated correctly. The compare key also converges the scp-with-port and ssh:// spellings, strips embedded credentials, and fails closed on malformed host segments. * fix: apply CodeRabbit auto-fixes Fixed 1 file(s) based on 1 unresolved review comment. Co-authored-by: CodeRabbit <noreply@coderabbit.ai> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: CodeRabbit <noreply@coderabbit.ai>
This commit is contained in:
8 files changed
+257
-67
No files matched your search
+57
-14
@@ -66,12 +66,58 @@ export function getRepoBaseDirectoryName(repo: { localPath: string; branch?: str
|
||||
return repo.localPath
|
||||
}
|
||||
|
||||
export const SCP_STYLE_URL_PATTERN = /^([^@/:]+)@([^/:]+):(.+)$/
|
||||
const SCP_STYLE_URL_WITH_PORT_PATTERN = /^([^@/:]+)@([^/:]+):(\d{1,5})\/(.+\/.+)$/
|
||||
|
||||
export function isScpStyleUrl(url: string): boolean {
|
||||
return SCP_STYLE_URL_PATTERN.test(url.trim())
|
||||
}
|
||||
|
||||
export function isSSHUrl(url: string): boolean {
|
||||
return url.trim().startsWith('ssh://') || isScpStyleUrl(url)
|
||||
}
|
||||
|
||||
export function normalizeSSHUrl(url: string): string {
|
||||
if (url.startsWith('ssh://')) {
|
||||
return url
|
||||
}
|
||||
|
||||
const match = url.match(SCP_STYLE_URL_WITH_PORT_PATTERN)
|
||||
if (match) {
|
||||
const [, user, host, port, path] = match
|
||||
const portNumber = Number(port)
|
||||
if (portNumber > 0 && portNumber <= 65535) {
|
||||
return `ssh://${user}@${host}:${port}/${path}`
|
||||
}
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
export function extractHostFromSSHUrl(url: string): string | null {
|
||||
const scpMatch = url.match(SCP_STYLE_URL_PATTERN)
|
||||
if (scpMatch) {
|
||||
return scpMatch[2] || null
|
||||
}
|
||||
|
||||
if (url.startsWith('ssh://')) {
|
||||
try {
|
||||
const parsed = new URL(url)
|
||||
return parsed.port ? `${parsed.hostname}:${parsed.port}` : parsed.hostname || null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function getRepoNameFromUrl(url: string): string {
|
||||
const cleaned = trimTrailingChar(url.trim().replace(/\.git$/, ''), '/')
|
||||
const scpMatch = cleaned.match(/^git@[^:]+:(.+)$/)
|
||||
const scpMatch = cleaned.match(SCP_STYLE_URL_PATTERN)
|
||||
|
||||
if (scpMatch) {
|
||||
const parts = scpMatch[1]?.split('/') ?? []
|
||||
const parts = scpMatch[3]?.split('/') ?? []
|
||||
return parts[parts.length - 1] || ''
|
||||
}
|
||||
|
||||
@@ -97,19 +143,20 @@ export function getRepoDisplayName(repo: {
|
||||
}
|
||||
|
||||
export function normalizeRepoUrlForCompare(url: string): string {
|
||||
let normalized = trimTrailingChar(url.trim().replace(/\.git$/, ''), '/')
|
||||
const normalized = trimTrailingChar(normalizeSSHUrl(url.trim()).replace(/\.git$/, ''), '/')
|
||||
const scpMatch = normalized.match(SCP_STYLE_URL_PATTERN)
|
||||
|
||||
if (scpMatch) {
|
||||
return `https://${scpMatch[2]}/${scpMatch[3]}`.toLowerCase()
|
||||
}
|
||||
|
||||
const shorthandMatch = normalized.match(/^([^/]+)\/([^/]+)$/)
|
||||
|
||||
if (shorthandMatch && !normalized.includes('://') && !normalized.startsWith('git@')) {
|
||||
if (shorthandMatch && !normalized.includes('://')) {
|
||||
return `https://github.com/${normalized}`.toLowerCase()
|
||||
}
|
||||
|
||||
const scpMatch = normalized.match(/^git@([^:]+):(.+)$/)
|
||||
if (scpMatch) {
|
||||
return `https://${scpMatch[1]}/${scpMatch[2]}`.toLowerCase()
|
||||
}
|
||||
|
||||
if (normalized.startsWith('ssh://')) {
|
||||
if (/^(?:ssh|https?):\/\//.test(normalized)) {
|
||||
try {
|
||||
const parsed = new URL(normalized)
|
||||
const path = parsed.pathname.replace(/^\/+/, '')
|
||||
@@ -120,9 +167,5 @@ export function normalizeRepoUrlForCompare(url: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
if (normalized.startsWith('http://')) {
|
||||
normalized = `https://${normalized.slice(7)}`
|
||||
}
|
||||
|
||||
return normalized.toLowerCase()
|
||||
}
|
||||
Reference in new issue
Block a user