Skip to content

Instantly share code, notes, and snippets.

@emirhg
Last active July 3, 2021 11:36
Show Gist options
  • Save emirhg/8adeaaca039d802a1f79f87d2253a761 to your computer and use it in GitHub Desktop.
Save emirhg/8adeaaca039d802a1f79f87d2253a761 to your computer and use it in GitHub Desktop.
prepare-commit-msg
#!/bin/zsh
# Author: "Emir Herrera" <emir.herrera@gmail.com>
# Donations: http://paypal.me/emirhg
#
# A GIT Hook to Pregenerate a Commit Message with an elapsed working time taken from the time tracker ActivityWatch. A `ref #123 @1.h` message like is generated taking the activity number from the branch name and te time from ActivityWatch Web API. Requires JQ in order to add all the events recorded by ActivityWatch
#
# Usage:
#
# copy this file under "${GIT_WORKING_TREE}/.git/hooks/prepare-commit-msg and set execution permitions
#
# Requirements:
#
# ActivityWatch (https://activitywatch.net/)
# JQ (https://stedolan.github.io/jq/)
# OpenProject (Optional - https://www.openproject.org/)
# Zenity (https://gitlab.gnome.org/GNOME/zenity)
#
# It uses OpenProject commit message format (1)
# to register working time & costs assosiated to a work package
# Working time is the result of the recorded activity on the host since the last commit.
# in case the file /tmp/check-in-work exists, it uses that timestamp for the working window
# The events can be manually selected, with a preset test filter in order to exclude certain applications. The dialog is handled by Zenity.
# The tracked activities set is defined by the check-in-work file
#
# References:
#
# (1) OpenProject Documentation, 12/November/2020 - https://docs.openproject.org/user-guide/time-and-costs/time-tracking/#logging-time-via-commit-message
#
# This script is distributed as it is without any warranty.
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
TMP_DATA_FILE='/tmp/activitywatch-last-commit-events.json'
LAST_TIMESTAMP=`cat /tmp/check-in-work 2>/dev/null`
# Only if the COMMIT_SOURCE is different of an existing commit, append teh activity data
if [ "${COMMIT_SOURCE}" != "commit" ]; then
if [ -z $LAST_TIMESTAMP ]
then
LAST_TIMESTAMP=$(git --no-pager log --branches -n1 --format=%ad --date=format:'%Y-%m-%dT%H:%M:%S%z' --author=`git config user.email`)
fi
curl -sX GET "http://localhost:5600/api/0/buckets/aw-watcher-window_neuron/events?start=${LAST_TIMESTAMP}" -H "accept: application/json" -o $TMP_DATA_FILE
# Select activities
EXCLUSIONS='"(Private Browsing)|Facebook"'
SELECTED_EVENTS=();
while IFS= read -r line; do [[ ! -z "$line" ]] && SELECTED_EVENTS+="$line" || SELECTED_EVENTS+=" "
done < <(jq -r "to_entries|map([\"\(\"\(.value.data.app) \(.value.data.title)\" | test(${EXCLUSIONS}) | not )\", .key, \"\(.value.data.app)\", \" \(.value.data.title)\", .value.timestamp, .value.duration]) | .[][]" $TMP_DATA_FILE)
FILTER=$(zenity --list --checklist --width=1080 --height=450 \
--column="Reportar" \
--column="Índice" \
--column="Aplicación" \
--column="Título" \
--column="Hora" \
--column="Duracion" \
--hide-column=2 --separator=',' $SELECTED_EVENTS)
# An error pops up when running `git rebase`
# The error should not occur anymore
ELAPSED_TIME=$(jq "[.[$FILTER]] | map(.duration) | add | . / 3600" $TMP_DATA_FILE)
echo $ELAPSED_TIME
COMMENTED_ACTIVITY=$(jq "[.[${FILTER}]] | map({app:.data.app, title: .data.title, duration: .duration})" $TMP_DATA_FILE | sed 's/^/# /')
# An error pops up when running `git rebase`
# The error should not occur anymore
TASK_NUMBER=$(git symbolic-ref HEAD | sed -n -e "s/.*\/\(feature\|hotfix\|bugfix\|warmfix\)\/[a-zA-Z]*[^0-9]*\([0-9]\+\).*/\2/p")
if [ ! -z $TASK_NUMBER ]; then
TASK_MESSAGE="refs #${TASK_NUMBER} ";
if [ ! -z $ELAPSED_TIME ]; then
ELAPSED_TIME_MESSAGE="@${ELAPSED_TIME} since ${LAST_TIMESTAMP}"
fi
fi
if [ "${COMMIT_SOURCE}" = "message" ]; then
echo "${TASK_MESSAGE}${ELAPSED_TIME_MESSAGE}" >> $COMMIT_MSG_FILE
else
echo "${COMMENTED_ACTIVITY}" >> $COMMIT_MSG_FILE
echo "${TASK_MESSAGE}${ELAPSED_TIME_MESSAGE}\n$(cat $COMMIT_MSG_FILE)" > $COMMIT_MSG_FILE
fi
>/tmp/check-in-work
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment