Skip to content

Instantly share code, notes, and snippets.

Created December 23, 2022 15:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save codeinthehole/d6a496b5a11e7500b7dd0c20f3e5b48c to your computer and use it in GitHub Desktop.
A Bash script that uses OpenAI's API to generate a pull request title
#!/usr/bin/env bash
# Print a title of the current pull request's commits.
# - For single commit pull requests, this prints the subject of the commit.
# - For multi-commit pull requests, this uses OpenAI's REST API to digest the
# commit messages of the pull request into a single sentence.
# Requires an OPENAI_API_KEY env var to authenticate requests - see:
set -e
# Commit selection variables.
# OpenAI tuning variables (can be overridden by passing as env vars).
INTRO=${INTRO:-"Below are some commit messages. Write a one sentence summary of them."}
function main {
# How we generate the title depends on if there is more than one commit.
if [ "$(num_commits)" -eq 1 ]; then
function num_commits {
git log origin/$TARGET_BRANCH.. --oneline | wc -l
function single_commit_title {
git log origin/$TARGET_BRANCH.. --format='%s' | head -1
function multi_commit_title {
# Build the JSON payload.
payload=$(jq -n \
--arg model "$MODEL" \
--argjson temp $TEMPERATURE \
--argjson tokens $MAX_TOKENS \
--arg prompt "$(
echo "$INTRO";
git log origin/$TARGET_BRANCH.. --format='%B';
)" '{model: $model, temperature: $temp, max_tokens: $tokens, prompt: $prompt}'
# POST payload to OpenAI API.
response=$(echo "$payload" | http Authorization:"Bearer $OPENAI_API_KEY")
# Keep a copy of the payload and response for debugging.
echo "$payload" | jq . > /tmp/openai-title.json
echo "$response" | jq . >> /tmp/openai-title.json
# Print the first line of the completion text.
echo "$response" | jq -r '.choices[0].text' | sed '/./,$!d' | head -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment