#atom

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:

  1. 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
  2. Review Focus Areas:
    • Style conformance with project standards
    • DRY (Don't Repeat Yourself) principle adherence
    • Bug detection and prevention
    • Improvement suggestions
  3. Implementation Options:
    • Simple clipboard-based approaches (lowest cost)
    • API-based integrations (highest automation)
    • Model selection based on review complexity

Why It Matters:


How to Implement:

  1. 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.")
  1. Define Review Criteria:
    • Develop prompt template focusing on desired review aspects
    • Include project-specific standards where applicable
  2. Choose Delivery Method:
    • Clipboard integration for web interface submission
    • API calls for fully automated workflow

Example:


Connections:


References:

  1. Primary Source:
    • Code example from blog post
  2. Additional Resources:
    • Git documentation for diff commands
    • Best practices for code review prompts

Tags:

#CodeReview #Automation #DevWorkflow #GitIntegration #AI #QualityAssurance #snippet #script


Connections:


Sources: