UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

105 lines (104 loc) • 4.86 kB
import path from "node:path"; import { parseRemoteUrl } from "../bridge/git-hosts.js"; import { git, gitRoot } from "../bridge/projects.js"; import { findNearestPhrenPath, findPhrenPath, projectSlugFromPath, readRootManifest } from "../phren-paths.js"; import { getProjectSourcePath, isGitRepository, projectCheckoutCandidates, readProjectConfig } from "../project-config.js"; import { parseStoreQualified, resolveProject } from "../store-routing.js"; import { isValidProjectName } from "../utils-paths.js"; import { ForgeError } from "./errors.js"; const NAME = /^(?!\.{1,2}$)[\w.-]+$/; const DOMAIN = /^[a-z\d](?:[a-z\d-]*[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]*[a-z\d])?)+$/; export function parseRepo(text, defaultDomain = "github.com") { const input = text.trim(); const remote = parseRemoteUrl(input); const parts = (remote?.path ?? input.replace(/\/+$/, "")).split("/"); const domain = remote?.domain ?? (parts.length === 2 ? defaultDomain.trim().toLowerCase() : parts.shift()?.toLowerCase()); // The repository component of a full issue URL is useful to all callers. if (remote && parts.length === 4 && parts[2] === "issues" && /^[1-9]\d*$/.test(parts[3])) parts.length = 2; const [owner, name] = parts; const repo = name?.replace(/\.git$/i, ""); if (!domain || !DOMAIN.test(domain) || parts.length !== 2 || !NAME.test(owner ?? "") || !NAME.test(repo ?? "")) { throw new ForgeError("invalid", "Use owner/name, domain/owner/name or a repository URL."); } return `${domain}/${owner}/${repo}`; } function isShorthand(text) { return /^[\w.-]+\/[\w.-]+\/*$/.test(text.trim()); } /** Apply the project's forge domain to a bare owner/name. Explicit domains take precedence. */ export async function parseRepoForProject(text, project, store) { const repo = project && isShorthand(text) ? await repoForProject(project, store) : undefined; return parseRepo(text, repo?.split("/")[0]); } export function forgeKind(domain) { if (domain === "github.com") return "github"; if (domain.startsWith("gitboy.")) return "gitboy"; throw new ForgeError("invalid", "Use a github.com or gitboy repository."); } export function repoParts(input, expected) { const repo = parseRepo(input); const [domain, owner, name] = repo.split("/"); const kind = forgeKind(domain); if (expected && expected !== kind) throw new ForgeError("invalid", "The repository belongs to a different forge."); return { repo, domain, kind, path: `${owner}/${name}` }; } async function checkoutRepo(directory, remote) { try { const url = (await git(directory, "config", "--get", `remote.${remote}.url`)).trim(); return parseRemoteUrl(url) ? repoParts(url) : undefined; } catch { return undefined; } } export async function walkUpIssueCheckout(store, cwd = process.cwd()) { if (process.env.PHREN_PATH?.trim() || findNearestPhrenPath(cwd) !== path.resolve(store)) return undefined; return gitRoot(cwd); } export async function repoForCheckout(directory) { const origin = await checkoutRepo(directory, "origin"); if (origin?.kind === "github") { const gitboy = await checkoutRepo(directory, "gitboy"); if (gitboy?.kind === "gitboy") return gitboy.repo; } return origin?.repo; } export async function repoForProject(project, store) { let root = store ?? findPhrenPath(); if (!root) return undefined; const { storeName, projectName } = parseStoreQualified(project); if (!isValidProjectName(projectName)) return undefined; if (storeName) { try { root = resolveProject(root, project).store.path; } catch { return undefined; } } const config = readProjectConfig(root, projectName); const source = getProjectSourcePath(root, projectName, config); const currentCheckout = storeName ? undefined : await walkUpIssueCheckout(root); if (currentCheckout && (projectName === projectSlugFromPath(currentCheckout) || projectName === readRootManifest(root)?.primaryProject || source === currentCheckout)) { return repoForCheckout(currentCheckout); } const override = config.issues; if (override !== undefined && override !== null && typeof override !== "string") { throw new ForgeError("invalid", "The project's issues setting must be a repository reference."); } const configured = override?.trim(); if (configured && !isShorthand(configured)) return repoParts(configured).repo; const directory = projectCheckoutCandidates(projectName, source).find(isGitRepository); const checkout = directory ? await repoForCheckout(directory) : undefined; return configured ? parseRepo(configured, checkout?.split("/")[0]) : checkout; }