Skip to content

Instantly share code, notes, and snippets.

@valmat
Last active February 29, 2024 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valmat/cd64141685f2655c4a02d59902962ca3 to your computer and use it in GitHub Desktop.
Save valmat/cd64141685f2655c4a02d59902962ca3 to your computer and use it in GitHub Desktop.
Automatically creates a new tag and AI generated description for it
#!/bin/env bash
set -e
model="gpt-4-turbo-preview";
temperature=0.8;
prompt="Here is the output of \`git log <latest_tag>..HEAD\`. Please provide the perfect tag message."`
`" Use emjoi if it is possible. Use past tense. "`
`"Provide a message ready for direct substitution "`
`"(don't use \`\`\` and provide multiline massege)."
# the hash of the commit that was marked with the last tag
latest_tag_hash=$(git rev-list --tags --max-count=1)
# the name of the last tag, using its hash
latest_tag=$(git describe --tags --abbrev=0 $latest_tag_hash)
# Output the commit log from the previous tag to the HEAD (current state)
# use the found name of the previous tag to start the story
massege=`git log $latest_tag..HEAD`
minor_ver="${latest_tag##*.}"
base_ver="${latest_tag%.*}"
((minor_ver+=1))
new_tag="${base_ver}.${minor_ver}"
echo "$latest_tag -> $new_tag"
if [ -z "$massege" ]; then
echo "Nothing to commit, working tree clean" >&2
exit 1
fi
json_payload=$(jq -n \
--arg model "$model" \
--arg temperature "$temperature" \
--arg prompt "$prompt" \
--arg massege "$massege" \
'{
model: $model,
temperature: ($temperature | tonumber),
messages: [
{
role: "system",
content: $prompt
},
{
role: "user",
content: $massege
}
]
}')
response=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$json_payload")
# Checking if the "error" key is in the response
if echo "$response" | jq -e '.error' > /dev/null; then
echo "Error: $(echo "$response" | jq -r '.error.message')" >&2
exit 2
else
# If there is no error, trying to extract the desired message
content=$(echo "$response" | jq -e -r '.choices[0].message.content')
# Checking if the message was extracted
if ! [[ $? -eq 0 ]]; then
# If the message could not be extracted,
# output the original text of the response to stderr
echo "Failed to extract the message. Response was:" >&2
echo "$response" >&2
exit 3
fi
fi
description_file=$(mktemp)
echo "$content" > "$description_file"
nano "$description_file"
git tag -a "$new_tag" -m "$content"
cat "$description_file"
rm "$description_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment