|
| 1 | +import type { SkillHandler } from "../../types.js"; |
| 2 | +import { execFile } from "node:child_process"; |
| 3 | +import { promisify } from "node:util"; |
| 4 | +import { streamQuery } from "../../handler-utils.js"; |
| 5 | + |
| 6 | +const execFileAsync = promisify(execFile); |
| 7 | + |
| 8 | +const SYSTEM_PROMPT = `You are an expert at writing pull request descriptions. Generate a clear, well-structured PR description in Markdown. |
| 9 | +
|
| 10 | +Use this format: |
| 11 | +
|
| 12 | +## Summary |
| 13 | +- 2-3 bullet points explaining the purpose and motivation |
| 14 | +
|
| 15 | +## Changes |
| 16 | +- List the key changes grouped logically |
| 17 | +- Reference specific files when helpful |
| 18 | +
|
| 19 | +## Test plan |
| 20 | +- [ ] Checklist items describing how to verify the changes |
| 21 | +
|
| 22 | +Output ONLY the PR description in Markdown, nothing else.`; |
| 23 | + |
| 24 | +async function gitCommand( |
| 25 | + args: string[], |
| 26 | + cwd: string, |
| 27 | +): Promise<string | null> { |
| 28 | + try { |
| 29 | + const result = await execFileAsync("git", args, { |
| 30 | + cwd, |
| 31 | + maxBuffer: 10 * 1024 * 1024, |
| 32 | + }); |
| 33 | + return result.stdout.trim() || null; |
| 34 | + } catch { |
| 35 | + return null; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const handler: SkillHandler = async (ctx) => { |
| 40 | + const base = (ctx.flags.base as string) || "main"; |
| 41 | + const cwd = ctx.context.cwd; |
| 42 | + |
| 43 | + const [commitLog, diffStat, fullDiff] = await Promise.all([ |
| 44 | + gitCommand(["log", "--oneline", `${base}..HEAD`], cwd), |
| 45 | + gitCommand(["diff", `${base}...HEAD`, "--stat"], cwd), |
| 46 | + gitCommand(["diff", `${base}...HEAD`], cwd), |
| 47 | + ]); |
| 48 | + |
| 49 | + const diff = fullDiff || ctx.context.gitDiff; |
| 50 | + |
| 51 | + if (!diff && !commitLog) { |
| 52 | + ctx.logger.info( |
| 53 | + `No changes found between '${base}' and HEAD. Check that you are on a feature branch.`, |
| 54 | + ); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const parts = []; |
| 59 | + |
| 60 | + if (commitLog) { |
| 61 | + parts.push(`Commits on this branch:\n${commitLog}`); |
| 62 | + } |
| 63 | + |
| 64 | + if (diffStat) { |
| 65 | + parts.push(`Changed files summary:\n${diffStat}`); |
| 66 | + } |
| 67 | + |
| 68 | + if (diff) { |
| 69 | + parts.push(`Full diff:\n\`\`\`diff\n${diff}\n\`\`\``); |
| 70 | + } |
| 71 | + |
| 72 | + if (ctx.context.fileTree) { |
| 73 | + parts.push(`Repository file tree:\n${ctx.context.fileTree}`); |
| 74 | + } |
| 75 | + |
| 76 | + const prompt = `Generate a pull request description for the following changes:\n\n${parts.join("\n\n")}`; |
| 77 | + |
| 78 | + await streamQuery(ctx.agent, prompt, { systemPrompt: SYSTEM_PROMPT }); |
| 79 | +}; |
| 80 | + |
| 81 | +export default handler; |
0 commit comments