Skip to content

Instantly share code, notes, and snippets.

@stewartpark
Last active August 7, 2017 12:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stewartpark/c398439d5ee26e2aa2ac to your computer and use it in GitHub Desktop.
Save stewartpark/c398439d5ee26e2aa2ac to your computer and use it in GitHub Desktop.
Automated test coverage report. (Jenkins<3Slack)
"""
Slack coverage.py notifier.
Stewart Park <stewartpark92@gmail.com>
It reports test coverage to a Slack channel as a Jenkins bot.
Add this as a post-build/build step once coverage.py generates the html report.
Once everything is properly set up, you can add a badge on your README like below:
[![Coverage.py](http://<jenkins-host>/job/<job-name>/coveragepy/badge.svg)](http://<jenkins-host>/job/<job-name>/coveragepy/)
"""
import os, requests, json
COMPANY_NAME = "<your-company-subdomain>"
JENKINS_SLACK_TOKEN = "<your-jenkins-slack-token>"
CHANNEL_TO_REPORT = "#<channel-name>"
COVER_FILE_PATH = os.path.join(os.environ.get("WORKSPACE"), "cover/index.html")
COVER_BADGE_PATH = os.path.join(os.environ.get("WORKSPACE"), "cover/badge.svg") # You can publish this with ShiningPanda
COVER_PERCENT = open(COVER_FILE_PATH).read().split('pc_cov">')[1].split("<")[0]
COVER_PERCENT_INT = int(COVER_PERCENT[:-1])
if COVER_PERCENT_INT < 70:
EMOTICON = ":poop:"
COLOR = "warning"
elif COVER_PERCENT_INT < 80:
EMOTICON = ":rage:"
COLOR = "warning"
elif COVER_PERCENT_INT < 90:
EMOTICON = ":-1:"
COLOR = "warning"
elif COVER_PERCENT_INT < 95:
EMOTICON = ":okay_hand:"
COLOR = "good"
elif COVER_PERCENT_INT == 100:
EMOTICON = ":two_hearts:"
COLOR = "good"
else:
EMOTICON = ":+1:"
COLOR = "good"
TEXT = EMOTICON + " Test coverage: " + COVER_PERCENT
PAYLOAD = {
"attachments": [
{
"color": COLOR,
"fallback": TEXT,
"mrkdwn_in": ["text"],
"fields": [
{"short": False, "value": TEXT}
]
}
],
"channel": CHANNEL_TO_REPORT
}
print requests.post(
"https://" + COMPANY_NAME + ".slack.com/services/hooks/jenkins-ci?token=" + JENKINS_SLACK_TOKEN,
data={"payload": json.dumps(PAYLOAD)}
).text
open(COVER_BADGE_PATH, "w").write(requests.get("https://img.shields.io/badge/coverage-" + COVER_PERCENT + "-red.svg").text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment