Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# === bitbucket_commits_check ===
# checks whether there are any commits in the provided repo (all branches) since the last working day
# written by deseven, 2019
# http://deseven.info
# bitbucket base URL
bitbucketURL="https://example.com"
# bitbucket authentication (base64 of user:password)
bitbucketAuth=""
# slack hook URL
slackHook="https://example.com/hooks/abcdef123456"
# log file for debugging purposes
logFile=/var/log/bitbucket_commits_check.log
if [ -z "$1" ]; then
echo "usage: $0 bitbucket-project/bitbucket-repo"
exit 1
fi
function toLog() {
#echo "$@"
echo -e `date +'[%d.%m.%Y %H:%M:%S]'` "$@" >> "$logFile"
}
function unixtimeFrom() {
dayOfWeek=$(date +'%w')
case $dayOfWeek in
[2-5])
echo $(date +'%s' -d "1 day ago 00:00")000
;;
1)
echo $(date +'%s' -d "3 day ago 00:00")000
;;
*)
echo 0
;;
esac
}
bitbucketProject=$(echo "$1" | cut -f1 -d'/')
bitbucketRepo=$(echo "$1" | cut -f2 -d'/')
# getting all branches for a repo
readarray -t branches < <(curl -s -H "Authorization: Basic ${bitbucketAuth}" "${bitbucketURL}/rest/api/1.0/projects/${bitbucketProject}/repos/${bitbucketRepo}/branches" | jq -M -c -r '.values[].displayId' 2>/dev/null)
for branch in "${branches[@]}"; do
timeFrom=$(unixtimeFrom)
toLog "checking branch $branch of ${bitbucketProject}/${bitbucketRepo}..."
lastCommitTime=$(curl -s -H "Authorization: Basic ${bitbucketAuth}" "${bitbucketURL}/rest/api/1.0/projects/${bitbucketProject}/repos/${bitbucketRepo}/commits/?until=${branch}&limit=1" | jq -M -c -r '.values[].authorTimestamp' 2>/dev/null)
if [[ $lastCommitTime -ge $timeFrom ]]; then
commitFound="true"
toLog "$lastCommitTime >= $timeFrom - looks ok"
else
toLog "$lastCommitTime < $timeFrom - that's bad"
fi
done
if [ "$commitFound" != "true" ]; then
toLog "no valid commits found, sending notification..."
curl -s -X POST --data-urlencode "payload={\"text\":\"В репозитории ${bitbucketProject}/${bitbucketRepo} с начала прошлого рабочего дня не было ни одного коммита!\"}" "$slackHook" > /dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.