Last active
May 18, 2021 00:31
-
-
Save fatso83/cc014edf3f9877b93914e59887e58eb2 to your computer and use it in GitHub Desktop.
Log your lunch hours in JIRA on the CLI. Install on Mac: `curl https://gist.githubusercontent.com/fatso83/cc014edf3f9877b93914e59887e58eb2/raw/ > tmp.sh; sh tmp.sh --install-mac`
This file contains hidden or 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 | |
| ################################################################################ | |
| # Lunch logger for lazy Making Waves people. | |
| # | |
| # Integrates with native scheduler on OS X | |
| # | |
| # Usage: | |
| # `log-lunch today # will log lunch for today` | |
| # `log-lunch 2016-08-03 # will log lunch for august 3rd` | |
| # | |
| # Options: | |
| # --help Show this help | |
| # --install-mac Install as a LaunchD task that runs every day at 11 | |
| # --uninstall-mac Uninstalls LaunchD task | |
| # | |
| # Install: | |
| # On a Mac: | |
| # just run : `sh log-lunch.sh --install-mac` | |
| # | |
| # Anything else: | |
| # Put it in some dir in your PATH (like /usr/local/bin) | |
| # Edit the LUNCH_TASK variable below per project | |
| # Set executable: `chmod +x /usr/local/bin/log-lunch` | |
| # | |
| # To run this on every weekday, just put this on your crontab (`crontab -e`) | |
| # `10 00 * * 1-5 /usr/local/bin/log-lunch` | |
| # See `man 5 crontab` for details on the format | |
| # | |
| # You can also use LaunchD (see `--install-mac`) | |
| # | |
| # Tested on both BSD (Mac) and GNU (Linux) systems. | |
| # | |
| # Caveats: | |
| # Not idiot proof - use at own peril. Debug tips below. | |
| # | |
| # Dependencies: | |
| # You need bash, grep, curl and a `base64` utility. | |
| # Optionally it can use "pick_json" (npm install -g pick_json) for interesting results | |
| # | |
| # Update: `curl -L https://gist.github.com/fatso83/cc014edf3f9877b93914e59887e58eb2/raw > ~/bin/log-lunch` | |
| # | |
| # Author: Carl-Erik Kopseng | |
| # Latest version: https://gist.github.com/fatso83/cc014edf3f9877b93914e59887e58eb2 | |
| # END | |
| ################################################################################ | |
| # This issue number must be changed per project. 43440-14 is for NorgesGruppen | |
| LUNCH_TASK="P43440-14" | |
| CREDENTIALS_FILE="$HOME/.jira-credentials-mw" | |
| OUTPUT=$(mktemp) | |
| main(){ | |
| # date format iso 8601 | |
| local ISO8601='^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}$' | |
| # Set DEBUG for debugging errors, i.e. `DEBUG=1 log-lunch` | |
| if [[ "$DEBUG" ]];then | |
| FLAGS="-v" | |
| else | |
| # Silent, but output a error if it happens | |
| FLAGS="--fail -s -S" | |
| fi | |
| # Use the current day as the date to log hours, if no date was specified | |
| if [[ $1 == "today" ]]; then | |
| DATE=$(date "+%Y-%m-%d") | |
| else | |
| DATE="$1" | |
| fi | |
| # Verify right format of date. -E flag is portable (GNU and BSD grep) | |
| if ! (echo $DATE | grep -E "$ISO8601" 1>/dev/null) ; then | |
| echo "Wrong format of date: $DATE" | |
| exit 1 | |
| fi | |
| DATA=" | |
| { | |
| \"comment\": $(create_comment), | |
| \"started\": \"${DATE}T11:30:00.000+0100\", | |
| \"timeSpentSeconds\": 1800 | |
| }" | |
| # log half an hour | |
| curl $FLAGS \ | |
| -H 'Content-Type: application/json' \ | |
| -H "Authorization: Basic $(fetch_credentials)" \ | |
| --data "$DATA" \ | |
| "https://jira.makingwaves.no/rest/api/2/issue/${LUNCH_TASK}/worklog" -o "$OUTPUT" | |
| EXIT_CODE=$? | |
| if [[ "$DEBUG" ]]; then | |
| echo "Sent data: \n$DATA" | |
| echo "\nFind full return data in $OUTPUT" | |
| # When running with DEBUG the exit code is 0 on non-200 HTTP codes | |
| # so no point in continuing | |
| exit | |
| fi | |
| if [[ $EXIT_CODE != 0 ]]; then | |
| echo "Lunch logging failed 😿.. Try setting DEBUG=1 for details" | |
| else | |
| echo "Hours logged 🐹" | |
| fi | |
| } # main end | |
| fetch_credentials(){ | |
| # don't leak the password to the surrounding shell | |
| local PASSWORD | |
| local USERNAME | |
| # store credentials if not present (base64 encoded) | |
| if [[ ! -e "$CREDENTIALS_FILE" ]]; then | |
| read -p "Username: " USERNAME | |
| read -p "Password (not shown): " -s PASSWORD | |
| echo -n "$USERNAME:$PASSWORD" | base64 > $CREDENTIALS_FILE | |
| # set the file readable only by you | |
| chmod 400 "$CREDENTIALS_FILE" | |
| fi | |
| # return contents of file | |
| cat "$CREDENTIALS_FILE" | |
| } | |
| # os x specific variables and installation routines | |
| # Based on info from | |
| # - http://www.splinter.com.au/using-launchd-to-run-a-script-every-5-mins-on/ | |
| # - http://launchd.info | |
| MACNAME=com.github.fatso.mw.lunchlogger | |
| PLIST=$HOME/Library/LaunchAgents/$MACNAME.plist | |
| installmac() { | |
| local binary=$HOME/bin/log-lunch | |
| if is_installed_mac ; then | |
| echo A task is already installed | |
| exit 1 | |
| fi | |
| # check architecture | |
| [[ $(uname) != "Darwin" ]] && echo "This is not a mac" && exit 1 | |
| # install script in a directory | |
| mkdir $HOME/bin 2>/dev/null | |
| install -m 755 $0 $binary | |
| # macs use launchd and plists. start by creating a plist describing the task | |
| cat > $PLIST << EOF | |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>Label</key> | |
| <string>$MACNAME</string> | |
| <key>ProgramArguments</key> | |
| <array> | |
| <string>$binary</string> | |
| <string>today</string> | |
| </array> | |
| <!-- Run at 09:45 AM every day --> | |
| <key>StartCalendarInterval</key> | |
| <dict> | |
| <key>Hour</key> | |
| <integer>9</integer> | |
| <key>Minute</key> | |
| <integer>45</integer> | |
| </dict> | |
| <!-- debugging --> | |
| <key>StandardErrorPath</key> | |
| <string>/tmp/$MACNAME.err</string> | |
| <key>StandardOutPath</key> | |
| <string>/tmp/$MACNAME.out</string> | |
| </dict> | |
| </plist> | |
| EOF | |
| launchctl load -w "$PLIST" | |
| if ! is_installed_mac ; then | |
| echo Not installed ok - not found in list of running tasks | |
| exit 1 | |
| fi | |
| echo Installed lunch logger service as $MACNAME | |
| echo "Run \"\$HOME/bin/log-lunch --uninstall-mac\" to uninstall service" | |
| echo "Should it not start, try opening the Console.app and filter for messages containing $MACNAME" | |
| } | |
| uninstallmac(){ | |
| launchctl unload -w "$PLIST" | |
| rm $PLIST | |
| echo "Uninstalled LaunchD task $PLIST" | |
| } | |
| is_installed_mac(){ | |
| launchctl list|grep github > /dev/null | |
| } | |
| # fetch a random food from the net, otherwise fall back to a standard text | |
| # depends on http://npmjs.com/pick_json being installed | |
| create_comment(){ | |
| if which pick_json > /dev/null; then | |
| local output=$(curl -s https://www.mezgrman.de/apps/randomfood.json | pick_json -e "text") | |
| if [[ "$output" == "" ]]; then | |
| echo '"Fetching recipe failed"' | |
| exit 1 | |
| else | |
| echo "$output" | |
| fi | |
| else | |
| echo '"Nom, nom, nom. You put the chocolate on the moose. Chocolate moose."' | |
| fi | |
| } | |
| # Print the readme at the start of this doc | |
| usage(){ | |
| echo | |
| sed -n -e '3,/^# END/s/#\(.*\)/\1/p' $0 | |
| } | |
| # Parse args | |
| if [[ $1 == "--help" || $1 == "-h" || x"$1" == x ]]; then | |
| usage | |
| exit 1 | |
| elif [[ $1 == "--install-mac" ]]; then | |
| installmac | |
| elif [[ $1 == "--uninstall-mac" ]]; then | |
| uninstallmac | |
| else | |
| main "$@" | |
| fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment