Skip to content

Instantly share code, notes, and snippets.

@mokemokechicken
Last active August 31, 2016 01:42
Show Gist options
  • Save mokemokechicken/07e2b868fd569043e005 to your computer and use it in GitHub Desktop.
Save mokemokechicken/07e2b868fd569043e005 to your computer and use it in GitHub Desktop.
SlackBot for rcov coverage report
COVERAGE_DIR=${COVERAGE_DIR:-coverage}
REPORT_NAME=${REPORT_NAME:-index.html}
author=$(git log --format='%an <%ae>' -1 $id_new | sed 's/\\/\\\\/g;s/\"/\\"/g')
message=$(git log --format=%s -1 $id_new | sed 's/\\/\\\\/g;s/\"/\\"/g')
latest="${COVERAGE_DIR}/${REPORT_NAME}"
[ -e "$COVERAGE_DIR" ] || mkdir "$COVERAGE_DIR"
cp "$1" "$latest"
## detect current/previous commit
new_commit=$(git log --format=%H -1)
pre_commit=$(git log --format=%H -1 HEAD^)
pre_commit_list=$(git log --format=%H -100 HEAD^)
for c in $pre_commit_list
do
if [ -e "${latest}.${c}" ]; then
pre_commit=$c
break
fi
done
## backup report
back_new="${latest}.${new_commit}"
back_old="${latest}.${pre_commit}"
[ -e "$back_new" ] || cp "$latest" "$back_new"
# delete old reports
ls -t "${COVERAGE_DIR}" | grep "${REPORT_NAME}" | sed '1,100d' | xargs -I % rm %
## calculate rates
scan_coverage() {
grep "class='coverage_total'" | perl -pe 's/.*>([0-9.]+)%.*/$1/'
}
rate_new=$(cat "$latest" | scan_coverage)
rate_old=0
if [ -e "$back_old" ]; then
rate_old=$(cat "$back_old" | scan_coverage)
fi
rate_diff=$(ruby -e 'puts "%.2f" % (ARGV[0].to_f - ARGV[1].to_f)' -- $rate_new $rate_old)
## detect not-changed/increased/decreased
mode=0
if [ $(echo "$rate_diff > 0" | bc) -eq 1 ]; then
mode=1
elif [ $(echo "$rate_diff < 0" | bc) -eq 1 ]; then
mode=2
fi
## construct messages
mes=("DID NOT CHANGE" "INCREASED" "DECREASED")
color=("#a0a0a0" "good" "danger")
cat >${COVERAGE_DIR}/.payload <<_EOT_
{
"attachments": [
{
"channel": "${SLACK_CHENNEL}",
"fallback": "Coverage ${mes[$mode]} (${rate_new}%)",
"text": "*${mes[$mode]}* (${rate_old:-0}% -> ${rate_new}%)",
"pretext": "${JOB_NAME} - #${BUILD_NUMBER} Coverage report (<${JENKINS_URL}/job/${JOB_NAME}/${BUILD_NUMBER}/rcov/|Open>)",
"color": "${color[$mode]}",
"mrkdwn_in": ["pretext", "text", "fields"],
"fields": [
{
"value": "Commit: \`${new_commit}\`",
"short": false
},
{
"value": "Author: ${author}",
"short": false
},
{
"value": " ${message}",
"short": false
}
]
}
]
}
_EOT_
## post to slack
curl --data-urlencode payload@${COVERAGE_DIR}/.payload "${SLACK_ENDPOINT}"
rm ${COVERAGE_DIR}/.payload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment