diff --git a/src/pr.ts b/src/pr.ts index eafd7df..44ee4ce 100644 --- a/src/pr.ts +++ b/src/pr.ts @@ -124,6 +124,36 @@ export async function getRemoteHostname(): Promise { } } +const PLATFORM_CLI: Record string[]; + label: string; +}> = { + github: { + bin: "gh", + args: (title, body, base, draft) => { + const a = ["pr", "create", "--title", title, "--body", body, "--base", base]; + if (draft) a.push("--draft"); + return a; + }, + label: "gh pr create", + }, + gitlab: { + bin: "glab", + args: (title, body, base, draft) => { + const a = ["mr", "create", "--title", title, "--description", body, "--target-branch", base]; + if (draft) a.push("--draft"); + return a; + }, + label: "glab mr create", + }, + gitea: { + bin: "tea", + args: (title, body, base, _draft) => ["pulls", "create", "--title", title, "--description", body, "--base", base], + label: "tea pulls create", + }, +}; + export async function createPR( platform: Platform, title: string, @@ -131,80 +161,8 @@ export async function createPR( base: string, draft: boolean, ): Promise { - if (platform === "github") { - const args = [ - "pr", - "create", - "--title", - title, - "--body", - body, - "--base", - base, - ]; - if (draft) args.push("--draft"); - - const proc = Bun.spawn(["gh", ...args], { - stdout: "pipe", - stderr: "pipe", - }); - const exitCode = await proc.exited; - const stdout = await new Response(proc.stdout).text(); - const stderr = await new Response(proc.stderr).text(); - - if (exitCode !== 0) { - throw new Error( - stderr.trim() || `gh pr create failed (exit code ${exitCode})`, - ); - } - - const match = stdout.match(/(https?:\/\/[^\s]+)/); - return match?.[1] ?? stdout.trim(); - } - - if (platform === "gitlab") { - const args = [ - "mr", - "create", - "--title", - title, - "--description", - body, - "--target-branch", - base, - ]; - if (draft) args.push("--draft"); - - const proc = Bun.spawn(["glab", ...args], { - stdout: "pipe", - stderr: "pipe", - }); - const exitCode = await proc.exited; - const stdout = await new Response(proc.stdout).text(); - const stderr = await new Response(proc.stderr).text(); - - if (exitCode !== 0) { - throw new Error( - stderr.trim() || `glab mr create failed (exit code ${exitCode})`, - ); - } - - const match = stdout.match(/(https?:\/\/[^\s]+)/); - return match?.[1] ?? stdout.trim(); - } - - const args = [ - "pulls", - "create", - "--title", - title, - "--description", - body, - "--base", - base, - ]; - - const proc = Bun.spawn(["tea", ...args], { + const cli = PLATFORM_CLI[platform]; + const proc = Bun.spawn([cli.bin, ...cli.args(title, body, base, draft)], { stdout: "pipe", stderr: "pipe", }); @@ -213,9 +171,7 @@ export async function createPR( const stderr = await new Response(proc.stderr).text(); if (exitCode !== 0) { - throw new Error( - stderr.trim() || `tea pulls create failed (exit code ${exitCode})`, - ); + throw new Error(stderr.trim() || `${cli.label} failed (exit code ${exitCode})`); } const match = stdout.match(/(https?:\/\/[^\s]+)/); diff --git a/src/types.ts b/src/types.ts index 5a5c41f..c548fbe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,22 +12,21 @@ export interface FileEntry { label: string; } -export interface ProjectContext { +export interface BaseContext { readme: string | null; packageDescription: string | null; structure: string | null; - recentCommits: string[]; diff: string; } -export interface PRContext { - readme: string | null; - packageDescription: string | null; - structure: string | null; +export interface ProjectContext extends BaseContext { + recentCommits: string[]; +} + +export interface PRContext extends BaseContext { branchName: string; baseBranch: string; branchCommits: string[]; - diff: string; } export interface CommitResult {