# Pull Request Review Tools

An AI-powered pull request review automation framework that provides intelligent code analysis using AWS Bedrock and Claude models. The framework supports multiple programming languages and project types with extensible, pluggable implementations.

## Features

- **Multi-language Support**: Python, JavaScript, PHP, Terraform, Database migrations, and generic code review
- **Intelligent Analysis**: Uses AWS Bedrock with Claude models for comprehensive code review
- **Extensible Architecture**: Plugin-based system for easy addition of new project types
- **GitHub Integration**: Seamless integration with GitHub PRs and comments
- **Configurable**: Environment variable-based configuration with repository-specific overrides
- **Docker Support**: Containerized execution for consistent environments

## Supported Project Types

- **terraform**: Infrastructure as Code with Atlantis integration
- **python**: Python code with best practices, security, and testing focus
- **javascript**: JavaScript/Node.js with framework-specific considerations (React, Vue, Angular)
- **php**: PHP code with PSR standards, security, and framework support (Laravel, Symfony)
- **database**: Database migrations using Liquibase/Liquigraph
- **generic**: Universal code review for any programming language

## Quick Start

### Prerequisites

- Python 3.13+
- Docker (optional)
- AWS credentials with Bedrock access
- GitHub token with repository access

### Environment Variables

Required environment variables:

- `GITHUB_TOKEN`: GitHub token with PR read permissions
- `GITHUB_PR_NUMBER`: PR number from the URL
- `GITHUB_REPO_NAME`: Repository name (e.g., "owner/repo-name")
- `GITHUB_REPO_OWNER`: Repository owner (defaults to "theorchard")
- `PROJECT_TYPE`: Project type - one of: `python`, `terraform`, `database`, `generic`, `javascript`, `php`

Optional environment variables:

- `Environment=dev`: Print results to terminal instead of posting to GitHub
- `TIMEOUT`: Analysis timeout in seconds (default: 600)

### Local Development

1. **Set up environment variables:**
   ```bash
   export GITHUB_TOKEN="your-github-token"
   export GITHUB_PR_NUMBER="123"
   export GITHUB_REPO_NAME="owner/repo-name"
   export PROJECT_TYPE="python"
   ```

2. **Assume AWS role:**
   ```bash
   aswume shared  # or dev/prod account
   ```

3. **Run analysis:**
   ```bash
   make docker_run  # Run in development mode (prints to terminal)
   ```

### Using with Different Project Types

Simply set the `PROJECT_TYPE` environment variable:

```bash
# For JavaScript projects
export PROJECT_TYPE="javascript"
make docker_run

# For PHP projects  
export PROJECT_TYPE="php"
make docker_run

# For Terraform projects
export PROJECT_TYPE="terraform"
make docker_run
```

## Make Commands

### Development & Testing
- `make ci_unit_lint` - Run all tests and linting (primary development command)
- `make env_test` - Set up test environment with dependencies
- `make ci_unit_lint_clean` - Clean cache files before testing

### Docker Operations
- `make docker_build` - Build the Docker image
- `make docker_run` - Run analysis in development mode (prints to terminal)
- `make docker_run_dev` - Explicitly run in development mode
- `make docker_run_prod` - Run in production mode (posts to GitHub)

### Individual Commands (without Make)
```bash
# Install dependencies
uv sync --group test

# Run linting
uv run ruff check
uv run ruff format

# Run tests
uv run pytest -v tests/unit/

# Run specific test
uv run pytest -v tests/unit/test_tf_infra.py::test_specific_function
```

## Architecture

The framework uses a modular, plugin-based architecture:

### Core Components

- **Base Classes** (`pr_review/base.py`): Abstract interfaces for extensibility
- **GitHub Client** (`pr_review/github_client.py`): GitHub API integration
- **LLM Client** (`pr_review/llm_client.py`): AWS Bedrock integration
- **Factory Pattern** (`pr_review/config.py`): Configuration and analyzer creation

### Project-Specific Implementations

Each project type includes:
- **PR Waiter**: Handles prerequisites before analysis (e.g., CI completion, Terraform plans)
- **Prompt Builder**: Creates tailored review prompts with language-specific guidelines
- **PR Analyzer**: Processes results and applies post-processing

### Workflow

1. **Factory Creation**: `PRAnalyzerFactory` creates analyzer based on repository or project type
2. **PR Waiting**: Wait for analysis prerequisites (CI, Terraform plans, etc.)
3. **Prompt Building**: Generate comprehensive, language-specific review prompt
4. **AI Analysis**: Send to AWS Bedrock Claude model for intelligent analysis
5. **Post-Processing**: Format results for GitHub markdown
6. **Result Delivery**: Post comment to GitHub or print to terminal

## Adding New Project Types

### Option 1: Use Existing Project Type
Set `PROJECT_TYPE` environment variable to use built-in configurations.

### Option 2: Create Custom Project Type Implementation
1. Create new module in `pr_review/` extending base classes
2. Add configuration to `PROJECT_TYPE_CONFIGS` in `config.py`

### Option 3: Repository-Specific Override
Add specific repository configuration to `REPO_CONFIGS` for custom behavior while keeping project type defaults for other repositories.

## Configuration

The framework uses a two-tier configuration system:

1. **Repository-Specific** (`REPO_CONFIGS`): Override configurations for specific repositories
2. **Project Type** (`PROJECT_TYPE_CONFIGS`): Default configurations based on project type

Repository-specific configurations take precedence over project type configurations.

## Requirements

- Python 3.13+
- AWS credentials with Bedrock access in us-east-1
- GitHub token with repository permissions
- Docker (for containerized execution)

## Contributing

1. Run tests: `make ci_unit_lint`
2. Follow existing patterns for new project types
3. Add comprehensive tests for new functionality
4. Update documentation for new features

For detailed development guidance, see `CLAUDE.md`.