refactor(scout): fold github remote parsing into repository

This commit is contained in:
Shoubhit Dash 2026-04-24 19:03:56 +05:30
parent c750df3e86
commit b633a8b1c8
3 changed files with 10 additions and 35 deletions

View file

@ -33,7 +33,7 @@ import { AppRuntime } from "@/effect/app-runtime"
import { Git } from "@/git"
import { setTimeout as sleep } from "node:timers/promises"
import { Process } from "@/util"
import { parseGitHubRemote } from "@/util/github-remote"
import { parseGitHubRemote } from "@/util/repository"
import { Effect } from "effect"
type GitHubAuthor = {

View file

@ -1,34 +0,0 @@
function normalize(input: string) {
return input.trim().replace(/^git\+/, "").replace(/#.*$/, "")
}
export function parseGitHubRemote(url: string): { owner: string; repo: string } | null {
const match = normalize(url).match(/^(?:(?:https?|ssh|git):\/\/)?(?:git@)?github\.com[:/]([^/]+)\/([^/]+?)(?:\.git)?$/)
if (!match) return null
return { owner: match[1], repo: match[2] }
}
export function parseGitHubRepository(input: string): { owner: string; repo: string } | null {
const cleaned = normalize(input)
const remote = parseGitHubRemote(cleaned)
if (remote) return remote
const prefixed = cleaned.match(/^github:([^/\s]+)\/([^/\s]+)$/)
if (prefixed) {
return { owner: prefixed[1], repo: prefixed[2].replace(/\.git$/, "") }
}
const match = cleaned.match(/^([^/\s]+)\/([^/\s]+)$/)
if (!match) return null
return { owner: match[1], repo: match[2].replace(/\.git$/, "") }
}
export function githubRepositoryURL(input: { owner: string; repo: string }) {
return `https://github.com/${input.owner}/${input.repo}`
}
export function githubCloneURL(input: { owner: string; repo: string }) {
const base = process.env.OPENCODE_REPO_CLONE_GITHUB_BASE_URL
if (!base) return `https://github.com/${input.owner}/${input.repo}.git`
return new URL(`${input.owner}/${input.repo}.git`, base.endsWith("/") ? base : `${base}/`).href
}

View file

@ -88,6 +88,15 @@ export function parseRepositoryReference(input: string) {
}
}
export function parseGitHubRemote(input: string) {
const cleaned = normalize(input)
if (!cleaned.includes("://") && !cleaned.match(/^(?:[^@/\s]+@)?github\.com:/)) return null
const parsed = parseRepositoryReference(cleaned)
if (!parsed || parsed.host !== "github.com" || !parsed.owner || parsed.segments.length !== 2) return null
return { owner: parsed.owner, repo: parsed.repo }
}
export function repositoryCachePath(input: Reference) {
return path.join(Global.Path.repos, ...input.host.split(":"), ...input.segments)
}