import { mkdirSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import type { Role, RoleResult, StartStep, WorkflowMessage } from "@uncaged/nerve-core"; import { isDryRun, spawnSafe } from "@uncaged/nerve-workflow-utils"; import { cfgGet } from "../../lib/provider.js"; import { lastParseFromMessages, lastRepoFromMessages, resolveRepoCwd } from "../../lib/repo-context.js"; import { formatSpawnFailure } from "../../lib/spawn-utils.js"; export type PublishMeta = { success: boolean; }; export type BuildPublishDeps = { nerveRoot: string; }; function logPath(nerveRoot: string): string { return join(nerveRoot, "logs", `solve-issue-publish-${Date.now()}.log`); } export function buildPublishRole({ nerveRoot }: BuildPublishDeps): Role { return async (start: StartStep, messages: WorkflowMessage[]): Promise> => { const dry = isDryRun(start); const file = logPath(nerveRoot); mkdirSync(join(file, ".."), { recursive: true }); const cwd = resolveRepoCwd(messages); const parsed = lastParseFromMessages(messages); const repoInfo = lastRepoFromMessages(messages); if (dry) { const msg = "[dry-run] publish skipped (no git push / PR)"; writeFileSync(file, `${msg}\n`, "utf-8"); return { content: `[dry-run] publish skipped — log: ${file}`, meta: { success: true }, }; } if (cwd === null || parsed === null) { writeFileSync(file, "missing cwd or SOLVE_ISSUE_PARSE\n", "utf-8"); return { content: `publish failed: missing repo path or issue markers\nLog: ${file}`, meta: { success: false }, }; } const branchRun = await spawnSafe("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd, env: null, timeoutMs: 15_000, }); if (!branchRun.ok) { writeFileSync(file, formatSpawnFailure(branchRun.error), "utf-8"); return { content: `publish failed: cannot read branch — ${formatSpawnFailure(branchRun.error)}`, meta: { success: false }, }; } const branch = branchRun.value.stdout.trim(); if (branch.length === 0) { return { content: "publish failed: empty branch name", meta: { success: false } }; } const pushRun = await spawnSafe("git", ["push", "-u", "origin", branch], { cwd, env: null, timeoutMs: 180_000, }); const lines: string[] = []; if (!pushRun.ok) { lines.push(`git push failed: ${formatSpawnFailure(pushRun.error)}`); writeFileSync(file, lines.join("\n"), "utf-8"); return { content: `publish: push failed\nLog: ${file}`, meta: { success: false }, }; } lines.push(`$ git push -u origin ${branch}`); lines.push(pushRun.value.stdout); lines.push(pushRun.value.stderr); const token = process.env.GITEA_TOKEN ?? (await cfgGet(nerveRoot, "GITEA_TOKEN")); if (token === null || token.length === 0) { lines.push("missing GITEA_TOKEN"); writeFileSync(file, lines.join("\n"), "utf-8"); return { content: `publish: push ok but no GITEA_TOKEN for PR API\nLog: ${file}`, meta: { success: false }, }; } const base = repoInfo?.defaultBranch ?? "main"; const title = `fix: issue #${parsed.number}`; const body = [`Automated PR from solve-issue workflow.`, `Issue: https://${parsed.host}/${parsed.owner}/${parsed.repo}/issues/${parsed.number}`].join( "\n", ); const apiUrl = `https://${parsed.host}/api/v1/repos/${parsed.owner}/${parsed.repo}/pulls`; let prOk = false; let prBody = ""; try { const res = await fetch(apiUrl, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `token ${token}`, }, body: JSON.stringify({ title, body, head: branch, base, }), }); prBody = await res.text(); prOk = res.ok; lines.push(`POST ${apiUrl} -> ${res.status}`); lines.push(prBody.slice(0, 4000)); } catch (e) { lines.push(`fetch error: ${e instanceof Error ? e.message : String(e)}`); } writeFileSync(file, lines.join("\n"), "utf-8"); if (!prOk) { return { content: `publish: push ok but PR API failed\nLog: ${file}`, meta: { success: false }, }; } return { content: `publish: pushed ${branch} and opened PR\nLog: ${file}`, meta: { success: true }, }; }; }