Subtitle:
Leveraging scripts and AI to automate code quality assessment
Core Idea:
Automated code review tools combine version control system outputs with AI language models to provide instant feedback on code quality, style adherence, and potential bugs.
Key Principles:
- Automation Foundation:
- Git diff commands extract code changes for review
- Command line scripts orchestrate the review process
- AI models analyze changes in context of software engineering best practices
- Review Focus Areas:
- Style conformance with project standards
- DRY (Don't Repeat Yourself) principle adherence
- Bug detection and prevention
- Improvement suggestions
- Implementation Options:
- Simple clipboard-based approaches (lowest cost)
- API-based integrations (highest automation)
- Model selection based on review complexity
Why It Matters:
- Efficiency Gain:
- Reduces time spent on initial code quality checks
- Allows human reviewers to focus on higher-level concerns
- Consistency:
- Ensures all code undergoes standardized review process
- Applies consistent standards across projects
- Learning Opportunity:
- Developers receive immediate feedback before peer review
- Helps developers learn best practices through repeated exposure
How to Implement:
- Create Basic Script:
- Write script to extract git diff of current changes
- Format appropriate prompt for AI model
import requests
import json
import subprocess
import argparse
import os
PROMPT = "Check the changes and compare with the style of the repo, are standards respected, is the code DRY, are there any obvious bugs? Any potential improvements? Be brief."
def get_git_diff(commit="master"):
"""
Retrieves the git diff between the specified commit and HEAD.
Args:
commit (str, optional): The commit ID to compare against. Defaults to "master".
Returns:
str: The git diff output, or None if an error occurred.
"""
try:
process = subprocess.run(
["git", "diff", f"{commit}...HEAD"],
capture_output=True,
text=True,
check=True # Raise an exception if the command fails
)
return process.stdout
except subprocess.CalledProcessError as e:
print(f"Error getting git diff: {e}")
print(f"Stderr: {e.stderr}") # Print the actual error message from git
return None
except FileNotFoundError:
print("Error: Git not found. Make sure Git is installed and in your PATH.")
return None
def query_openrouter(diff_content, model=None):
"""
Queries the OpenRouter API with the provided diff content.
Args:
diff_content (str): The git diff content.
Returns:
str: The content of the response from OpenRouter, or None on error.
"""
try:
api_key = os.environ.get("OPENROUTER_API_KEY")
if model is None:
model = "gemini"
if not api_key:
print("Error: OPENROUTER_API_KEY environment variable not set.")
return None
if model == "gemini":
model_name = "google/gemini-2.0-flash-001"
elif model == "claude":
model_name = "anthropic/claude-3.7-sonnet:beta"
else:
print(f"Error: Invalid model: {model}. Please use 'gemini' or 'claude'.")
return None
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
data=json.dumps({
"model": model_name,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": f"{PROMPT} {diff_content}"
}
]
}
],
})
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
return response.json()['choices'][0]['message']['content']
except requests.exceptions.RequestException as e:
print(f"Error connecting to OpenRouter: {e}")
return None
except json.JSONDecodeError:
print("Error decoding JSON response.")
return None
except KeyError:
print("Error: Unexpected JSON response format from OpenRouter.")
print(response.text) #Print to help debug
return None
def copy_to_clipboard(diff_content):
"""
Pastes the diff content to the clipboard.
Args:
diff_content (str): The diff content to paste.
Returns:
None
"""
try:
subprocess.run(["pbcopy"], input=diff_content.encode())
except FileNotFoundError:
print("Error: pbcopy not found. Please install it if needed.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Get git diff and query OpenRouter.")
group = parser.add_mutually_exclusive_group()
group.add_argument("--commit", type=str, default="master", help="The commit ID to compare against.")
group.add_argument("--model", type=str, help="The model to use for the prompt. Can be 'gemini' or 'claude'.")
group.add_argument("--ai", action="store_true", help="Flag to permit this script to use OpenRouter API, which is paid.")
args = parser.parse_args()
diff = PROMPT + " " + get_git_diff(args.commit)
if not args.ai:
if diff:
copy_to_clipboard(diff)
print("Prompt and diff copied to clipboard.")
else:
print("Failed to get git diff.")
else:
if diff:
response_content = query_openrouter(diff, model=args.model)
if response_content:
copy_to_clipboard(response_content)
print(response_content)
else:
print("Failed to get response from OpenRouter.")
else:
print("Failed to get git diff.")
- Define Review Criteria:
- Develop prompt template focusing on desired review aspects
- Include project-specific standards where applicable
- Choose Delivery Method:
- Clipboard integration for web interface submission
- API calls for fully automated workflow
Example:
-
Scenario:
- Developer wants to check code before submitting for team review
-
Application:
# Script usage example code-review # Copies prompt+diff to clipboard for manual submission # Or fully automated approach code-review --ai # Automatically gets AI review and displays results
-
Result:
- Developer receives detailed feedback highlighting style issues, potential bugs, and improvement opportunities
- Code quality improves before team review begins
Connections:
- Related Concepts:
- Git Diff Commands: Understanding how to extract code changes
- Python: The language is written in
- LLM-Powered Code Reviews: Specific approaches to using LLMs for code analysis
- Broader Concepts:
- Development Workflow Automation: Broader category of development process automation
- Continuous Integration: Integration of automated reviews into CI pipelines
References:
- Primary Source:
- Code example from blog post
- Additional Resources:
- Git documentation for diff commands
- Best practices for code review prompts
Tags:
#CodeReview #Automation #DevWorkflow #GitIntegration #AI #QualityAssurance #snippet #script
Connections:
Sources: