forked from DJakosa/git-to-toggl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-commit
executable file
·50 lines (40 loc) · 1.73 KB
/
post-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# Git to Toggl post-commit hook.
# Created by Denis Jakoša <denisjakosa@gmail.com>
# Modified by Tomaz Kramberger <tomazkramberger@gmail.com>
# Add your Toggl API key here
API_KEY=""
MESSAGE=$(git log -1 HEAD --pretty=format:%s)
if [ -z "$MESSAGE" ]; then
echo "Commit has no message!"
elif [ -z "$API_KEY" ]; then
echo "You need to add your Toggl API key to send commit message to current time entry!"
else
echo "Task description for Toggl: $MESSAGE"
# Get the running task from Toggl
curl -s -u $API_KEY:api_token -X GET https://www.toggl.com/api/v8/time_entries/current -H "Accept: application/json" > gittoggl.json
# Get the id of the running task
RUNNING_ID=$(cat gittoggl.json | jq '.data.id')
# Create new task if it is not running yet
if [[ "$RUNNING_ID" -eq "null" ]]; then
curl -s -u $API_KEY:api_token \
-H "Content-Type: application/json" \
-d '{"time_entry":{"description":"'${MESSAGE}'","duration":0,"start":"'$(date -u +'%Y-%m-%dT%H:%M:%SZ')'", "created_with":"curl"}}' \
-X POST https://www.toggl.com/api/v8/time_entries/start > gittoggl.json
RUNNING_ID=$(cat gittoggl.json | jq '.data.id')
NEW_DESCRIPTION="$MESSAGE"
else
# Append commit message to the description
NEW_DESCRIPTION=$(cat gittoggl.json | jq '.data.description' | tr -d '"')
NEW_DESCRIPTION="$NEW_DESCRIPTION; $MESSAGE"
fi
# Remove tmp file
rm gittoggl.json
# Build JSON for update
UPDATED_TIME_ENTRY="$(jq -n --arg ARG "$NEW_DESCRIPTION" '{time_entry: {description: $ARG} }')"
# Update description
curl -s -u $API_KEY:api_token \
-H "Content-Type: application/json" \
-d "${UPDATED_TIME_ENTRY}" \
-X PUT https://www.toggl.com/api/v8/time_entries/$RUNNING_ID > /dev/null
fi