When you build a GitHub bot that opens fix pull requests instead of leaving inline comments, naive line-based patching breaks on multi-finding files. You apply the first hunk, and the line numbers shift underneath every remaining patch. Coordinates collide, syntax breaks, and Git rejects the hunk.
In CodePeel, we avoided line-splicing by combining three parts: a strict fixability filter, full-buffer file rewrites, and GitHub's Git Data API. The worker constructs signed commits in memory without cloning the repository to disk.
1. Filter Out Vague Advice Before Writing Code
Architectural comments like "split this class" or "add unit tests" need domain context from the author. Asking a model to guess those changes produces noisy diffs you end up closing.
Our triage step drops any finding unless it includes three fields:
- A concrete
problemCodeandfixCodepair. - A line number inside the modified diff.
- A file path that exists in the repository tree.
Review Findings
|
v
Fixability Filter ---> Drops architectural notes and vague nits
|
v
Group by File Path (avoids colliding single-file patches)
|
v
Full-Buffer Rewrite
2. Rewrite the Whole File Buffer Instead of Splicing Lines
Patching one hunk at a time fails when two bugs sit in the same file. Changing line count in the first patch invalidates the line offsets for the second.
We rewrite the entire file buffer in one pass:
- The worker fetches the file content at the PR head commit from the GitHub Contents API.
- We send the full file text alongside every fixable finding for that path.
- The generator returns one updated file with all patches applied together.
- We diff that output against the original buffer. If nothing changed, we skip the file so we avoid opening empty commits.
3. Build Commits via GitHub's Git Trees API
Cloning a repo for every webhook slows down the worker and eats disk space. Instead, we build the commit tree directly through GitHub's Git Data API:
1. Fetch PR Head ---> Read branch name and Head SHA
|
2. Read Tree SHA ---> GET /repos/.../git/commits/{head_sha}
|
3. Create Tree Object ---> POST /repos/.../git/trees (base_tree = Head Tree)
|
4. Create Commit ---> POST /repos/.../commits (parent = Head SHA)
|
5. Create Fix Branch ---> POST /repos/.../git/refs/heads/codepeel/fix-pr-...
|
6. Open Fix PR ---> POST /repos/.../pulls (base = Feature Branch)
Setting base_tree to the PR head tree means we only upload blobs for modified files. Unchanged files keep their existing tree pointers. We target your active feature branch as the base rather than main, so your CI checks and test suite still gate the merge.
Here is a typical SQL parameterization patch generated by this flow:
--- a/src/db/users.ts
+++ b/src/db/users.ts
@@ -39,7 +39,7 @@
async findByEmail(email: string) {
- const result = await db.query(`SELECT * FROM users WHERE email = '${email}'`);
+ const result = await db.query('SELECT * FROM users WHERE email = $1', [email]);
return result.rows[0];
}
4. Known Limits of Single-File Rewrites
We keep two boundaries explicit in production:
- No cross-file signature changes: Because we rewrite files independently, refactors that change a shared function signature across multiple files stay out of auto-fix.
- Parallel webhook execution: Fix generation runs alongside standard review comments. If a fix pass times out, your inline PR comments still post on time.
If you ship solo and want a reviewer that opens fix PRs on private repos, you can try CodePeel (30 reviews/month free on private repos, plus a 7-day Pro trial). You can also read the full architecture breakdown on the CodePeel engineering blog.
Top comments (0)