11.6 C
Pakistan
Wednesday, February 21, 2024

Using Mods to Create AI-Powered Pull Requests

A Step-by-Step Guide

Like me, you might be becoming impatient as you wait for the public release of GitHub Copilot X.

Because of this, I felt that making my own straightforward AI-powered Pull Request generator using Mods, a program that integrates AI into your command line, would be a useful exercise.

Finding Mods was like finding gold for someone who already loved the charm.sh tools and Go libraries. In this lesson, I’ll show you how to use mods to create a Pull Request that has both a catchy title and a detailed description. Let’s get started!

What you will require before beginning

There are a few necessary tools and setups you’ll need to have before we start the training. I’m going to proceed as though you can install the utilities via Homebrew; if not, check out their documentation for your system.

Get the GitHub CLI tool installed
For this tutorial, we’ll be utilizing GitHub’s gh command-line tool. Run:

brew install gh
  1. Bring in moderators

The AI command-line tool mods, which powers our Pull Request generation, must then be installed.

brew install charmbracelet/tap/mods
  1. Use Gum to Add Some Shell Glamour

Let’s install gum as well for a little extra flair in your shell environment.

brew install gum
  1. Select an AI Backend

Both LocalAI and OpenAI are supported by Mods. For the purposes of this manual, OpenAI will be used. You must create an OpenAI API key in order to accomplish this. To create one, go to the OpenAI API key page.

  1. Configure the Environment Variable

Make sure to set the OPENAI_API_KEY environment variable on your system after you have your OpenAI API key. You are now prepared to begin creating Pull Requests with AI support thanks to these prerequisites!

The “ship” script

I’ve crafted a simple script to get us started:

#!/bin/bash

# Exit on error
set -e

# Function to check if a command exists
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# Check for required commands
for cmd in git gum gh mods; do
    if ! command_exists "$cmd"; then
        echo "Error: $cmd is not installed."
        exit 1
    fi
done

# Get the default branch from the remote repository
get_default_branch() {
    git remote show origin | grep 'HEAD branch' | cut -d' ' -f5
}

# Generate PR title and body
generate_pr_info() {
    local default_branch
    default_branch=$(get_default_branch)

    local type scope pr_title_prefix pr_summary pr_body

    # Using the Conventional Commit format
    type=$(gum choose "fix" "feat" "docs" "style" "refactor" "test" "chore" "revert")
    scope=$(gum input --placeholder "scope")

    # Since the scope is optional, wrap it in parentheses if it has a value.
    [ -n "$scope" ] && scope="($scope)"

    pr_title_prefix="$type$scope"

    gum style --foreground 212 "Generating Pull Request title..."
    pr_summary=$(git diff "$default_branch".. | mods "create a Pull Request title. First word should start with a lowercase letter")
    pr_title="$pr_title_prefix: $pr_summary"

    gum style --foreground 212 "Generating Pull Request title..."
    pr_body=$(git diff "$default_branch".. | mods -f "create a Pull Request body" --max-tokens 250)

    gh pr create \
        --title "$pr_title" \
        --body "$pr_body"
}

# Main script execution starts here
generate_pr_info

Standard commit format
I advise following a common commit format. specifically by adhering to the Conventional Commit structure. This is the rationale behind the script’s use of PR_TITLE_PREFIX.

Token Restrictions
Together with the –max-tokens 250 argument for the Pull Request body, I’ve also established a token cap for OpenAI usage. One token, according to OpenAI’s documentation, corresponds to around 4 typical English text characters.

Experiment
I also experimented with several pr_body inputs, including:

  • mods -f "Create a walkthrough in the Pull Request description"
  • mods -f "PR body: explanation"
  • mods -f "PR body: capture the most important changes"

It’s difficult to state with certainty what gets generated in the tests I did. In order to best meet your needs, I thus urge you to experiment with the moderator prompts.

The investigation
Save the Script Initially, I saved the executable script from the gist to my $PATH, for example, /.local/bin/ship

Create a New Branch in Step 2
I then used the following command to make a new branch from my main branch:

git switch -c test-pr-gen

Create a “Hello World” program in step 3
I then filled in a main.with a straightforward “Hello, world!” application, go file

echo 'package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}' > main.go

Step 4: Add to GitHub, commit, and push
I later added it to my Git repository, made the necessary adjustments, and uploaded the updated version to GitHub:

git add main.go && git commit -m "add hello world program"
git push -u origin test-pr-gen

Be aware that I typically decide to squash commits on merge. Therefore, even if I’m not using the conventional commit structure here, it will still be considered after being squashed.

  1. Conduct the test.
    It’s time to test the script at last. I just execute the ship alias I configured:
ship

I’ll have to decide on the kind and extent of the adjustment. I simply selected type = feat and scope = go for this.

Mods will then produce two responses based on the query and the git diff. It lasted between 10 and 15 seconds in my experiments.

Gh sends me a link to the Pull Request whenever it is finished.

Below, we can see that with very little effort, we are able to create a good title and description that can be shared with our peers.

Summary
It is undoubtedly advantageous to use AI to assist with jobs that could be tiresome and cause quality to suffer. It’s up to you to decide if the AI-generated description and title in this case are useful. However, it gives you a foundation upon which to grow, which is typically more manageable than starting from scratch.

Just keep in mind not to optimize something you can easily stop doing.

By doing so, I’ll end the debate over whether or not to use pull requests elsewhere.

I appreciate you reading. A would be appreciated if you liked this article. Remember to follow for more such articles!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles