This repository has been archived by the owner on Jan 23, 2019. It is now read-only.
forked from pricingassistant/buildbot-status-hipchat
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathslack.py
136 lines (114 loc) · 4.54 KB
/
slack.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from buildbot.status.base import StatusReceiverMultiService
from buildbot.status.builder import Results, SUCCESS
import requests
import json
class SlackStatusPush(StatusReceiverMultiService):
"""
Sends messages to a Slack.io channel when each build finishes with a handy
link to the build results.
"""
def __init__(self, weburl,
localhost_replace=False, username=None,
icon=None, notify_on_success=True, notify_on_failure=True,
**kwargs):
"""
Creates a SlackStatusPush status service.
:param weburl: Your Slack weburl
:param localhost_replace: If your Buildbot web fronted doesn't know
its public address it will use "localhost" in its links. You can
change this by setting this variable to true.
:param username: The user name of the "user" positing the messages on
Slack.
:param icon: The icon of the "user" posting the messages on Slack.
:param notify_on_success: Set this to False if you don't want
messages when a build was successful.
:param notify_on_failure: Set this to False if you don't want
messages when a build failed.
"""
StatusReceiverMultiService.__init__(self)
self.weburl = weburl
self.localhost_replace = localhost_replace
self.username = username
self.icon = icon
self.notify_on_success = notify_on_success
self.notify_on_failure = notify_on_failure
self.watched = []
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
for w in self.watched:
w.unsubscribe(self)
return StatusReceiverMultiService.disownServiceParent(self)
def builderAdded(self, name, builder):
self.watched.append(builder)
return self # subscribe to this builder
def buildFinished(self, builder_name, build, result):
if not self.notify_on_success and result == SUCCESS:
return
if not self.notify_on_failure and result != SUCCESS:
return
build_url = self.master_status.getURLForThing(build)
if self.localhost_replace:
build_url = build_url.replace("//localhost", "//{}".format(
self.localhost_replace))
source_stamps = build.getSourceStamps()
branch_names = ', '.join([source_stamp.branch for source_stamp in source_stamps])
repositories = ', '.join([source_stamp.repository for source_stamp in source_stamps])
responsible_users = ', '.join(build.getResponsibleUsers())
revision = ', '.join([source_stamp.revision for source_stamp in source_stamps])
project = ', '.join([source_stamp.project for source_stamp in source_stamps])
if result == SUCCESS:
status = "Success"
color = "good"
else:
status = "Failure"
color = "failure"
message = "New Build for {project} ({revision})\nStatus: *{status}*\nBuild details: {url}".format(
project=project,
revision=revision,
status=status,
url=build_url
)
fields = []
if responsible_users:
fields.append({
"title": "Commiters",
"value": responsible_users
})
if repositories:
fields.append({
"title": "Repository",
"value": repositories,
"short": True
})
if branch_names:
fields.append({
"title": "Branch",
"value": branch_names,
"short": True
})
payload = {
"text": " ",
"attachments": [
{
"fallback": message,
"text": message,
"color": color,
"mrkdwn_in": ["text", "title", "fallback"],
"fields": fields
}
]
}
if self.username:
payload['username'] = self.username
if self.icon:
if self.icon.startswith(':'):
payload['icon_emoji'] = self.icon
else:
payload['icon_url'] = self.icon
requests.post(self.weburl, data=json.dumps(payload))