|
8 | 8 | * 1. validate_repo — verify path exists, is a git repo, check staleness |
9 | 9 | * 2. create_session — create a Codekin session for the run |
10 | 10 | * 3. run_prompt — start Claude, send the prompt, wait for result |
11 | | - * 4. save_report — write Markdown output to outputDir, commit |
| 11 | + * 4. save_report — write Markdown output to outputDir, commit on codekin/reports branch, push |
12 | 12 | * |
13 | 13 | * MD file format — YAML frontmatter followed by the Claude prompt: |
14 | 14 | * |
15 | 15 | * --- |
16 | 16 | * kind: code-review.daily |
17 | 17 | * name: Daily Code Review |
18 | 18 | * sessionPrefix: review |
19 | | - * outputDir: review logs |
| 19 | + * outputDir: .codekin/reports/code-review |
20 | 20 | * filenameSuffix: _code-review-daily.md |
21 | 21 | * commitMessage: chore: code review |
22 | 22 | * --- |
@@ -312,14 +312,56 @@ function registerWorkflow(engine: WorkflowEngine, sessions: SessionManager, def: |
312 | 312 |
|
313 | 313 | console.log(`[workflow:${def.kind}] Saved report to ${filePath}`) |
314 | 314 |
|
| 315 | + // Commit on a dedicated branch so reports don't pollute the working branch |
| 316 | + const REPORTS_BRANCH = 'codekin/reports' |
315 | 317 | try { |
316 | 318 | const relativePath = `${def.outputDir}/${filename}` |
317 | | - execSync(`git add "${relativePath}"`, { cwd: repoPath, timeout: 10_000 }) |
318 | | - execSync( |
319 | | - `git commit -m "${def.commitMessage} ${dateStr}"`, |
320 | | - { cwd: repoPath, timeout: 15_000 } |
321 | | - ) |
322 | | - console.log(`[workflow:${def.kind}] Committed ${relativePath}`) |
| 319 | + const originalBranch = execSync('git rev-parse --abbrev-ref HEAD', { cwd: repoPath, timeout: 5_000 }).toString().trim() |
| 320 | + |
| 321 | + // Ensure the reports branch exists (create as orphan if needed) |
| 322 | + try { |
| 323 | + execSync(`git rev-parse --verify ${REPORTS_BRANCH}`, { cwd: repoPath, timeout: 5_000, stdio: 'pipe' }) |
| 324 | + } catch { |
| 325 | + // Branch doesn't exist yet — create it from the current branch |
| 326 | + execSync(`git branch ${REPORTS_BRANCH}`, { cwd: repoPath, timeout: 5_000 }) |
| 327 | + console.log(`[workflow:${def.kind}] Created branch ${REPORTS_BRANCH}`) |
| 328 | + } |
| 329 | + |
| 330 | + // Stash any uncommitted changes on the working branch |
| 331 | + const stashResult = execSync('git stash --include-untracked', { cwd: repoPath, timeout: 10_000 }).toString().trim() |
| 332 | + const didStash = !stashResult.includes('No local changes') |
| 333 | + |
| 334 | + try { |
| 335 | + execSync(`git checkout ${REPORTS_BRANCH}`, { cwd: repoPath, timeout: 10_000 }) |
| 336 | + |
| 337 | + // Re-create the report file on this branch (the file was written while on the original branch) |
| 338 | + const reportsDirOnBranch = join(repoPath, def.outputDir) |
| 339 | + if (!existsSync(reportsDirOnBranch)) { |
| 340 | + mkdirSync(reportsDirOnBranch, { recursive: true }) |
| 341 | + } |
| 342 | + writeFileSync(join(reportsDirOnBranch, filename), markdown, 'utf-8') |
| 343 | + |
| 344 | + execSync(`git add "${relativePath}"`, { cwd: repoPath, timeout: 10_000 }) |
| 345 | + execSync( |
| 346 | + `git commit -m "${def.commitMessage} ${dateStr}"`, |
| 347 | + { cwd: repoPath, timeout: 15_000 } |
| 348 | + ) |
| 349 | + console.log(`[workflow:${def.kind}] Committed ${relativePath} on ${REPORTS_BRANCH}`) |
| 350 | + |
| 351 | + // Push to remote |
| 352 | + try { |
| 353 | + execSync(`git push origin ${REPORTS_BRANCH}`, { cwd: repoPath, timeout: 30_000, stdio: 'pipe' }) |
| 354 | + console.log(`[workflow:${def.kind}] Pushed ${REPORTS_BRANCH} to origin`) |
| 355 | + } catch (pushErr) { |
| 356 | + console.warn(`[workflow:${def.kind}] Could not push ${REPORTS_BRANCH}: ${pushErr}`) |
| 357 | + } |
| 358 | + } finally { |
| 359 | + // Always switch back to the original branch |
| 360 | + execSync(`git checkout ${originalBranch}`, { cwd: repoPath, timeout: 10_000 }) |
| 361 | + if (didStash) { |
| 362 | + execSync('git stash pop', { cwd: repoPath, timeout: 10_000 }) |
| 363 | + } |
| 364 | + } |
323 | 365 | } catch (err) { |
324 | 366 | console.warn(`[workflow:${def.kind}] Could not commit report: ${err}`) |
325 | 367 | } |
|
0 commit comments