Skip to content

Instantly share code, notes, and snippets.

@rey
Last active January 9, 2019 18:25
Show Gist options
  • Save rey/663de9e00f73212a963b5697604b2747 to your computer and use it in GitHub Desktop.
Save rey/663de9e00f73212a963b5697604b2747 to your computer and use it in GitHub Desktop.
#!/bin/bash
# harvest.sh by Rey Dhuny
#
# About
# --
# Use harvest.sh to generate a quick-and-dirty summary of the current month's hours used for a single project.
#
# Disclaimer
# --
# Don't use this for anything mission critical.
#
# Dependency
# --
# * This needs jq that you can install with homebrew: `brew install jq`
#
# Usage
# --
# 1. Download this script to your computer, call it harvest.sh
# 2. Open harvest.sh in your favourite text editor and fill in the following variables:
# i. YOUR_TOKEN get this from https://id.getharvest.com/developer
# ii. ACCOUNT_ID get this from https://id.getharvest.com/developer
# iii. PROJECT_ID get this from the Project reports URL slug e.g. the PROJECT_ID of https://kanye.harvestapp.com/reports/123456 is 123456
# 3. Open your shell, cd to the directory where harvest.sh lives and run `bash harvest.sh`
# Set this to true to show debugging. You probably dont need to change this.
debug=false
# Visit https://id.getharvest.com/developers to get this token
YOUR_TOKEN="ENTER_YOUR_TOKEN_HERE"
# Visit https://id.getharvest.com/developers to get this account ID
ACCOUNT_ID="ENTER_YOUR_ACCOUNT_ID_HERE"
# Get this from the Project report's URL slug e.g. the PROJECT_ID of https://kanye.harvestapp.com/reports/123456 is 123456
PROJECT_ID="ENTER_YOUR_PROJECT_ID_HERE"
# Only return hours from the 1st of the current month. You don't need to change this.
CURRENT_MONTH_DATE=$(date +"%Y-%m-01")
# Runs from the /tmp/ folder. You don't need to change this.
WORKING_FOLDER="/tmp/harvest"
if [ ${debug} = true ]; then
echo
echo " * * *"
echo
echo -e "YOUR_TOKEN \n ${YOUR_TOKEN}"
echo
echo -e "ACCOUNT_ID \n ${ACCOUNT_ID}"
echo
echo -e "PROJECT_ID \n ${PROJECT_ID}"
echo
echo -e "CURRENT_MONTH_DATE \n ${CURRENT_MONTH_DATE}"
echo
echo " * * *"
echo
fi
# Create workspace
clear
mkdir -p ${WORKING_FOLDER} && cd ${WORKING_FOLDER} && pwd
echo
# Request
curl \
--silent \
-H "Harvest-Account-ID: ${ACCOUNT_ID}" \
-H "Authorization: Bearer ${YOUR_TOKEN}" \
-H "User-Agent: Harvest API Example" \
"https://api.harvestapp.com/api/v2/time_entries?project_id=${PROJECT_ID}&from=${CURRENT_MONTH_DATE}" \
> response.json
# Get meta
PROJECT_NAME=`cat response.json | jq ".time_entries[1] | .project.name" | sed 's/\"//g'`
CLIENT_NAME=`cat response.json | jq ".time_entries[1] | .client.name" | sed 's/\"//g'`
# Get hour breakdown
cat response.json | jq ".time_entries[] | .hours" > hours.all.file
cat response.json | jq ".time_entries[] | select (.billable == true) | .hours" > hours.billable.file
cat response.json | jq ".time_entries[] | select (.billable == false) | .hours" > hours.nonbillable.file
TOTAL_HOURS=`awk '{s+=$1} END {print s}' hours.all.file`
TOTAL_BILLABLE_HOURS=`awk '{s+=$1} END {print s}' hours.billable.file`
TOTAL_NON_BILLABLE_HOURS=`awk '{s+=$1} END {print s}' hours.nonbillable.file`
# Check for 0 hours
if [ -z "${TOTAL_BILLABLE_HOURS}" ]; then
TOTAL_BILLABLE_HOURS=0
fi
if [ -z "${TOTAL_NON_BILLABLE_HOURS}" ]; then
TOTAL_NON_BILLABLE_HOURS=0
fi
echo
echo "\`\`\`"
echo "Hours Report for $(date +"%B %Y") (Generated $(date +"%a, %d %b @ %H:%M:%S %Z"))"
echo "${PROJECT_NAME} - ${CLIENT_NAME}"
echo "https://10up.harvestapp.com/reports/projects/${PROJECT_ID}"
echo
echo "---"
echo "Total: ${TOTAL_HOURS} hours"
echo "Billable: ${TOTAL_BILLABLE_HOURS} hours"
echo "Non-billable: ${TOTAL_NON_BILLABLE_HOURS} hours"
echo "---"
echo "\`\`\`"
echo
TOTAL_ITEMS=`cat hours.all.file | wc -l | sed -e "s/^ *//"`
echo "There are ${TOTAL_ITEMS} time entries"
echo
echo "--"
echo
for((i=0; i < ${TOTAL_ITEMS}; i++)); do
ID=`cat response.json | jq ".time_entries[${i}] | .id" | sed 's/\"//g'`
USER=`cat response.json | jq ".time_entries[${i}] | .user.name" | sed 's/\"//g'`
DATE_LOGGED=`cat response.json | jq ".time_entries[${i}] | .spent_date" | sed 's/\"//g'`
HOURS=`cat response.json | jq ".time_entries[${i}] | .hours" | sed 's/\"//g'`
PERMALINK=`cat response.json | jq ".time_entries[${i}] | .external_reference.permalink" | sed 's/\"//g'`
echo "${USER} ${DATE_LOGGED} ${HOURS} ${PERMALINK}" >> report.file
done
cat report.file | column -t
# Clean up
rm -rd ${WORKING_FOLDER}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment