Skip to content

Instantly share code, notes, and snippets.

@t04glovern
Created May 14, 2023 05:35
Show Gist options
  • Save t04glovern/af3ca25a9972919b4215e7686fdb6fb3 to your computer and use it in GitHub Desktop.
Save t04glovern/af3ca25a9972919b4215e7686fdb6fb3 to your computer and use it in GitHub Desktop.
Generates PR comments - prefixes with JIRA ticket numbers provided you are using branch names provided by JIRA
alias pr_comment='~/.pr-comment.sh'
#!/bin/bash
# Extract the OpenAI API key from the environment
OPENAI_API_KEY=$OPENAI_API_KEY
# Fetch the current branch name
branch_name=$(git rev-parse --abbrev-ref HEAD)
# Extract task ID from branch name
task_id=$(echo $branch_name | awk -F- '{print $1"-"$2}')
# Validate task ID format
if [[ ! $task_id =~ ^[A-Z]{2,5}-[0-9]{1,5}$ ]]; then
task_id=""
fi
# Fetch the changes
changes=$(git diff)
# Use the OpenAI API to generate a description of the changes
# Need to escape double quotes in changes for JSON formatting
changes_escaped=$(echo $changes | sed 's/"/\\"/g')
description=$(curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{
\"model\": \"text-davinci-003\",
\"prompt\": \"Describe the following code changes for use in a short pull request comment: $changes_escaped\",
\"temperature\": 0.5,
\"max_tokens\": 100
}" \
"https://api.openai.com/v1/completions" | jq -r '.choices[0].text' | xargs)
# Format the command for the git commit
commit_message=""
if [[ -z $task_id ]]; then
commit_message="$description"
else
commit_message="$task_id: $description"
fi
commit_command="git commit -m \"$commit_message\""
echo $commit_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment