0.1.1 • Published 9 months ago

@cheffromspace/github-pr-assistant v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

GitHub PR Assistant

An AI-powered GitHub PR reviewer using Anthropic Claude and Model Context Protocol (MCP). This tool helps automate code reviews by analyzing pull requests and providing detailed, actionable feedback.

Features

  • Automatically reviews GitHub pull requests using Claude AI
  • Provides detailed code review feedback with security, performance, and maintainability insights
  • Interactive tools to fetch files, add comments, and approve/request changes on PRs
  • Can be used as a library in your Node.js applications or as a standalone CLI tool
  • Supports customizable review focus areas and Claude model selection

Prerequisites

Before installing this tool, you'll need:

  1. Anthropic API Key: Get one from Anthropic Console
  2. GitHub Access Token: Create a token with repo scope from GitHub Settings

Installation

Global Installation (recommended for CLI usage)

npm install -g @cheffromspace/github-pr-assistant

Local Installation (for use in a project)

npm install @cheffromspace/github-pr-assistant

Configuration

You can configure the tool using environment variables or command-line options:

Required Configuration

  • Anthropic API Key: Set via ANTHROPIC_API_KEY environment variable or --anthropic-key option
  • GitHub Access Token: Set via GITHUB_ACCESS_TOKEN environment variable or --github-token option

Optional Configuration

  • Claude Model: Defaults to claude-3-7-sonnet-latest, can be customized via --model option
  • Max Tokens: Response length limit, defaults to 4000, can be adjusted via --max-tokens option

Usage

Command Line Interface

# Set environment variables (recommended approach)
export ANTHROPIC_API_KEY=your-anthropic-api-key
export GITHUB_ACCESS_TOKEN=your-github-token

# Run a PR review
gh-ai-pr-review review --owner your-org --repo your-repo --pr 123

# Use a specific Claude model
gh-ai-pr-review review --owner your-org --repo your-repo --pr 123 --model claude-3-opus-20240229

# Adjust token limit for longer responses
gh-ai-pr-review review --owner your-org --repo your-repo --pr 123 --max-tokens 8000

# Provide credentials via command line (not recommended for shared environments)
gh-ai-pr-review review --owner your-org --repo your-repo --pr 123 \
  --anthropic-key sk-ant-api-key \
  --github-token github-pat-token

As a Node.js Library

import { PRReviewClient } from '@cheffromspace/github-pr-assistant';

// Create client with configuration
const client = new PRReviewClient({
  anthropicApiKey: 'your-anthropic-api-key', // Or use process.env.ANTHROPIC_API_KEY
  githubToken: 'your-github-token',          // Or use process.env.GITHUB_ACCESS_TOKEN
  model: 'claude-3-opus-20240229',           // Optional, defaults to sonnet
  maxTokens: 8000                            // Optional, defaults to 4000
});

// Method 1: Review a PR by providing repo details (client will fetch PR data)
const review = await client.reviewPR({
  owner: 'your-org',
  repo: 'your-repo',
  number: 123
});

// Method 2: Review a PR with pre-fetched data
const review = await client.reviewPR({
  // Full PR data object
  title: 'Fix authentication bug',
  body: 'This PR addresses the authentication issue...',
  user: { login: 'developer' },
  // ... other PR details
});

// Access the review results
console.log(review.content);  // The review text
console.log(review.toolUses); // Actions taken by Claude during review

API Reference

PRReviewClient

The main client class for interacting with the PR review system.

Constructor

new PRReviewClient(config?: ClientConfig)

Parameters:

  • config (optional): Configuration object with:
    • anthropicApiKey: Anthropic API key (string)
    • githubToken: GitHub API token (string)
    • model: Claude model to use (string, default: 'claude-3-7-sonnet-latest')
    • maxTokens: Maximum tokens for response (number, default: 4000)

Methods

fetchPRData(owner: string, repo: string, prNumber: number): Promise<PRData>

Fetches complete pull request data from GitHub.

Parameters:

  • owner: Repository owner (organization or username)
  • repo: Repository name
  • prNumber: Pull request number

Returns: Complete PR data object

reviewPR(prData: PRDataWithPrompt | { owner: string, repo: string, number: number }): Promise<ReviewResult>

Performs a review of the provided PR data.

Parameters:

  • prData: Either complete PR data object OR repository identifiers to fetch the PR

Returns: Review result with content and tool uses

interface ReviewResult {
  content: string;        // The review text
  toolUses?: Array<{      // Actions performed during review
    name: string;         // Tool name (e.g., 'add_comment', 'approve')
    input: any;           // Input provided to the tool
    output: any;          // Tool execution result
  }>;
}

Available Tools

The PR Assistant uses the following tools to interact with GitHub:

  1. get_file: Fetches file contents from the repository
  2. add_comment: Posts a comment on the PR with review feedback
  3. approve: Approves the PR if it meets quality standards
  4. request_change: Requests changes when significant issues are found

Integration Ideas

  • CI/CD Pipeline: Run automated reviews on all PRs
  • GitHub Actions: Trigger reviews when PRs are opened or updated
  • Pre-merge Checks: Require AI review before merging
  • Developer Workflow: Get quick feedback before requesting human review

Troubleshooting

Common Issues

  • Authentication Errors: Ensure both API keys are valid and have appropriate permissions
  • Rate Limit Errors: GitHub or Anthropic API rate limits may be exceeded
  • Model Availability: Ensure the selected Claude model is available in your account

Debug Mode

For detailed logging:

# Set DEBUG environment variable
export DEBUG=gh-ai-pr-assistant:*

# Run the command
gh-ai-pr-review review --owner your-org --repo your-repo --pr 123

License

MIT