Created
October 10, 2024 08:42
-
-
Save enjikaka/dfd98cb355e347b7cc426ef38e37c1dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dest_branch="origin/${1:-"main"}" | |
# Diff the current branch against origin/main | |
diff_output=$(git diff $dest_branch) | |
json_safe_diff=$(jq -Rs . <<< "$diff_output") | |
OPENAI_API_KEY=$(cat ~/.openai_api_key) | |
# Use jq to create the JSON payload | |
json_payload=$(jq -n \ | |
"{ | |
\"messages\": [ | |
{ | |
\"role\": \"system\", | |
\"content\": \"You are a helpful assistant designed to generate clear consiste pull request descriptions for humans to quickly understand what has changed in a pull request.\" | |
}, | |
{ | |
\"role\": \"user\", | |
\"content\": $json_safe_diff | |
} | |
], | |
\"model\": \"gpt-3.5-turbo-1106\", | |
}") | |
# Send the diff to OpenAI API and ask for a PR description | |
# Make sure you have jq and curl installed and your OPENAI_API_KEY is set as an environment variable | |
response=$(curl -s -X POST "https://api.openai.com/v1/chat/completions" \ | |
-H "Authorization: Bearer $OPENAI_API_KEY" \ | |
-H "Content-Type: application/json" \ | |
-d "$json_payload") | |
# Check if there's an "error" key in the response | |
error_message=$(echo $response | jq -r '.error.message // empty') | |
if [[ -n $error_message ]]; then | |
echo "🤖 Error: $error_message" | |
exit 1 | |
else | |
# Extract the text from the response | |
pr_description=$(echo $response | jq -r '.choices[0].message.content') | |
# Check if pr_description is empty | |
if [[ -n $pr_description ]]; then | |
# Copy the PR description to the clipboard | |
echo "$pr_description" | pbcopy | |
# Output the PR description | |
echo "🤖 PR Description based on the diff with ${dest_branch}: | |
$pr_description | |
(The description is copied to your clipboard.)" | |
else | |
echo "Error: Unable to generate PR description." | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment