forked from danigm/gitlab-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatters.py
More file actions
398 lines (301 loc) · 13.8 KB
/
formatters.py
File metadata and controls
398 lines (301 loc) · 13.8 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python3
import re
# generic event called from webhooks set by admins (lacking info)
def formatRepoUpdateMsg(data):
changes = []
for change in data['changes']:
if 'ref' in change:
refType = re.search(r'/([^/]+)/[^/]+$', change['ref']).group(1)
refName = re.search(r'/([^/]+)$', change['ref']).group(1)
if refType == 'tags':
if not int('0x' + change['before'], 0):
msg = 'tagged [{0}]({1}/-/commit/{0}) with tag *"{2}"*\n'\
.format(change['after'],
data['project']['web_url'].replace("_", "\_"),
refName)
else:
msg = 'removed tag *"{0}"* from [{1}]({2}/-/commit/{1})\n'\
.format(refName,
change['after'],
data['project']['web_url']).replace("_", "\_")
elif refType == 'heads':
if not int('0x' + change['before'], 0):
msg = 'created branch [{0}]({1}/-/tree/{0})\n'\
.format(refName,
data['project']['web_url']).replace("_", "\_")
elif not int('0x' + change['after'], 0):
msg = 'removed branch *"{0}"*\n'.format(refName)
# ignore head changes non differentiable from normal commits
else:
continue
else:
msg = 'update with unknown ref type "{0}"\n'.format(refType)
changes.append(msg)
msg = None
if len(changes):
msg = '*{0}*\n\n'.format(data['project']['path_with_namespace'])
msg += '*{0}* {1}'\
.format(data['user_name'],
'issued multiple changes\n\n' if len(changes) > 1 else '')
msg += '\n'.join(changes)
return msg
def formatPushMsg(data):
msg = '*{0}*\n\n'.format(data['project']['path_with_namespace'])
# assume 0 commits push is a reset
if data['total_commits_count'] == 0:
msg += '*{0}* performed a reset at branch *{2}*\n'\
.format(data['user_name'],
re.search(r'/([^/]+)$', data['ref']).group(1))
else:
msg += '*{0}* pushed *{1}* new commits to branch *{2}*\n'\
.format(data['user_name'],
data['total_commits_count'],
re.search(r'/([^/]+)$', data['ref']).group(1))
for commit in data['commits']:
part = commit['message'].rstrip().partition('\n')
msg += '\n[{0}]({1})\n{2}\n'\
.format(part[0],
commit['url'].replace("_", "\_"),
part[2])
return msg
# note that if you enable tag push notifications both side wide (handled by
# formatRepoUpdateMsg) and per repo (handled here), you'll get notifications
# twice for this event until you disable one of the webhooks
def formatTagPushMsg(data):
msg = '*{0}*\n\n'.format(data['project']['path_with_namespace'])
refName = re.search(r'/([^/]+)$', data['ref']).group(1)
if not int('0x' + data['before'], 0):
msg += '*{0}* tagged [{1}]({2}) with tag *"{3}"*\n\n'\
.format(data['user_name'],
data['checkout_sha'],
data['commits'][0]['url'].replace("_", "\_"),
refName)
else:
msg += '*{0}* removed tag *"{1}"* from [{2}]({3}/-/commit/{2})\n'\
.format(data['user_name'],
refName,
data['before'],
data['project']['web_url'].replace("_", "\_"))
return msg
# TODO: can be made more informative
def formatMergeRequestMsg(data):
msg = '*{0}*\n\n'.format(data['project']['path_with_namespace'])
attrs = data['object_attributes']
action = attrs.get('action', 'open')
if attrs['source_project_id'] == attrs['target_project_id']:
source_branch = attrs['source_branch']
else:
source_branch = attrs['target']['path_with_namespace']
if action == 'open':
msg += '*{0}* requested to merge from *{1}* into *{2}*\n'\
.format(data['user']['name'],
source_branch,
attrs['target_branch'])
elif action == 'reopen':
msg += '*{0}* reopened a merge request *{1}* from *{2}* into *{3}*\n'\
.format(data['user']['name'],
attrs['id'],
source_branch,
attrs['target_branch'])
elif action == 'update':
msg += '*{0}* updated a merge request *{1}* from *{2}* into *{3}*\n'\
.format(data['user']['name'],
attrs['id'],
source_branch,
attrs['target_branch'])
if 'assignees' in data['changes']:
msg += '• Assignees were changed\n'
if 'labels' in data['changes']:
msg += '• Labels were changed\n'
if 'discussion_locked' in data['changes']:
msg += '• The discussion was locked \n'
elif action == 'close':
msg += '*{0}* closed a merge request *{1}* from *{2}* into *{3}*\n'\
.format(data['user']['name'],
attrs['id'],
source_branch,
attrs['target_branch'])
msg += '\n[{0}]({1})\n{2}\n'\
.format(attrs['title'],
attrs['url'].replace("_", "\_"),
attrs['description'])
if action != 'close':
msg += '*labels:* ' + ", ".join([label['title'] for label in
data.get('labels', ['none'])]) + '\n'
msg += '*asignees:* ' + ", ".join([asignee['name'] for asignee in
data.get('assignees', ['none'])]) + '\n'
return msg
# TODO: can be made more informative
def formatIssueMsg(data):
msg = '*{0}*\n\n'.format(data['project']['path_with_namespace'])
attrs = data['object_attributes']
action = attrs.get('action', 'open')
if action == 'open':
msg += '*{0}* opened issue *{1}*\n'.format(data['user']['name'], attrs['id'])
elif action == 'reopen':
msg += '*{0}* reopened issue *{1}*\n'.format(data['user']['name'], attrs['id'])
elif action == 'update':
msg += '*{0}* updated issue *{1}*\n'.format(data['user']['name'], attrs['id'])
if 'assignees' in data['changes']:
msg += '• Assignees were changed\n'
if 'labels' in data['changes']:
msg += '• Labels were changed\n'
if 'discussion_locked' in data['changes']:
msg += '• The discussion was locked \n'
elif action == 'close':
msg += '*{0}* closed issue *{1}*\n'\
.format(data['user']['name'],
attrs['id'])
msg += '\n[{0}]({1})\n{2}\n\n'\
.format(attrs['title'],
attrs['url'].replace("_", "\_"),
attrs['description'])
if action != 'close':
msg += '*labels:* ' + ", ".join([label['title'] for label in data.get('labels', [])]) + '\n'
msg += '*asignees:* ' + ", ".join([asignee['name'] for asignee in data.get('assignees', [])]) + '\n'
return msg
def formatNoteMsg(data):
msg = '*{0}*\n\n'.format(data['project']['path_with_namespace'])
attrs = data['object_attributes']
nType = attrs['noteable_type']
if nType == 'Commit':
msg += '{0} [commented]({1}) on commit [{2}]({3})\n\n{4}'\
.format(data['user']['name'],
attrs['url'].replace("_", "\_"),
data['commit']['id'],
data['commit']['url'].replace("_", "\_"),
attrs['note'])
elif nType == 'MergeRequest':
msg += '{0} [commented]({1}) on Merge Request [{2}]({3})\n\n{4}'\
.format(data['user']['name'],
attrs['url'].replace("_", "\_"),
data['merge_request']['id'],
data['merge_request']['url'].replace("_", "\_"),
attrs['note'])
elif nType == 'Issue':
msg += '{0} [commented]({1}) on issue [{2}]({3})\n\n{4}'\
.format(data['user']['name'],
attrs['url'].replace("_", "\_"),
data['issue']['iid'],
data['issue']['url'].replace("_", "\_"),
attrs['note'])
elif nType == 'Snippet':
msg += '{0} [commented]({1}) on code snippet [{2}]({3})\n\n{4}'\
.format(data['user']['name'],
attrs['url'].replace("_", "\_"),
data['snippet']['id'],
re.search(r'^(.*)#[^#]+$', attrs['url']).group(1).replace("_", "\_"),
attrs['note'])
return msg
def formatWikiMsg(data):
msg = '*{0}*\n\n'.format(data['project']['path_with_namespace'])
attrs = data['object_attributes']
action = attrs.get('action', 'create')
msg += '*{0}* {1}d a Wiki entry\n\n'\
.format(data['user']['name'],
action)
msg += '{0}[{1}]({2})'\
.format('(was) ' if action == 'delete' else '',
attrs['title'],
attrs['url'].replace("_", "\_"))
return msg
def formatGroupMsg(data):
action = data['event_name']
if action == 'group_create':
msg = 'Group *"{0}"* has been created'.format(data['full_path'])
elif action == 'group_rename':
msg = 'Group slug *"{0}"* has been renamed to *"{1}"*'.format(data['old_full_path'], data['full_path'])
elif action == 'group_destroy':
msg = 'Group *"{0}"* has been deleted'.format(data['full_path'])
return msg
def formatUserMsg(data):
action = data['event_name']
if action == 'user_create':
msg = 'User *{0}* has been created\n\nFull name: {1}\nEmail: {2}'\
.format(data['username'], data['name'], data['email'])
elif action == 'user_rename':
msg = 'User *{0}* has been renamed to *{1}*'.format(data['old_username'], data['username'])
elif action == 'user_destroy':
msg = 'User *{0}* has been deleted'.format(data['username'])
elif action == 'user_add_to_group':
msg = 'User *{0}* has been added to group *{1}* with {2} access'\
.format(data['user_name'],
data['group_path'],
data['group_access'])
elif action == 'user_remove_from_group':
msg = 'User *{0}* has been removed from group *{1}* - access was {2}'\
.format(data['user_name'],
data['group_path'],
data['group_access'])
elif action == 'user_update_for_group':
msg = 'User *{0}* has been updated for group *{1}* - access is {2}'\
.format(data['user_name'],
data['group_path'],
data['group_access'])
else:
msg = action
return msg
def formatKeyMsg(data):
action = data['event_name']
if action == 'key_create':
msg = '*{0}* created an SSH key with type {1}'\
.format(data['username'],
re.search(r'^ssh-([^ ]+) ', data['key']).group(1))
if action == 'key_destroy':
msg = '*{0}* removed an SSH key' .format(data['username'])
return msg
def formatProjectMsg(data):
msg = '*{0}*\n\n'.format(re.search(r'^([^/]+)/.*$', data['path_with_namespace']).group(1))
action = data['event_name']
if action in ['project_create', 'project_update']:
msg += 'Project *{0}* has been {1}d\n\npath: {2}\nvisibility: {3}\nowners: {4}'\
.format(data['name'],
re.search(r'^.*_([^_]+)$', action).group(1),
data['path_with_namespace'],
data['project_visibility'],
", ".join([owner['name'] for owner in data.get('owners', [])]))
for owner in data.get('owners', []):
msg += '{0}{1}\n'\
.format(owner['name'],
(' ' + owner['email']) if owner['email'] else '')
if action == 'project_rename':
msg += 'Project *{0}* path *{1}* has been renamed to *{2}*\n'\
.format(data['name'],
re.search(r'^.*/([^/]+)$', data['old_path_with_namespace']).group(1),
data['path'])
if action == 'project_transfer':
msg += 'Project *{0}* has been transferred from *{1}*\n\nold path: {2}\nnew path: {3}'\
.format(data['name'],
re.search(r'^([^/]+)/.*$', data['old_path_with_namespace']).group(1),
data['old_path_with_namespace'],
data['path_with_namespace'])
if action == 'project_destroy':
msg += 'Project *{0}* has been removed\n\npath was: {1}\n'\
.format(data['name'],
data['path_with_namespace'])
return msg
eventFormatters = {
'repository_update': formatRepoUpdateMsg,
'push': formatPushMsg,
'tag_push': formatTagPushMsg,
'merge_request': formatMergeRequestMsg,
'issue': formatIssueMsg,
'note': formatNoteMsg,
'wiki_page': formatWikiMsg,
'group_create': formatGroupMsg,
'group_rename': formatGroupMsg,
'group_destroy': formatGroupMsg,
'user_create': formatUserMsg,
'user_rename': formatUserMsg,
'user_destroy': formatUserMsg,
'user_add_to_group': formatUserMsg,
'user_update_for_group': formatUserMsg,
'user_remove_from_group': formatUserMsg,
'key_create': formatKeyMsg,
'key_destroy': formatKeyMsg,
'project_create': formatProjectMsg,
'project_update': formatProjectMsg,
'project_rename': formatProjectMsg,
'project_transfer': formatProjectMsg,
'project_destroy': formatProjectMsg
}