Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# === bitbucket_user_commits_check ===
# checks whether there are any commits for user 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=/srv/scripts_data/bitbucket_user_commits_check.log
if [ -z "$1" ]; then
echo "usage: $0 user-slug1 [user-slug2]..."
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
}
# usage: bitbicketQuery api-route jq-filter
function bitbucketQuery() {
curl -s -H "Authorization: Basic ${bitbucketAuth}" "${bitbucketURL}$1" | jq -M -c -r "$2" 2>/dev/null
}
userSlugs=( "$@" )
declare -A users
declare -A usersNames
for slug in "${userSlugs[@]}"; do
users[$slug]=false
usersNames[$slug]=$(bitbucketQuery "/rest/api/1.0/users/${slug}" '.displayName')
if [ -z "${usersNames[$slug]}" ] || [ "${usersNames[$slug]}" == 'null' ]; then
echo "user $slug is not found!"
exit 2
fi
done
readarray -t projects < <(bitbucketQuery "/rest/api/1.0/projects" '.values[].key')
for project in "${projects[@]}"; do
readarray -t repos < <(bitbucketQuery "/rest/api/1.0/projects/${project}/repos" '.values[].slug')
for repo in "${repos[@]}"; do
readarray -t branches < <(bitbucketQuery "/rest/api/1.0/projects/${project}/repos/${repo}/branches" '.values[].displayId')
for branch in "${branches[@]}"; do
readarray -t commits < <(bitbucketQuery "/rest/api/1.0/projects/${project}/repos/${repo}/commits/?until=${branch}&limit=50" '.values[] | "\(.committer.slug)\t\(.committerTimestamp)"')
for commit in "${commits[@]}"; do
commitUser=${commit%$'\t'*}
commitTime=${commit##*$'\t'}
if [[ $commitTime -ge $(unixtimeFrom) ]]; then
for user in "${!users[@]}"; do
if [ "$user" == "$commitUser" ]; then
users[$user]=true
break
fi
done
fi
done
done
done
done
for user in "${!users[@]}"; do
if [ "${users[$user]}" == "false" ]; then
toLog "sending alert about ${usersNames[$user]} (${user})"
curl -s -X POST --data-urlencode "payload={\"text\":\"${usersNames[$user]} (${user}) не сделал ни одного коммита с начала прошлого рабочего дня.\"}" "$slackHook" > /dev/null
fi
done
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.