Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

PR Agent is an AI-powered pull request analyzer available as both a CLI tool and a GitHub Action. It analyzes code changes using multiple AI providers (Anthropic Claude, OpenAI GPT, Google Gemini) and provides summaries, risk detection, complexity scoring, and architecture-aware recommendations.

## Build & Development Commands

```bash
# Install dependencies (--legacy-peer-deps required for LangChain peer dependency conflicts)
npm install --legacy-peer-deps

# Build everything (TypeScript + GitHub Action bundle)
npm run build

# Build TypeScript only
npm run build:tsc

# Build GitHub Action only (uses @vercel/ncc)
npm run build:action

# Clean and rebuild
npm run build:clean

# Run tests
npm test
npm test -- --watch # Watch mode
npm test -- --coverage # With coverage

# Run single test file
npm test -- tests/config-loader.test.ts

# Development mode (no build needed)
npm run dev

# Run CLI from built dist
npm run cli
```

## Architecture

The project follows a modular monolith pattern with four layers:

### Entry Points
- `src/cli/index.ts` - CLI entry point using Commander.js
- `src/action.ts` - GitHub Action entry point
- `src/index.ts` - Probot app integration

### Core Analysis Engine
- `src/agents/pr-analyzer-agent.ts` - Main LangChain-based analysis workflow
- `src/agents/base-pr-agent-workflow.ts` - LangGraph workflow orchestration

### AI Provider Layer
- `src/providers/` - Provider implementations (Anthropic, OpenAI, Google)
- `src/providers/provider.factory.ts` - Factory pattern for instantiation
- `src/providers/provider.interface.ts` - Common interface all providers implement

### Tools & Utilities
- `src/tools/pr-analysis-tools.ts` - Diff parsing and analysis tools
- `src/utils/arch-docs-parser.ts` - Parse `.arch-docs` documentation folder
- `src/utils/arch-docs-rag.ts` - RAG (Retrieval-Augmented Generation) for architecture context
- `src/utils/branch-resolver.ts` - Git branch detection and resolution
- `src/cli/utils/config-loader.ts` - Configuration file management

### Data Flow
```
User Input β†’ CLI/Action Interface β†’ Config Loader β†’ PRAnalyzerAgent
β†’ Parse Diff β†’ Load Architecture Docs (optional) β†’ Build RAG Context
β†’ Provider Factory β†’ AI Provider β†’ Structured Analysis Results
β†’ Format Output (CLI terminal or GitHub PR comment)
```

## Key Configuration Files

- `.pragent.config.json` - User configuration (AI provider, model, API keys, analysis settings)
- `.pr-analyzer.yml` - GitHub Action configuration
- `action.yml` - GitHub Action manifest

## Technology Stack

- **Framework**: TypeScript/Node.js (ES Modules), requires Node.js >=18.0.0
- **AI Orchestration**: LangChain v1.x with LangGraph for workflow management
- **CLI Framework**: Commander.js with Inquirer for interactive prompts
- **Testing**: Jest with ts-jest
- **Action Bundling**: @vercel/ncc bundles the GitHub Action into a single file

## Important Notes

- Large diffs (>50KB) automatically use agent-based chunking (configurable via `analysis.agentThreshold`)
- The GitHub Action bundle in `dist/` must be committed after changes to action code
- Environment variables `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, or `GOOGLE_API_KEY` can be used instead of config file
- Default branch detection uses: config file β†’ GitHub API β†’ git commands β†’ fallback to `origin/main`
193 changes: 180 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ PR Agent analyzes your code changes and provides:
- **Complexity Rating**: A 1-5 scale rating of complexity with file-level breakdown
- **Actionable Insights**: Specific recommendations based on your codebase
- **Architecture-Aware Analysis**: Leverages your `.arch-docs` for context-aware reviews
- **Peer Review Integration**: Validates PRs against linked Jira tickets and acceptance criteria
- **Static Analysis**: Semgrep integration for security vulnerabilities and code quality issues

## Features

✨ **Intelligent Agent Mode** - Automatically handles large diffs without chunking
πŸ—οΈ **Architecture Documentation Integration** - Uses `.arch-docs` for smarter analysis
πŸ”¬ **Static Analysis Integration** - Semgrep-powered security and code quality scanning
πŸ”Œ **Multiple AI Providers** - Anthropic Claude, OpenAI GPT, Google Gemini
πŸ–₯️ **CLI & GitHub Action** - Use locally or in CI/CD pipelines
πŸ“Š **File-Level Analysis** - Individual risk and complexity scores per file
🌐 **Language-Aware** - Supports TypeScript, JavaScript, Python, Java, Go, Rust, and more
βš™οΈ **Configurable** - Customize models, providers, and analysis modes
✨ **Intelligent Agent Mode** - Automatically handles large diffs without chunking
πŸ—οΈ **Architecture Documentation Integration** - Uses `.arch-docs` for smarter analysis
πŸ”¬ **Static Analysis Integration** - Semgrep-powered security and code quality scanning
πŸ”Œ **Multiple AI Providers** - Anthropic Claude, OpenAI GPT, Google Gemini, Zhipu AI
πŸ–₯️ **CLI & GitHub Action** - Use locally or in CI/CD pipelines
πŸ“Š **File-Level Analysis** - Individual risk and complexity scores per file
🎫 **Peer Review Integration** - Validates against Jira tickets and acceptance criteria
🌐 **Language-Aware** - Supports TypeScript, JavaScript, Python, Java, Go, Rust, and more
βš™οΈ **Configurable** - Customize models, providers, and analysis modes
🎨 **Unified Output Format** - Semgrep and AI findings use consistent formatting, sorted by severity

## Quick Reference
Expand Down Expand Up @@ -59,6 +61,7 @@ pr-agent help # Show help
- [Configuration](#configuration)
- [Architecture Documentation Integration](#architecture-documentation-integration)
- [Static Analysis Integration](#static-analysis-integration)
- [Peer Review Integration](#peer-review-integration)
- [GitHub Action Usage](#github-action-usage)
- [Supported AI Models](#supported-ai-models)
- [Common Use Cases](#common-use-cases)
Expand All @@ -75,6 +78,7 @@ Before using PR Agent, you'll need:
- **Anthropic Claude**: Sign up at [Anthropic Console](https://console.anthropic.com/) (Recommended)
- **OpenAI GPT**: Get your key from [OpenAI Platform](https://platform.openai.com/)
- **Google Gemini**: Get your key from [Google AI Studio](https://makersuite.google.com/)
- **Zhipu AI**: Get your key from [Zhipu AI Platform](https://open.bigmodel.cn/) (Anthropic-compatible API)

2. **For GitHub Action**: Repository with permissions to add workflows and secrets

Expand Down Expand Up @@ -173,6 +177,7 @@ pr-agent analyze --complexity
pr-agent analyze --provider anthropic
pr-agent analyze --provider openai
pr-agent analyze --provider google
pr-agent analyze --provider zhipu

# Use specific model
pr-agent analyze --provider anthropic --model claude-sonnet-4-5-20250929
Expand Down Expand Up @@ -241,7 +246,8 @@ pr-agent config --reset
"apiKeys": {
"anthropic": "sk-ant-...",
"openai": "",
"google": ""
"google": "",
"zhipu": ""
},
"analysis": {
"defaultMode": "full",
Expand All @@ -265,6 +271,14 @@ pr-agent config --reset
"verbose": false,
"showStrategy": true,
"showRecommendations": true
},
"peerReview": {
"enabled": false,
"provider": "jira",
"instanceUrl": "https://your-company.atlassian.net",
"email": "your-email@company.com",
"apiToken": "your-jira-api-token",
"defaultProject": "PROJ"
}
}
```
Expand All @@ -282,6 +296,9 @@ export OPENAI_API_KEY="sk-..."

# Google Gemini
export GOOGLE_API_KEY="..."

# Zhipu AI
export ZHIPU_API_KEY="..."
```

Add to your `.bashrc`, `.zshrc`, or `.env` file for persistence.
Expand Down Expand Up @@ -512,12 +529,151 @@ Key insights from arch-docs integration:

### Benefits of Arch-Docs Integration

βœ… **Context-Aware Analysis** - AI understands your specific architecture
βœ… **Pattern Enforcement** - Detects violations of documented patterns
βœ… **Security Alignment** - Checks against your security guidelines
βœ… **Consistency** - Ensures PRs follow established conventions
βœ… **Context-Aware Analysis** - AI understands your specific architecture
βœ… **Pattern Enforcement** - Detects violations of documented patterns
βœ… **Security Alignment** - Checks against your security guidelines
βœ… **Consistency** - Ensures PRs follow established conventions
βœ… **Better Recommendations** - Suggestions aligned with your architecture

## Peer Review Integration

PR Agent can integrate with Jira to provide intelligent peer review analysis that validates your PR against linked tickets and acceptance criteria.

### Overview

When enabled, the peer review feature acts like a **senior developer reviewing your PR**:

- **Extracts ticket references** from PR title, branch name, or commit messages
- **Fetches ticket details** from Jira (description, acceptance criteria, story points)
- **Rates ticket quality** to identify poorly-defined requirements
- **Validates implementation** against derived requirements
- **Provides verdict** with blockers, warnings, and recommendations

### Setup

1. **Configure Jira credentials** in `.pragent.config.json`:

```json
{
"peerReview": {
"enabled": true,
"provider": "jira",
"instanceUrl": "https://your-company.atlassian.net",
"email": "your-email@company.com",
"apiToken": "your-jira-api-token",
"defaultProject": "PROJ",
"analyzeAcceptanceCriteria": true,
"rateTicketQuality": true,
"checkScopeCreep": true
}
}
```

2. **Get a Jira API Token**:
- Go to [Atlassian Account Settings](https://id.atlassian.com/manage-profile/security/api-tokens)
- Click "Create API token"
- Copy the token to your config

3. **Reference tickets in your PR**:
- PR title: `feat(PROJ-123): Add new feature`
- Branch name: `feature/PROJ-123-add-feature`
- Commit message: `PROJ-123: implement feature`

### Usage

```bash
# Run analysis with peer review
pr-agent analyze

# The peer review runs automatically if enabled in config
# Or enable it for a single run:
pr-agent analyze --peer-review
```

### Example Output

```
═══════════════════════════════════════════════════════════════
πŸ” PEER REVIEW ANALYSIS
═══════════════════════════════════════════════════════════════

πŸ“‹ LINKED TICKET
───────────────────────────────────────────────────────────────
Key: PROJ-123
Title: Add user authentication
Type: STORY
Status: In Progress
Points: 5

πŸ“Š TICKET QUALITY RATING
───────────────────────────────────────────────────────────────
Overall Score: 🟒 85/100 (GOOD)

Dimension Scores:
β€’ Description Clarity: 🟒 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘ 85
β€’ Acceptance Criteria: 🟒 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘ 90
β€’ Testability: 🟑 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘ 75
β€’ Scope Definition: 🟒 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘ 80

βœ… REQUIREMENTS VALIDATION
───────────────────────────────────────────────────────────────
Compliance: 🟒 92%

πŸ“Š REQUIREMENT STATUS:
βœ… Implement login endpoint with JWT tokens
βœ… Add password validation (min 8 chars)
🟑 Create logout endpoint
└─ Endpoint exists but doesn't invalidate tokens
βœ… Store hashed passwords in database

❌ COVERAGE GAPS:
🟑 [MINOR] Token invalidation not implemented
└─ Impact: Users cannot fully logout, tokens remain valid

🎯 PEER REVIEW VERDICT
───────────────────────────────────────────────────────────────
βœ… APPROVED (Confidence: 85%)

The PR implements the core authentication functionality as specified.
Minor gaps in token invalidation should be addressed in a follow-up.

Scores:
β€’ Implementation Completeness: 🟒 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘ 88
β€’ Quality Score: 🟒 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘ 82

πŸ’‘ RECOMMENDATIONS:
β€’ Add token invalidation on logout
β€’ Consider adding rate limiting
β€’ Add integration tests for auth flow

═══════════════════════════════════════════════════════════════
```

### Configuration Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `enabled` | boolean | `false` | Enable peer review integration |
| `provider` | string | `"jira"` | Issue tracker provider (currently only Jira) |
| `instanceUrl` | string | - | Your Jira instance URL |
| `email` | string | - | Your Jira account email |
| `apiToken` | string | - | Jira API token |
| `defaultProject` | string | - | Default project key for ticket extraction |
| `analyzeAcceptanceCriteria` | boolean | `true` | Validate against acceptance criteria |
| `rateTicketQuality` | boolean | `true` | Rate ticket definition quality |
| `generateTestSuggestions` | boolean | `true` | Generate test suggestions |
| `checkScopeCreep` | boolean | `true` | Detect scope creep in PRs |
| `includeTicketDetails` | boolean | `true` | Show ticket details in output |
| `verbose` | boolean | `false` | Enable verbose output |

### Benefits

βœ… **Requirement Validation** - Ensures PRs implement what the ticket specifies
βœ… **Quality Gate** - Catches incomplete implementations before review
βœ… **Ticket Quality Feedback** - Identifies poorly-defined requirements
βœ… **Scope Creep Detection** - Flags changes outside ticket scope
βœ… **Senior Developer Perspective** - Catches edge cases and missing behaviors

## GitHub Action Usage

### Setup Instructions
Expand Down Expand Up @@ -766,7 +922,11 @@ pr-agent/
β”‚ β”‚ β”œβ”€β”€ anthropic.provider.ts
β”‚ β”‚ β”œβ”€β”€ openai.provider.ts
β”‚ β”‚ β”œβ”€β”€ google.provider.ts
β”‚ β”‚ β”œβ”€β”€ zhipu.provider.ts
β”‚ β”‚ └── factory.ts
β”‚ β”œβ”€β”€ issue-tracker/ # Issue tracker integrations
β”‚ β”‚ β”œβ”€β”€ jira-mcp-client.ts # Jira API client
β”‚ β”‚ └── peer-review-integration.ts
β”‚ β”œβ”€β”€ tools/ # Agent tools
β”‚ β”‚ β”œβ”€β”€ pr-analysis-tools.ts
β”‚ β”‚ └── index.ts
Expand Down Expand Up @@ -957,6 +1117,13 @@ Best for: Quick analysis and cost-conscious usage

Best for: Alternative to Claude/GPT with competitive performance

### Zhipu AI

- **claude-sonnet-4-20250514** - Via Anthropic-compatible API
- **glm-4** - Zhipu's native model

Best for: Users in China or those preferring Zhipu's platform with Claude-compatible models

## Common Use Cases

### 1. Pre-Commit Review
Expand Down
Loading