Skip to main content

Templates for GitHub PR Review Automation

You:

Can you provide more information about the templates that would be involved in this process? Where can they be stored and is there automation in terms of using the templates to do a setup initially for a new team or a team wanting to use the AIPR review process?


ChatGPT:

"Templates are the backbone of customization for the AIPR ReviewBot, and making them accessible, reusable, and automatable is how we take this to the next level. Let’s lay out what templates to include, where to store them, and how to automate the setup process for new teams."


What Templates Should Be Included?

Templates define how the bot behaves, comments, and interacts with pull requests. Here’s a breakdown of essential templates:

1. Comment Templates

Define the structure and tone of the bot's feedback.

Examples:

{
"complexity": "The function at lines {startLine}-{endLine} exceeds the maximum cyclomatic complexity of {maxComplexity}. Consider breaking it down for better maintainability.",
"coverage": "This file has {coveragePercentage}% test coverage, below the required threshold of {minCoverage}%. Please add tests to improve coverage.",
"refactor": "Consider refactoring the method `{methodName}` to improve readability and reduce complexity.",
"generalPositive": "Great job on the implementation of `{featureName}`! The code is clean and well-structured."
}

2. Suggestion Templates

Templates for code suggestions, allowing the bot to generate actionable fixes.

Examples:

{
"lintFix": "Here’s a suggested linting fix for `{fileName}` at line {lineNumber}:",
"missingTests": "It seems tests are missing for `{methodName}`. Add unit tests to cover edge cases.",
"securityVulnerability": "The use of `{dependencyName}` may pose a security risk. Consider replacing it with `{recommendedDependency}`."
}

3. Workflow Templates

Predefined GitHub Actions workflows for setting up the bot.

Examples:

  • Basic Workflow: Triggered on PR events.
  • Advanced Workflow: Includes event-specific workflows for opened, synchronized, or closed PRs.
name: AI-Powered PR Review

on:
pull_request:
types: [opened, synchronize]

jobs:
review:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Run AIPR ReviewBot
uses: ./actions/ai-pr-review
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

4. Configuration File Template

A default bot-config.json file to ensure teams start with sensible defaults.

Example:

{
"general": {
"enableSuggestions": true,
"enableEvaluations": true,
"defaultRiskThreshold": 7,
"enableAutoComments": true
},
"evaluations": {
"codeComplexity": {
"maxCyclomaticComplexity": 10
},
"testCoverage": {
"minTestCoverage": 80
}
},
"suggestions": {
"enableSuggestions": true,
"suggestionTypes": ["refactor", "tests", "linting"]
},
"persona": {
"name": "CodeGuard",
"tone": "friendly",
"commentTemplates": {
"complexity": "This function exceeds the allowed cyclomatic complexity.",
"coverage": "The file does not meet the test coverage threshold."
}
}
}

Where to Store Templates?

1. In the Repository

  • Location: .github/templates/
  • Use Case: For repository-specific templates.
  • Access: Easy to version and customize per project.

2. In a Centralized Config Repository

  • Location: org-config/templates/
  • Use Case: For organization-wide consistency.
  • Access: Can be fetched dynamically during runtime using the GitHub API.

3. As Part of the GitHub Action

  • Location: Included in the bot’s repository or npm package.
  • Use Case: Provide a fallback or default set of templates.
  • Access: Preloaded during bot installation.

How to Automate Template Setup

Automation ensures teams can start using the bot with minimal manual effort.

1. CLI Tool for Initialization

Provide a CLI tool to bootstrap the setup process.

Example CLI Commands

# Initialize bot configuration for a repository
npx ai-pr-review init --repo your-org/your-repo

# Customize bot templates
npx ai-pr-review templates add complexity --content "Custom complexity message"

Implementation

const { Octokit } = require('@octokit/rest');
const fs = require('fs');

/**
* Initialize AIPR ReviewBot for a repository.
* @param {string} repoOwner - Repository owner.
* @param {string} repoName - Repository name.
*/
async function initializeBot(repoOwner, repoName) {
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });

// Default templates
const templates = {
commentTemplates: {
complexity: "The function at lines {startLine}-{endLine} exceeds cyclomatic complexity.",
coverage: "The test coverage is below the required threshold.",
},
workflow: `
name: AI-Powered PR Review

on:
pull_request:
types: [opened, synchronize]

jobs:
review:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Run AIPR ReviewBot
uses: ./actions/ai-pr-review
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
`
};

// Upload configuration file
const configPath = '.github/bot-config.json';
await octokit.repos.createOrUpdateFileContents({
owner: repoOwner,
repo: repoName,
path: configPath,
message: 'Add bot configuration file',
content: Buffer.from(JSON.stringify(templates, null, 2)).toString('base64'),
});

console.log(`Bot initialized for ${repoOwner}/${repoName}`);
}

initializeBot('your-org', 'your-repo');

2. GitHub Action for Bootstrapping

Create a workflow that sets up the bot automatically for new repositories.

Example Bootstrapping Workflow

name: AIPR ReviewBot Setup

on:
repository_dispatch:
types: [initialize-bot]

jobs:
setup-bot:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Initialize Bot
run: |
npx ai-pr-review init --repo ${{ github.repository_owner }}/{{ github.event.repository.name }}

Trigger this workflow with a repository dispatch event to bootstrap new repositories.


3. Dynamic Template Fetching

Fetch templates dynamically during runtime to keep the system lightweight and flexible.

Fetching Templates from GitHub

async function fetchTemplate(repoOwner, repoName, templateName) {
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
const { data } = await octokit.repos.getContent({
owner: repoOwner,
repo: repoName,
path: `.github/templates/${templateName}.json`,
});

return JSON.parse(Buffer.from(data.content, 'base64').toString('utf-8'));
}

// Example usage
fetchTemplate('your-org', 'your-repo', 'commentTemplates').then((template) => {
console.log('Template:', template);
});

4. Updating Templates

Provide a mechanism for teams to update templates without redeploying the bot.

CLI Command for Updating Templates

npx ai-pr-review templates update complexity --content "Updated complexity message"

Implementation

async function updateTemplate(repoOwner, repoName, templateName, newContent) {
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });

// Fetch existing templates
const existingTemplates = await fetchTemplate(repoOwner, repoName, templateName);

// Update the template
existingTemplates[templateName] = newContent;

// Push updated templates
await octokit.repos.createOrUpdateFileContents({
owner: repoOwner,
repo: repoName,
path: `.github/templates/${templateName}.json`,
message: 'Update bot template',
content: Buffer.from(JSON.stringify(existingTemplates, null, 2)).toString('base64'),
});
}

Best Practices for Using Templates

  1. Start with Defaults:

    • Use preloaded templates to get started quickly.
    • Fine-tune over time based on team feedback.
  2. Keep Templates Versioned:

    • Store templates in the repository to ensure version control.
    • Use centralized templates for organization-wide consistency.
  3. Make Updating Easy:

    • Automate template updates with CLI tools or workflows.
  4. Be Transparent:

    • Clearly document how templates impact the bot’s behavior.
    • Share templates with the team for collaboration and alignment.

Let me know where you'd like to dive deeper—templates are the key to making this bot a perfect fit for any team! 🚀