Skip to content

Instantly share code, notes, and snippets.

@ivan-pinatti
Created June 3, 2019 15:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan-pinatti/5355402e070cee16a23b94b667317386 to your computer and use it in GitHub Desktop.
Save ivan-pinatti/5355402e070cee16a23b94b667317386 to your computer and use it in GitHub Desktop.
Git - Check stale branches and alert on Slack - #git #stale #branches #slack #sanity
#!/usr/bin/env bash
: ' Script that checks stale branches based on a specific date on a git
repository and send an alert message on a Slack channel
'
# check if debug flag is set
if [ "${DEBUG}" = true ]; then
set -x # enable print commands and their arguments as they are executed.
export # show all declared variables (includes system variables)
whoami # print current user
else
# unset if flag is not set
unset DEBUG
fi
# bash default parameters
set -o errexit # make your script exit when a command fails
set -o pipefail # exit status of the last command that threw a non-zero exit code is returned
set -o nounset # exit when your script tries to use undeclared variables
# check binaries
__BASENAME=$(which basename)
__CURL=$(which curl)
__DATE=$(which date)
__GIT=$(which git)
__HEAD=$(which head)
__SED=$(which sed)
# parameters
__date=${1:-"4 weeks ago"}
__branch_exclusion_list=${2:-"dev|stage|master"}
__slack_channel_or_person="${3:-"#my-ci-channel"}"
__msg_from="${4:-"bitbucket-sanity-check-bot"}"
__msg_alert_icon="${5:-":x:"}"
__slack_webhook_url="${6:-"https://hooks.slack.com/services/ASD4456S5/AHCE54563/ai85930591zBNasd456qwett"}"
# variables
readonly __date_epoch=$(${__DATE} --date="${__date}" +%s)
readonly __git_repo_name=$(${__BASENAME} $(${__GIT} rev-parse --show-toplevel))
# functions
function push_to_slack {
# parameters
local __from="${1:-}"
local __channel_or_person="${2:-}"
local __text="${3:-}"
# push to slack via webhook
${__CURL} --request POST \
--data-urlencode \
"payload={\"username\": \"${__from}\",
\"channel\": \"${__channel_or_person}\",
\"text\": \"${__text}\",
\"as_user\": \"false\",
\"link_names\": \"true\"
}" \
"${__slack_webhook_url}"
} # end of push_to_slack
# flow
for __branch in $(${__GIT} branch --remote); do
# check if it is not one of the environment branches/tags
if [[ ! ${__branch} =~ ^.*(${__branch_exclusion_list}|->|origin\/HEAD)$ ]]; then
# get the last commit date in UNIX epoch format
__last_commit_date_epoch=$(${__GIT} show ${__branch} --name-only --pretty="format:%at" | ${__HEAD} -1)
# check if the last commit is older than the reference date
if [[ ${__last_commit_date_epoch} < ${__date_epoch} ]]; then
__branch_name=$(echo ${__branch} | ${__SED} 's/^origin\///')
__last_commit_author=$(${__GIT} show ${__branch} --name-only --pretty="format: %ci by %an" | ${__HEAD} -n 1)
push_to_slack "${__msg_from}" \
"${__slack_channel_or_person}" \
"${__msg_alert_icon} Repository *${__git_repo_name}* ${__msg_alert_icon}\nBranch *${__branch_name}* is older than *${__date}*\n Last commit *${__last_commit_author}* \n@channel please check."
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment