|
| 1 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 2 | +import { writeFileSync } from "node:fs"; |
| 3 | + |
| 4 | +const tagName = process.env.RELEASE_TAG_NAME; |
| 5 | +const openCodeApiKey = process.env.OPENCODE_API_KEY; |
| 6 | +const model = process.env.OPENCODE_MODEL || "opencode/kimi-k2"; |
| 7 | +const contextPath = ".release-diff-context.md"; |
| 8 | +const outputPath = "opencode-release-summary.md"; |
| 9 | +const maxPatchBytes = 120_000; |
| 10 | + |
| 11 | +if (!tagName) { |
| 12 | + console.log("No release tag was provided; skipping OpenCode release summary."); |
| 13 | + process.exit(0); |
| 14 | +} |
| 15 | + |
| 16 | +if (!openCodeApiKey) { |
| 17 | + console.log("OPENCODE_API_KEY is not configured; skipping OpenCode release summary."); |
| 18 | + process.exit(0); |
| 19 | +} |
| 20 | + |
| 21 | +function git(args) { |
| 22 | + return execFileSync("git", args, { encoding: "utf8" }).trim(); |
| 23 | +} |
| 24 | + |
| 25 | +function optionalGit(args) { |
| 26 | + try { |
| 27 | + return git(args); |
| 28 | + } catch { |
| 29 | + return ""; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const previousTag = optionalGit(["describe", "--tags", "--abbrev=0", `${tagName}^`]); |
| 34 | +const baseRef = previousTag || git(["rev-list", "--max-parents=0", tagName]); |
| 35 | +const range = `${baseRef}..${tagName}`; |
| 36 | +const diffStat = optionalGit(["diff", "--stat", range]); |
| 37 | +const nameStatus = optionalGit(["diff", "--name-status", range]); |
| 38 | +const commits = optionalGit(["log", "--oneline", "--no-decorate", range]); |
| 39 | +const patch = optionalGit(["diff", "--no-ext-diff", "--unified=40", range, "--", ".", ":(exclude)package-lock.json"]); |
| 40 | +const truncatedPatch = |
| 41 | + Buffer.byteLength(patch, "utf8") > maxPatchBytes |
| 42 | + ? `${Buffer.from(patch).subarray(0, maxPatchBytes).toString("utf8")}\n\n[Diff truncated at ${maxPatchBytes} bytes.]` |
| 43 | + : patch; |
| 44 | + |
| 45 | +writeFileSync( |
| 46 | + contextPath, |
| 47 | + [ |
| 48 | + `# Release Diff Context`, |
| 49 | + ``, |
| 50 | + `Current tag: ${tagName}`, |
| 51 | + `Previous tag: ${previousTag || "(none; comparing from initial commit)"}`, |
| 52 | + `Compared range: ${range}`, |
| 53 | + ``, |
| 54 | + `## Commits`, |
| 55 | + commits || "(none)", |
| 56 | + ``, |
| 57 | + `## Changed Files`, |
| 58 | + nameStatus || "(none)", |
| 59 | + ``, |
| 60 | + `## Diff Stat`, |
| 61 | + diffStat || "(none)", |
| 62 | + ``, |
| 63 | + `## Patch`, |
| 64 | + "```diff", |
| 65 | + truncatedPatch || "(none)", |
| 66 | + "```", |
| 67 | + "" |
| 68 | + ].join("\n") |
| 69 | +); |
| 70 | + |
| 71 | +const prompt = [ |
| 72 | + "Summarize this release diff for maintainers and npm users.", |
| 73 | + "Use concise Markdown.", |
| 74 | + "Focus on user-visible changes, migration notes, packaging changes, and risks.", |
| 75 | + "Do not invent changes that are not supported by the attached diff context.", |
| 76 | + "Do not include secrets, token values, or operational speculation.", |
| 77 | + "Return only the summary body." |
| 78 | +].join(" "); |
| 79 | + |
| 80 | +const result = spawnSync( |
| 81 | + "npx", |
| 82 | + ["-y", "opencode-ai@1.17.9", "run", "--pure", "--model", model, "--file", contextPath, prompt], |
| 83 | + { |
| 84 | + encoding: "utf8", |
| 85 | + env: process.env |
| 86 | + } |
| 87 | +); |
| 88 | + |
| 89 | +if (result.status !== 0) { |
| 90 | + console.error(result.stderr || result.stdout || "OpenCode summary generation failed."); |
| 91 | + process.exit(result.status ?? 1); |
| 92 | +} |
| 93 | + |
| 94 | +const summary = result.stdout.trim(); |
| 95 | +if (!summary) { |
| 96 | + console.log("OpenCode returned an empty summary."); |
| 97 | + process.exit(0); |
| 98 | +} |
| 99 | + |
| 100 | +writeFileSync(outputPath, `${summary}\n`); |
| 101 | +console.log(`Wrote OpenCode release summary to ${outputPath}.`); |
0 commit comments