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",
temperature: 0.5,
system: [
"system",
"system.safety_harmful_content",
"system.safety_protected_material",
],
})

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.
  • temperature: Sets the creativity level for the AI model. A lower temperature means less creativity, and 0.5 is a balanced choice.
  • system.safety... injects safety rules to the system message to ensure the AI model is not producing harmful or protected content.

Gathering Changes with Git

The script captures the difference between the current branch and the defaultBranch.

// compute diff
const defaultBranch = await git.defaultBranch()
const changes = await git.diff({
base: defaultBranch,
})
console.log(changes)

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
npx genaiscript run prd

Adding the -prd flag will automatically update the pull request description on github as well.

Terminal window
npx genaiscript run prd -prd

Full source (GitHub)

prd.genai.mts
script({
title: "Pull Request Descriptor",
description: "Generate a pull request description from the git diff",
temperature: 0.5,
system: [
"system",
"system.assistant",
"system.safety_harmful_content",
"system.safety_protected_material",
],
})
const defaultBranch = await git.defaultBranch()
const branch = await git.branch()
if (branch === defaultBranch) cancel("you are already on the default branch")
// compute diff
const changes = await git.diff({
base: defaultBranch,
})
console.log(changes)
// task
$`## Task
Describe a high level summary of the changes in GIT_DIFF in a way that a software engineer will understand.
This description will be used as the pull request description.
## 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.)
`
def("GIT_DIFF", changes, { maxTokens: 30000 })