forked from pricingassistant/buildbot-status-hipchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hipchat.py
82 lines (62 loc) · 2.87 KB
/
hipchat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from buildbot.status.base import StatusReceiverMultiService
from buildbot.status.builder import Results, SUCCESS, FAILURE
import os, urllib
class HipChatStatusPush(StatusReceiverMultiService):
def __init__(self, api_token, announce_room_id, room_id, localhost_replace=False, **kwargs):
StatusReceiverMultiService.__init__(self)
self.api_token = api_token
self.announce_room_id = announce_room_id
self.room_id = room_id
self.localhost_replace = localhost_replace
def setServiceParent(self, parent):
StatusReceiverMultiService.setServiceParent(self, parent)
self.master_status = self.parent
self.master_status.subscribe(self)
self.master = self.master_status.master
def disownServiceParent(self):
self.master_status.unsubscribe(self)
self.master_status = None
return StatusReceiverMultiService.disownServiceParent(self)
def builderAdded(self, name, builder):
return self # subscribe to this builder
def getBuildURL(self, build):
url = self.master_status.getURLForThing(build)
if self.localhost_replace:
url = url.replace("//localhost", "//%s" % self.localhost_replace)
url = urllib.quote("<a href='%s'>%s</a>" % (url, url))
return url
def buildStarted(self, builderName, build):
url = self.getBuildURL(build)
if (build.getETA() is not None):
eta = str(build.getETA()) + " seconds"
else:
eta = "unknown"
message = "Started %s: (happening) %s (eta: %s) %s" % (builderName, build.getReason(), eta, url)
color = "yellow"
notify = "1"
if (builderName == "steam-upload"):
room_id = self.announce_room_id
else:
room_id = self.room_id
os.system('curl -d "room_id=%s&from=Buildbot&message=%s&color=%s¬ify=%s" https://api.hipchat.com/v1/rooms/message?auth_token=%s&format=json&message_format=text' % (room_id, message, color, notify, self.api_token))
def buildFinished(self, builderName, build, result):
url = self.getBuildURL(build)
times = build.getTimes()
timeElapsed = str(times[1] - times[0])
notify = "1"
if result == SUCCESS:
color = "green"
status_string = "(successful) %s" % (Results[result].upper())
elif result == FAILURE:
color = "red"
status_string = "(facepalm) %s" % (Results[result].upper())
else:
color = "purple"
status_string = "(unknown) %s" % (Results[result].upper())
message = "@all Finished %s: %s (elapsed time: %s seconds) %s" % (builderName, status_string, timeElapsed, url)
if (builderName == "steam-upload"):
room_id = self.announce_room_id
else:
room_id = self.room_id
# Yes, we are in Twisted and shouldn't do os.system :)
os.system('curl -d "room_id=%s&from=Buildbot&message=%s&color=%s¬ify=%s" https://api.hipchat.com/v1/rooms/message?auth_token=%s&format=json&message_format=text' % (room_id, message, color, notify, self.api_token))