Skip to content

Pull Request Descriptor

Pull requests are an integral part of collaborative software development. They allow developers to review code changes before merging them into the main codebase. Creating informative and concise pull request descriptions can be a time-consuming task, especially when dealing with large or complex changes. This is where GenAI comes in, streamlining the process with a smart script that generates pull request descriptions automatically. 🚀

Script Metadata

script({
title: "Pull Request Descriptor",
description: "Generate a pull request description from the git diff",
tools: ["fs"],
temperature: 0.5,
parameters: {
defaultBranch: {
type: "string",
description: "The default branch of the repository",
default: "main",
},
},
})

The script function is used to set up the script’s metadata. It’s the first thing you’ll notice, and here’s what each property means:

  • title: This is the name of the script, which is “Pull Request Descriptor.”
  • description: A brief explanation of what the script does.
  • tools: An array of tools that the script will use. In this case, it uses the filesystem (fs) tool.
  • temperature: Sets the creativity level for the AI model. A lower temperature means less creativity, and 0.5 is a balanced choice.
  • parameters: Defines the inputs that the script accepts. Here, it takes defaultBranch as a parameter, which is the branch against which the pull request will be compared.

Gathering Changes with Git

const defaultBranch = env.vars.defaultBranch
const { stdout: changes } = await host.exec("git", [
"diff",
defaultBranch,
"--cached",
"--",
".",
":!.vscode/*",
":!*yarn.lock",
":!*THIRD_PARTY_LICENSES.md",
])

The script captures the difference between the current branch and the defaultBranch. It uses the exec function to run the git diff command, excluding certain files and directories like .vscode and yarn.lock.

Defining the Git Diff Output

def("GIT_DIFF", changes, {
language: "diff",
maxTokens: 20000,
})

Here, def is used to define a variable called GIT_DIFF that holds the changes from the git diff command. It specifies that the content is in diff format and allows up to 20000 tokens (a measure of content length for the AI model).

Generating the Pull Request Description

$`You are an expert software developer and architect.
## Task
- Describe a high level summary of the changes in GIT_DIFF in a way that a software engineer will understand.
## Instructions
- do NOT explain that GIT_DIFF displays changes in the codebase
- try to extract the intent of the changes, don't focus on the details
- use bullet points to list the changes
- use emojis to make the description more engaging
- focus on the most important changes
- ignore comments about imports (like added, remove, changed, etc.)
`

The template literal, denoted by $, is where the AI model is given a prompt to generate the pull request description. The instructions are clearly laid out: summarize the changes without going into details and make the description easy to understand by using bullet points and emojis.

Running the Script

To use this script, you need the GenAIScript CLI installed. If you haven’t installed it yet, please refer to the installation guide.

Once you have the CLI set up, run the following command:

Terminal window
genaiscript run prd -prd

This will execute the prd script and give you a pull request description. The script’s filename is prd.genai.mts, but we use prd without the file extension when invoking it through the CLI.

Full source (GitHub)

prd.genai.mts
script({
title: "Pull Request Descriptor",
description: "Generate a pull request description from the git diff",
tools: ["fs"],
temperature: 0.5,
})
const defaultBranch = await git.defaultBranch()
const changes = await git.diff({
base: defaultBranch,
excludedPaths: [
".vscode/*",
"**/yarn.lock",
"**/genaiscript.d.ts",
"*THIRD_PARTY_LICENSES.md",
],
})
console.log(changes)
def("GIT_DIFF", changes, { maxTokens: 20000 })
// task
$`You are an expert software developer and architect.
## Task
- Describe a high level summary of the changes in GIT_DIFF in a way that a software engineer will understand.
## Instructions
- do NOT explain that GIT_DIFF displays changes in the codebase
- try to extract the intent of the changes, don't focus on the details
- use bullet points to list the changes
- use emojis to make the description more engaging
- focus on the most important changes
- ignore comments about imports (like added, remove, changed, etc.)
`
// running: make sure to add the -prd flag