Skip to content

Instantly share code, notes, and snippets.

@woylie
Last active June 15, 2023 12:29
Show Gist options
  • Select an option

  • Save woylie/f452d94b98bd731d23f2253fcc22be72 to your computer and use it in GitHub Desktop.

Select an option

Save woylie/f452d94b98bd731d23f2253fcc22be72 to your computer and use it in GitHub Desktop.
Generates your daily horoscope based on your Git commits of the last week using the OpenAI API
#!/bin/bash -e
# File: git_horoscope.sh
# Description: This script uses the OpenAI API to generate your daily horoscope
# based on your Git commits of the last seven days.
# Requirements:
# - https://github.com/openai/openai-python installed and in the $PATH
# - configured OpenAI API key (export OPENAI_API_KEY=abc)
# - Github CLI installed and configured
# Usage: ./git_horoscope.sh author
# Check if gh is installed
if ! command -v gh &> /dev/null; then
echo "Error: 'gh' command is not installed. Please install GitHub CLI (gh) and try again." >&2
exit 1
fi
# Check if OpenAI CLI is installed
if ! command -v openai &> /dev/null; then
echo "Error: 'openai' command is not installed. Please install the OpenAI CLI (https://github.com/openai/openai-python) and try again." >&2
exit 1
fi
# Check if OpenAI API Key is set
if [ -z "$OPENAI_API_KEY" ]; then
echo "Error: OPENAI_API_KEY is not set. Please set the OpenAI API key using 'export OPENAI_API_KEY=your_api_key'." >&2
exit 1
fi
# Check argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 gitusername"
exit 1
fi
author=$1
gpt_version="gpt-3.5-turbo"
prompt="Based on the celestial patterns in my commit history from the last 7 days, with each change represented as a star and each project as a constellation, reveal my coder's horoscope for today. Consider the rhythm of contributions, the nature of bug fixes, and the vigor in feature implementations. Provide a unique insight into my coding journey today."
if [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OS X
start_date=$(date -v-7d '+%Y-%m-%d')
else
# Linux and others
start_date=$(date -d '7 days ago' '+%Y-%m-%d')
fi
commits=$(gh search commits \
--limit 500 \
--author "$author" \
--author-date ">=$start_date" \
--sort author-date \
--order desc \
--json commit \
--jq 'map(select(.commit.message | startswith("Merge") | not) | "\(.commit.message)")[]' \
| sed '/Co-authored-by/d;')
text="$prompt\n$commits"
truncated_text="${text:0:4097}"
openai api chat_completions.create -m "$gpt_version" -g user "$truncated_text"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment