Skip to content

Instantly share code, notes, and snippets.

@aslakhellesoy
Last active February 19, 2021 10:03
Show Gist options
  • Save aslakhellesoy/de419d6ded6249e8a68c05d296314bf2 to your computer and use it in GitHub Desktop.
Save aslakhellesoy/de419d6ded6249e8a68c05d296314bf2 to your computer and use it in GitHub Desktop.
You can have a little tech debt for a short time. After that the bumbailiff will be up your bum.
#!/usr/bin/env bash
#
# The bumbailiff allows the team to take up a small amount of technical debt
# (TODOs in the code) for a limited period. After that period the script fails.
#
# Originally written by Aslak Hellesoy
#
set -ef -o pipefail
IFS=$'\n' # make newlines the only separator
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BOLD='\033[1m'
NC='\033[0m'
YOUNG=7
OLD=14
CREDIT_LIMIT=${1:-100}
TODO_PATTERN="TODO"
now_seconds_since_epoch_utc=$(date +%s)
todo_age_days_total=0
todo_count=0
leftpad() {
printf "%+3s\n" $1
}
for todo_file in $(git grep --files-with-matches "${TODO_PATTERN}"); do
if [ "$0" = "${todo_file}" ]; then
continue
fi
for todo_line in $(git blame "${todo_file}" -s -l -f | grep "${TODO_PATTERN}"); do
sha=$(echo "${todo_line}" | cut -c1-40)
commit_seconds_since_epoch_utc=$(git show -s --format="%at" "${sha}")
todo_age_days=$(( (now_seconds_since_epoch_utc - commit_seconds_since_epoch_utc) / 86400 ))
todo_age_days_total=$(( $todo_age_days_total + todo_age_days ))
todo_count=$(( todo_count + 1 ))
# Formatting
if ((${todo_age_days}<=${YOUNG})); then
color="${GREEN}"
elif ((${todo_age_days}<=${OLD})); then
color="${ORANGE}"
else
color="${RED}"
fi
path_length=${#todo_file}
cutoff=$((40 + 2 + path_length))
todo_line_stripped=$(echo "${todo_line}" | cut -c${cutoff}-)
prefix="${color}$(leftpad ${todo_age_days}) days:${NC}"
echo -e "${prefix} ${BOLD}${todo_file}${NC}${todo_line_stripped}"
done
done
echo
echo -e "πŸ’΅πŸ’΅πŸ’΅πŸ’΅πŸ’΅ Tech debt: ${BOLD}${todo_count}${NC} TODOs with a total age of ${BOLD}${todo_age_days_total}${NC} days. Credit limit: ${BOLD}${CREDIT_LIMIT}${NC}. πŸ’΅πŸ’΅πŸ’΅πŸ’΅πŸ’΅"
if ((${todo_age_days_total}>=${CREDIT_LIMIT})); then
overdraft=$((todo_age_days_total - CREDIT_LIMIT))
echo -e "πŸ’΅πŸ’΅πŸ’΅πŸ’΅πŸ’΅ ${RED}Overdraft: ${NC}${BOLD}${overdraft}${NC} ${RED}TODO days${NC} πŸ’΅πŸ’΅πŸ’΅πŸ’΅πŸ’΅"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment