-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
64 lines (49 loc) · 2.41 KB
/
__init__.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
import sys
from .lib.slack import Slack
from .lib import git
class GitSlackHook(object):
def __init__(self, url='', branch='', remote='',
message='New release deployed by %s', verbose=False):
if not url: sys.exit('Slack: url is required - aborting.')
self.client = Slack(url)
self.branch = branch
self.remote = remote
self.message = message
self.verbose = verbose
def pre_push(self, trello_cards=None, trello_colors=None):
# only talk to Slack if one of the specified branches is being pushed
# e.g. take action on master but not on a feature branch
push_branch = git.pushBranch()
if self.branch and push_branch != self.branch:
if self.verbose:
print('Slack: pushing unspecified branch "' + push_branch + '" skips notification')
return
# also skip if a remote is specified but we're not pushing to it
# e.g. pushing to github but release is to heroku
push_remote = git.pushRemote()
if self.remote and push_remote != self.remote:
if self.verbose:
print('Slack: pushing to unspecified remote "' + push_remote + '" skips notification')
return
username = git.currentUser()
text = self.message % username
attachments = []
if trello_cards:
text += ':'
for card in trello_cards:
fallback = '#' + str(card['idShort']) + ' - ' + card['name']
card_text = '<' + card['url'] + '|#' + str(card['idShort']) + '> ' + card['name']
attachment = {'fallback': fallback, 'text': card_text}
if trello_colors and 'labels' in card:
# just pick the first label we can find to color since Slack only supports one
for label in card['labels']:
color_name = label['color']
if color_name in trello_colors:
attachment['color'] = trello_colors[color_name]
break
elif self.verbose:
print('Slack: no value found for color named "%s"' % color_name)
attachments.append(attachment)
if self.verbose:
print('Slack: ' + text + ' ' + str(len(attachments)) + ' card(s)')
self.client.postMessage(text, attachments=attachments)