`pr create` takes --title (required), --body/--body-file, --base, --head,
--assignee, --reviewer, repeatable name-resolved --label, and --milestone.
An omitted --head defaults to the current local branch; an omitted --base to
the repository's default branch. Before creating, an existing open PR for the
same base/head pair short-circuits to `pull_request: { number, url, already:
true }` rather than opening a duplicate; only an open PR does, since a closed
one's branches are free to be proposed again.
`pr comment <n>` posts through the shared issue-comment endpoint and returns
the created comment as `comment: { number, author, created, body }` (ADR 0008),
reporting a 404 as PR_NOT_FOUND since the caller asked about a pull request.
That comment block is now built in one place (src/comment.ts) for both issue
and pr comment, as ADR 0008 requires them to stay identical.
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import type { CliDeps } from "./deps.js";
|
|
import { runSubprocess } from "./subprocess.js";
|
|
|
|
export interface RemoteRepo {
|
|
host: string;
|
|
owner: string;
|
|
name: string;
|
|
}
|
|
|
|
function parseRepoPath(rawPath: string): { owner: string; name: string } | null {
|
|
const path = rawPath.replace(/^\/+/, "").replace(/\/+$/, "").replace(/\.git$/, "");
|
|
const segments = path.split("/");
|
|
if (segments.length !== 2 || !segments[0] || !segments[1]) {
|
|
return null;
|
|
}
|
|
return { owner: segments[0], name: segments[1] };
|
|
}
|
|
|
|
export function parseRemoteUrl(url: string): RemoteRepo | null {
|
|
const trimmed = url.trim();
|
|
if (/^(https?|ssh):\/\//.test(trimmed)) {
|
|
let parsed: URL;
|
|
try {
|
|
parsed = new URL(trimmed);
|
|
} catch {
|
|
return null;
|
|
}
|
|
const repo = parseRepoPath(parsed.pathname);
|
|
if (!repo || !parsed.hostname) {
|
|
return null;
|
|
}
|
|
return { host: parsed.hostname, ...repo };
|
|
}
|
|
// scp-like SSH form: [user@]host:owner/name[.git]
|
|
const scp = /^(?:[^@/\s]+@)?([^:/\s]+):(.+)$/.exec(trimmed);
|
|
if (scp) {
|
|
const repo = parseRepoPath(scp[2]!);
|
|
if (!repo) {
|
|
return null;
|
|
}
|
|
return { host: scp[1]!, ...repo };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* The branch currently checked out, or null when git cannot name one — it is
|
|
* absent, this is not a repository, or HEAD is detached. Every null case is
|
|
* repaired the same way, by the caller naming the branch explicitly, so they
|
|
* are not distinguished here.
|
|
*/
|
|
export async function currentBranch(deps: CliDeps): Promise<string | null> {
|
|
const result = await runSubprocess("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
|
|
cwd: deps.cwd,
|
|
env: deps.env,
|
|
});
|
|
if (result.enoent || result.code !== 0) {
|
|
return null;
|
|
}
|
|
const branch = result.stdout.trim();
|
|
// A detached HEAD abbreviates to the literal "HEAD", which is no branch.
|
|
return branch && branch !== "HEAD" ? branch : null;
|
|
}
|
|
|
|
export async function detectRemote(deps: CliDeps): Promise<RemoteRepo | null> {
|
|
const result = await runSubprocess("git", ["remote", "get-url", "origin"], {
|
|
cwd: deps.cwd,
|
|
env: deps.env,
|
|
});
|
|
if (result.enoent || result.code !== 0) {
|
|
return null;
|
|
}
|
|
return parseRemoteUrl(result.stdout);
|
|
}
|