Automation

Automation#

gptme can be used to create powerful yet simple automated workflows. Here we showcase small but powerful examples that demonstrate the capabilities of gptme in various workflows and automation scenarios.

We will be using shell scripts, cron jobs, and other tools to automate the workflows.

Note

This is a work in progress. We intend to make gptme more powerful for automations, see issue #143 for more details on this plan.

Example: Implement feature

This example demonstrates how to implement a feature in a codebase using gptme, making sure the code is correct before creating a pull request.

Given a GitHub issue it will check out a new branch, look up relevant files, make changes, typecheck/test them, and create a pull request if everything is correct.

$ gptme 'read <url>' '-' 'create a branch' '-' 'look up relevant files' '-' 'make changes' '-' 'typecheck it' '-' 'test it' '-' 'create a pull request'

Example: Automated Code Review

This example demonstrates a simple and composable approach to automated code review using gptme and shell scripting.

  1. Create a script called review_pr.sh:

    #!/bin/bash
    # Usage: ./review_pr.sh <repo> <pr_number>
    
    repo=$1
    pr_number=$2
    
    # Fetch PR diff
    diff=$(gh pr view $pr_number --repo $repo --json diffUrl -q .diffUrl | xargs curl -s)
    
    # Generate review using gptme
    review=$(gptme --non-interactive "Review this pull request diff and provide constructive feedback:
    1. Identify potential bugs or issues.
    2. Suggest improvements for code quality and readability.
    3. Check for adherence to best practices.
    4. Highlight any security concerns.
    
    Pull Request Diff:
    $diff
    
    Format your review as a markdown list with clear, concise points.")
    
    # Post review comment
    gh pr comment $pr_number --repo $repo --body "## Automated Code Review
    
    $review
    
    *This review was generated automatically by gptme.*"
    
  2. Make the script executable:

    chmod +x review_pr.sh
    
  3. Set up a GitHub Actions workflow (.github/workflows/code_review.yml):

    name: Automated Code Review
    on:
      pull_request:
        types: [opened, synchronize]
    
    jobs:
      review:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - name: Install gptme and GitHub CLI
            run: |
              pip install gptme
              gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
          - name: Run code review
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            run: |
              ./review_pr.sh ${{ github.repository }} ${{ github.event.pull_request.number }}
    

This setup provides automated code reviews for your pull requests using gptme. It demonstrates how powerful automation can be achieved with minimal code and high composability.

Key points: - Uses shell scripting for simplicity and ease of understanding - Leverages gptme’s non-interactive mode for automation - Utilizes GitHub CLI (gh) for seamless GitHub integration - Integrates with GitHub Actions for automated workflow

Benefits of this approach: - Easily customizable: Adjust the gptme prompt to focus on specific aspects of code review - Composable: The shell script can be extended or combined with other tools - Minimal dependencies: Relies on widely available tools (bash, curl, gh) - Quick setup: Can be implemented in any GitHub repository with minimal configuration

To customize this for your specific needs: 1. Modify the gptme prompt in review_pr.sh to focus on your project’s coding standards 2. Add additional checks or integrations to the shell script as needed 3. Adjust the GitHub Actions workflow to fit your CI/CD pipeline

This example serves as a starting point for integrating gptme into your development workflow, demonstrating its potential for automating code review tasks.

Example: Daily Activity Summary

Here’s an example of how to use gptme to generate a daily summary based on ActivityWatch data using a shell script:

#!/bin/bash

# Function to get yesterday's date in YYYY-MM-DD format
get_yesterday() {
    date -d "yesterday" +%Y-%m-%d
}

# Function to get ActivityWatch report
get_aw_report() {
    local date=$1
    aw-client report $(hostname) --start $date --stop $(date -d "$date + 1 day" +%Y-%m-%d)
}

# Generate daily summary
generate_daily_summary() {
    local yesterday=$(get_yesterday)
    local aw_report=$(get_aw_report $yesterday)

    # Create a temporary file
    local summary_file=$(mktemp)

    # Generate summary using gptme
    gptme --non-interactive "Based on the following ActivityWatch report for $yesterday, provide a concise summary of yesterday's activities.
    Include insights on productivity, time spent on different categories, and any notable patterns.
    Suggest areas for improvement if applicable.

    ActivityWatch Report:
    $aw_report

    Please format the summary in a clear, easy-to-read structure.
    Save the summary to this file: $summary_file"

    # Return the path to the summary file
    echo "$summary_file"
}

# Run the summary generation and get the file path
summary_file=$(generate_daily_summary)

# Output the file path (you can use this in other scripts or log it)
echo "Daily summary saved to: $summary_file"

To automate this process to run every day at 8 AM, you could set up a cron job. Here’s an example cron entry:

0 8 * * * /path/to/daily_summary_script.sh

This automation will provide you with daily insights into your computer usage and productivity patterns from the previous day, leveraging the power of gptme to analyze and summarize the data collected by ActivityWatch.