Last active
April 2, 2019 20:13
-
-
Save alvarow/d369311b8473a093c9d98373326e7299 to your computer and use it in GitHub Desktop.
Send status of a given Jenkins build to a Rocket.chat webhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# -*- coding: utf-8 -*- | |
import json | |
import sys | |
import urllib2 | |
proxy = urllib2.ProxyHandler({ "http": "127.0.0.1:3128", "https": "127.0.0.1:3128" }) | |
jenkinsUrl = "http://labs-jenkins/jenkins/job/" | |
rocketUrl ="https://your-rocket.chat/hooks/sasdfafsa" | |
if len( sys.argv ) > 1 : | |
jobName = sys.argv[1] | |
else : | |
print "Please provide Jenkins job name (eServer, eWeb, etc)" | |
sys.exit(1) | |
try: | |
jenkinsStream = urllib2.urlopen( jenkinsUrl + jobName + "/lastBuild/api/json" ) | |
except urllib2.HTTPError, e: | |
print "URL Error: " + str(e.code) | |
print " (job name [" + jobName + "] probably wrong)" | |
sys.exit(2) | |
try: | |
buildStatusJson = json.load( jenkinsStream ) | |
except: | |
print "Failed to parse json" | |
sys.exit(3) | |
#print json.dumps( buildStatusJson ) | |
#sys.exit(0) | |
if buildStatusJson.has_key( "result" ): | |
# if buildStatusJson.has_key( "parameters" ): | |
# action_pos = 1 | |
# else : | |
# action_pos = 0 | |
jobURL = buildStatusJson["url"] | |
jobID = buildStatusJson["id"] | |
jobRequestor = buildStatusJson["actions"][1]["causes"][0]["shortDescription"] | |
jobResult = buildStatusJson["result"] | |
print "[" + jobName + "] " + jobURL | |
print "[" + jobName + "] lastest build is: " + jobID | |
print "[" + jobName + "] requested by: " + jobRequestor | |
print "[" + jobName + "] build status: " + jobResult | |
if buildStatusJson["result"] == "SUCCESS" : | |
rocketMessage = 'payload={"icon_url":"https://jenkins.io/images/226px-Jenkins_logo.svg","text":"#HAPPYPANDA for %s","attachments":[{"title":"Jenkins Build status for %s","title_link":"%s","text":"%s build %s, %s, has status %s","image_url":"http://labs-jenkins/happy.png","color":"#764FA5"}]}' % (jobName, jobName, jobURL, jobName, jobID, jobRequestor, jobResult) | |
else: | |
rocketMessage = 'payload={"icon_url":"https://jenkins.io/images/226px-Jenkins_logo.svg","text":"#SADPANDA for %s","attachments":[{"title":"Jenkins Build status for %s","title_link":"%s","text":"%s build %s, %s, has status %s","image_url":"http://labs-jenkins/sad.png","color":"#764FA5"}]}' % (jobName, jobName, jobURL, jobName, jobID, jobRequestor, jobResult) | |
else: | |
print "No result in the json response from Jenkins" | |
sys.exit(4) | |
#print rocketMessage | |
opener = urllib2.build_opener(proxy) | |
urllib2.install_opener(opener) | |
try: | |
urllib2.urlopen(rocketUrl,rocketMessage) | |
except urllib2.HTTPError, e: | |
print "POST to Rocket failed: " + str(e.code) + " - check Rocket webhook key" | |
sys.exit(5) | |
sys.exit(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment