forked from danielkappelle/bitbucket-mattermost-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload.py
209 lines (146 loc) · 5.81 KB
/
payload.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def _get_default_data():
return {
'color': '#FFFFFF',
'text': 'Not implemented'
}
def _set_color_from_priority(priority):
return {
'trivial': '#205081',
'minor': 'good',
'major': 'warning',
'critical': 'danger',
'blocker': '#000000'
}.get(priority, '#FFFFFF')
def _set_color_from_state(state):
return {
'SUCCESSFUL': 'good',
'FAILED': 'danger'
}.get(state, 'warning')
def _get_issue(data, action):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
template = '%s a %s %s [#%s: %s](%s) (%s)'
issue = data.issue
resp['text'] = template % (action, issue.priority, issue.type, issue.id,
issue.title, issue.links.html.href, issue.state)
resp['color'] = _set_color_from_priority(issue.priority)
return resp
def _get_pullrequest(data, action):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
pr = data.pullrequest
pr_link = '[%s](%s)' % (pr.title, pr.links.html.href)
pr_src_link = '%s/branch/%s' % (pr.source.repository.links.html.href,
pr.source.branch.name)
pr_dst_link = '%s/branch/%s' % (pr.destination.repository.links.html.href,
pr.destination.branch.name)
pr_src = '[%s:%s](%s)' % (pr.source.repository.full_name,
pr.source.branch.name,
pr_src_link)
pr_dst = '[%s:%s](%s)' % (pr.destination.repository.full_name,
pr.destination.branch.name,
pr_dst_link)
template = '%s pull request %s\nFrom %s to %s'
resp['text'] = template % (action, pr_link, pr_src, pr_dst)
return resp
def _set_author_infos(resp, data):
if data.actor.display_name == 'Anonymous':
resp['author_name'] = data.actor.display_name
return resp
resp['author_name'] = '%s (%s)' % (data.actor.display_name,
data.actor.username)
resp['author_icon'] = data.actor.links.avatar.href
resp['author_link'] = data.actor.links.html.href
return resp
def issue_comment_created(data):
resp = _get_issue(data, 'Commented')
return resp
def issue_created(data):
resp = _get_issue(data, 'Opened')
return resp
def issue_updated(data):
resp = _get_issue(data, 'Updated')
return resp
def repo_commit_comment_created(data):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
template = 'Commented commit %s at %s'
commit_link = '[#%s](%s)' % (data.comment.commit.hash[:7],
data.comment.links.html.href)
repo_link = '[%s](%s)' % (data.repository.full_name,
data.repository.links.html.href)
resp['text'] = template % (commit_link, repo_link)
return resp
def repo_commit_status_created(data):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
ci_link = '[%s](%s)' % (data.commit_status.key, data.commit_status.url)
resp['text'] = 'Launch CI build on %s' % ci_link
return resp
def repo_commit_status_updated(data):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
ci_link = '[%s](%s)' % (data.commit_status.key, data.commit_status.url)
resp['text'] = 'CI build on %s is finished' % ci_link
resp['color'] = _set_color_from_state(data.commit_status.state)
return resp
def repo_fork(data):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
template = 'Forked %s to %s'
src_link = '[%s](%s)' % (data.repository.full_name,
data.repository.links.html.href)
dst_link = '[%s](%s)' % (data.fork.full_name, data.fork.links.html.href)
resp['text'] = template % (src_link, dst_link)
return resp
def repo_push(data):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
changesets = len(data.push.changes[0].commits)
repo_link = '[%s](%s)' % (data.repository.full_name,
data.repository.links.html.href)
branch = data.push.changes[0].new.name
commits = []
for commit in data.push.changes[0].commits:
text = '- [%s](%s): %s' % (commit.hash[:7],
commit.links.html.href,
commit.message.strip().replace('\n', ' - '))
commits.append(text)
template = 'Pushed %s changesets to %s at %s\n%s'
resp['text'] = template % (changesets, branch,
repo_link, '\n'.join(commits))
return resp
def repo_updated(data):
resp = _get_default_data()
resp = _set_author_infos(resp, data)
repo_link = '[%s](%s)' % (data.repository.full_name,
data.repository.links.html.href)
resp['text'] = 'Updated repo %s' % repo_link
return resp
def pullrequest_approved(data):
resp = _get_pullrequest(data, 'Approved')
return resp
def pullrequest_created(data):
resp = _get_pullrequest(data, 'Opened')
return resp
def pullrequest_fulfilled(data):
resp = _get_pullrequest(data, 'Merged')
return resp
def pullrequest_rejected(data):
resp = _get_pullrequest(data, 'Rejected')
return resp
def pullrequest_updated(data):
resp = _get_pullrequest(data, 'Updated')
return resp
def pullrequest_unapproved(data):
resp = _get_pullrequest(data, 'Unapproved')
return resp
def pullrequest_comment_created(data):
resp = _get_pullrequest(data, 'Left a comment on the')
return resp
def pullrequest_comment_updated(data):
resp = _get_pullrequest(data, 'Updated a comment he left on the')
return resp
def pullrequest_comment_deleted(data):
resp = _get_pullrequest(data, 'Deleted a comment he left on the')
return resp