Pre-Merge Check Automation and Integration Patterns in the code-review-graph Repository
The code-review-graph repository ships a built-in pre-merge check workflow that combines a lightweight graph snapshot with an LLM prompt to block risky pull requests before they reach main.
The tirth8205/code-review-graph project implements pre-merge check automation and integration patterns as a first-class concern, exposing both a Python API and a CLI entry point so teams can embed context-aware analysis into Git hooks, CI pipelines, or review bots. Every stage of the workflow is grounded in concrete source files that handle context collection, prompt templating, and execution.
How Pre-Merge Check Automation Works
The pre-merge workflow follows a deterministic pipeline implemented across code_review_graph/prompts.py, code_review_graph/eval/token_benchmark.py, and the CLI entry point. Each stage is designed to fail fast and keep execution under a few hundred milliseconds for medium-sized repositories.
-
Collect Minimal Context – The
get_minimal_context(task="pre-merge check")function incode_review_graph/prompts.py(lines 144–150) traverses the repository graph and prunes it to changed files, dependency neighbors, recent reviewers, and metadata such as last commit and test coverage. -
Render the Prompt – The gathered context is interpolated into the pre-merge MCP template defined in the same file. The template instructs the LLM to verify compilation or type-checking status, flag missing test coverage, detect hard-coded secrets, and summarize risky dependency upgrades.
-
Run the LLM Evaluation – The prompt is dispatched to the configured provider, and the response is parsed into a structured
PreMergeReport. The simulation incode_review_graph/eval/token_benchmark.py(lines 135–148) demonstrates this end-to-end invocation. -
Fail-Fast Decision – If the report contains blockers such as compilation errors or failing security checks, the CLI returns a non-zero exit code that aborts the merge. When the check passes, it emits a concise "All clear" summary with optional improvement suggestions.
-
Human-Readable Summary – The output is formatted for immediate consumption by developers or downstream CI logs, ensuring the reason for a blocked merge is transparent.
Core Components and Source Files
get_minimal_context(task="pre-merge check")incode_review_graph/prompts.py(lines 144–150): Retrieves the lightweight graph snapshot that serves as prompt context, keeping PR analysis fast enough for local hooks.token_benchmark.pyincode_review_graph/eval/token_benchmark.py(lines 135–148): Provides a runnable simulation of the full pre-merge check, useful for debugging context size and LLM latency.test_prompts.pyintests/test_prompts.py(lines 147–149): Unit tests that enforce correct pre-merge terminology and guarantee the generated prompt remains well-formed after code changes.- CLI entry point
crgincode_review_graph/cli.py: Exposes thecrg pre-mergesub-command that wires the Python API into shell-based automation. - Git hook sample in
.git/hooks/pre-merge-commit.sample: A ready-to-adapt shell script showing how to invoke the CLI inside a local Git workflow.
Integration Patterns for Pre-Merge Checks
The repository supports four primary integration patterns that all consume the same core API, eliminating configuration drift between local and remote environments.
Git Hook Enforcement
Rename the sample hook to pre-merge-commit and make it executable. The script calls crg pre-merge $BASE $HEAD and aborts the merge if the exit code is non-zero. This pattern catches issues before they leave a developer's machine.
CI/CD Pipeline Stage
Add a job step that executes crg pre-merge --base ${{ github.base_ref }} --head ${{ github.head_ref }}. Because the CLI exits with an error code on blockers, the pipeline fails automatically without custom parsing logic.
On-Demand Review Bot
A PR comment bot can import get_minimal_context directly from code_review_graph.prompts, build the context for the current branch, and post the PreMergeReport summary back to the pull request thread. This pattern is useful for optional advisory checks rather than hard gates.
Batch Re-Scanning
Schedule a cron job that iterates over open pull requests and re-runs crg pre-merge against stale branches. The shared API ensures that batch results are identical to those produced by interactive or CI-triggered runs.
Practical Code Examples
Call the Python API Directly
from code_review_graph.prompts import get_minimal_context
# Build the context for a PR that merges `feature-xyz` into `main`
context = get_minimal_context(
task="pre-merge check",
repo_root=".", # path to the repository root
base="main",
head="feature-xyz"
)
# `context` is a dict that can be fed to the LLM or inspected directly
print(context["summary"])
Wire the Check into a Git Hook
#!/usr/bin/env bash
# .git/hooks/pre-merge-commit (make executable)
BASE=$(git rev-parse "$1")
HEAD=$(git rev-parse "$2")
# Execute the pre‑merge check; abort if it fails
crg pre-merge --base "$BASE" --head "$HEAD"
if [ $? -ne 0 ]; then
echo "Pre‑merge check failed – aborting merge."
exit 1
fi
Add a GitHub Actions Job
name: Pre‑Merge Validation
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
pre-merge:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install crg
run: pip install code-review-graph
- name: Run pre‑merge check
run: |
crg pre-merge \
--base ${{ github.base_ref }} \
--head ${{ github.head_ref }}
Summary
- The pre-merge check in
tirth8205/code-review-graphcombines a lightweight graph context builder with an LLM prompt to evaluate pull requests before they merge. - The workflow is anchored by
get_minimal_contextincode_review_graph/prompts.pyand demonstrated incode_review_graph/eval/token_benchmark.py. - Every integration pattern—whether a local Git hook, a CI/CD stage, a review bot, or a batch job—uses the same CLI or Python API, guaranteeing consistent behavior.
- Running the check requires only
crg pre-merge --base <ref> --head <ref>, making it trivial to adopt in existing DevOps pipelines.
Frequently Asked Questions
What file implements the pre-merge context collection?
The get_minimal_context function in code_review_graph/prompts.py (lines 144–150) implements context collection. It walks the repository graph, isolates changed nodes, and decorates them with metadata so the LLM prompt stays small and fast.
How does the pre-merge check fail a merge?
The CLI entry point crg pre-merge returns a non-zero exit code whenever the generated PreMergeReport contains blockers such as compilation errors or security violations. Git hooks and CI pipelines can treat this as a standard shell failure and abort the merge automatically.
Can I run the pre-merge check outside of Git hooks or CI?
Yes. You can import get_minimal_context from code_review_graph.prompts inside any Python script or bot. This lets you generate a pre-merge report on demand, post it as a PR comment, or integrate it into custom review workflows without invoking the CLI.
Where is the pre-merge behavior tested?
The tests/test_prompts.py file (lines 147–149) contains unit tests that verify the pre-merge prompt includes the correct terminology and that the template renders correctly. Additionally, code_review_graph/eval/token_benchmark.py provides an end-to-end simulation for manual validation and performance tuning.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →