2 Margebot: A Errbot Plugin for Gitlab MR reminders
4 from datetime
import datetime
, timezone
6 from dateutil
import parser
7 from dateutil
.tz
import tzutc
8 from dateutil
.relativedelta
import relativedelta
9 from errbot
import BotPlugin
, arg_botcmd
, botcmd
, re_botcmd
, webhook
10 from errbot
.templating
import tenv
11 from errcron
.bot
import CrontabMixin
16 class MargeGitlab(gitlab
.Gitlab
):
18 Subclass gitlab.Gitlab so extra_data args can be added
19 to the addprojecthook() and editprojecthook() methods
22 def __init__(self
, host
, token
="", oauth_token
="", verify_ssl
=True):
23 super().__init
__(host
, token
, oauth_token
, verify_ssl
)
25 def addprojecthook_extra(self
, project_id
, url
, push
=False, issues
=False, merge_requests
=False, tag_push
=False, extra_data
=None):
27 A copy parent addprojecthook with an extra_data field
29 data
= {"id": project_id
, "url": url
}
31 for ed_key
, ed_value
in extra_data
.items():
32 data
[ed_key
] = ed_value
33 data
['push_events'] = int(bool(push
))
34 data
['issues_events'] = int(bool(issues
))
35 data
['merge_requests_events'] = int(bool(merge_requests
))
36 data
['tag_push_events'] = int(bool(tag_push
))
37 request
= requests
.post("{0}/{1}/hooks".format(self
.projects_url
, project_id
),
38 headers
=self
.headers
, data
=data
, verify
=self
.verify_ssl
)
39 if request
.status_code
== 201:
43 def editprojecthook_extra(self
, project_id
, hook_id
, url
, push
=False, issues
=False, merge_requests
=False, tag_push
=False, extra_data
=None):
45 A copy of the parent editprojecthook with an extra_data field
47 data
= {"id": project_id
, "hook_id": hook_id
, "url": url
}
49 for ed_key
, ed_value
in extra_data
.items():
50 data
[ed_key
] = ed_value
51 data
['push_events'] = int(bool(push
))
52 data
['issues_events'] = int(bool(issues
))
53 data
['merge_requests_events'] = int(bool(merge_requests
))
54 data
['tag_push_events'] = int(bool(tag_push
))
55 request
= requests
.put("{0}/{1}/hooks/{2}".format(self
.projects_url
, project_id
, hook_id
),
56 headers
=self
.headers
, data
=data
, verify
=self
.verify_ssl
)
57 return request
.status_code
== 200
60 def deltastr(any_delta
):
62 Output a datetime delta in the format "x days, y hours, z minutes ago"
66 hours
= any_delta
.seconds
// 3600
67 mins
= (any_delta
.seconds
// 60) % 60
69 for (key
, val
) in [("day", days
), ("hour", hours
), ("minute", mins
)]:
71 l_delta
.append("1 " + key
)
73 l_delta
.append("{} {}s".format(val
, key
))
78 retval
= ", ".join(l_delta
) + " ago"
82 class Marge(BotPlugin
, CrontabMixin
):
84 I remind you about merge requests
88 Add a merge request webook of the form
89 'https://webookserver/margeboot/<rooms>'
90 to the projects you want tracked. <rooms> should be a
91 comma-separated list of short room names (anything before the '@')
92 that you want notified.
94 Add <roomname>@domain to the CHATROOM_PRESENCE list in config.py for
95 rooms margebot should join
99 # Set in config now: '0 11,17 * * * .crontab_hook' # 7:00AM and 1:00PM EST warnings
102 def __init__(self
, *args
, **kwargs
):
104 self
.chatroom_host
= None
106 self
.soak_delta
= None
107 self
.webhook_url
= None
108 super().__init
__(*args
, **kwargs
)
110 def get_configuration_template(self
):
112 GITLAB_HOST: Host name of your gitlab server
113 GITLAB_ADMIN_TOKEN: PAT from an admin's https://${GIT_HOST}/profile/personal_access_tokens.
114 CHATROOM_HOST: Chatroom host. Usually 'chatroom' + FQDN of Jabber server
115 CRONTAB: Schedule of automated merge request checks in '%M %H %d %m %w' format
116 VERIFY_SSL : True, False, or path to CA cert to verify cert
117 CRONTAB_SOAK_HOURS : Don't send out reminders about MRs opened less than this many hours
118 WEBHOOK_URL : URL to use for defining MR integration in gitlab
120 return {'GITLAB_HOST': 'gitlab.example.com',
121 'GITLAB_ADMIN_TOKEN': 'gitlab-admin-user-private-token',
122 'CHATROOM_HOST': 'conference.jabber.example.com',
123 'CRONTAB': '0 11,17 * * *',
125 'CRONTAB_SOAK_HOURS': 1,
126 'WEBHOOK_URL': 'https://webhooks.example.com:3142/margebot/'}
128 def check_configuration(self
, configuration
):
130 Check that the plugin has been configured properly
132 super().check_configuration(configuration
)
136 Initialization done when the plugin is activated
139 self
.log
.info('Margebot is not configured. Forbid activation')
141 self
.git_host
= self
.config
['GITLAB_HOST']
142 self
.chatroom_host
= self
.config
['CHATROOM_HOST']
143 Marge
.CRONTAB
= ['{} .crontab_hook'.format(self
.config
['CRONTAB'])]
144 gitlab_auth_token
= self
.config
['GITLAB_ADMIN_TOKEN']
145 verify_ssl
= self
.config
['VERIFY_SSL']
146 # self.gitlab = gitlab.Gitlab(self.git_host, gitlab_auth_token, verify_ssl=verify_ssl)
147 self
.gitlab
= MargeGitlab(self
.git_host
, gitlab_auth_token
, verify_ssl
=verify_ssl
)
148 self
.activate_crontab()
150 self
.soak_delta
= relativedelta(hours
=self
.config
['CRONTAB_SOAK_HOURS'])
151 self
.webhook_url
= self
.config
['WEBHOOK_URL']
152 if self
.webhook_url
[-1] != '/':
153 self
.webhook_url
+= '/'
156 def deactivate(self
):
158 Anything that needs to be tore down when the plugin is deactivated goes here.
162 @webhook('/margebot/<rooms>/')
163 def gitlab_hook(self
, request
, rooms
):
165 Webhook that listens on http://<server>:<port>/gitlab
168 self
.log
.info("webhook: request: {}, rooms: {}".format(request
, rooms
))
169 self
.log
.info("state: {}".format(request
['object_attributes']['state']))
171 # verify it's a merge request
172 if request
['object_kind'] != 'merge_request':
173 self
.log
.error('unexpecting object_kind: {}'.format(request
['object_kind']))
174 elif 'opened' in request
['object_attributes']['state']:
176 if request
['object_attributes']['work_in_progress']:
180 url
= request
['project']['homepage']
181 title
= request
['object_attributes']['title']
183 author_id
= request
['object_attributes']['author_id'] # map this to user name ...
184 author
= self
.gitlab
.getuser(author_id
)
186 author_name
= author
['username']
188 self
.log
.info("unexpected author_id {}".format(author_id
))
189 author_name
= author_id
191 target_project_id
= request
['object_attributes']['target_project_id']
192 iid
= request
['object_attributes']['iid']
193 mr_id
= request
['object_attributes']['id']
195 msg_template
= "Hi there ! {} has opened a new {}MR: \"{}\"\n{}/merge_requests/{}"
196 msg
= msg_template
.format(author_name
, wip
, title
, url
, iid
)
198 if 'OPEN_MRS' not in self
.keys():
200 self
['OPEN_MRS'] = empty_dict
202 open_mrs
= self
['OPEN_MRS']
204 if (target_project_id
, mr_id
, rooms
) not in open_mrs
:
205 for a_room
in rooms
.split(','):
207 self
.send(self
.build_identifier(a_room
+ '@' + self
.chatroom_host
), msg
)
209 self
.log
.info("webhook: Saving ({}, {}, {})".format(target_project_id
, mr_id
, rooms
))
210 open_mrs
[(target_project_id
, mr_id
, rooms
)] = True
211 self
['OPEN_MRS'] = open_mrs
214 def mr_status_msg(self
, a_mr
, author
=None):
216 Create the merge request status message
218 self
.log
.info("mr_status_msg: a_mr: {}".format(a_mr
))
220 # Only weed out MRs less than the soak time for the crontab output (where author==None)
221 now
= datetime
.now(timezone
.utc
)
222 creation_time
= parser
.parse(a_mr
['created_at'], tzinfos
=tzutc
)
224 self
.log
.info("times: {}, {}, {}".format(creation_time
, self
.soak_delta
, now
))
225 if creation_time
+ self
.soak_delta
> now
:
226 project_id
= a_mr
['target_project_id']
228 soak_hours
= self
.config
['CRONTAB_SOAK_HOURS']
229 info_template
= "skipping: MR <{},{}> was opened less than {} hours ago"
230 info_msg
= info_template
.format(project_id
, mr_id
, soak_hours
)
231 self
.log
.info(info_msg
)
234 str_open_since
= deltastr(now
- creation_time
)
237 if a_mr
['work_in_progress']:
238 warning
= "still WIP"
239 elif a_mr
['merge_status'] != 'can_be_merged':
240 warning
= "there are merge conflicts"
243 authored
= (a_mr
['author']['id'] == author
)
247 # getapprovals is only available in GitLab 8.9 EE or greater
248 # (not the open source CE version)
249 # approvals = self.gitlab.getapprovals(a_mr['id'])
251 # for approved in approvals['approved_by']:
252 # also_approved += "," + approved['user']['name']
254 upvotes
= a_mr
['upvotes']
255 msg
= "{} (opened {})".format(a_mr
['web_url'], str_open_since
)
257 msg
+= ": Has 2+ upvotes and could be merged in now"
259 msg
+= " except {}.".format(warning
)
265 msg
+= ": Your MR is waiting for another upvote"
267 msg
+= ": Waiting for another upvote"
269 msg
+= " but {}.".format(warning
)
275 msg
+= ": Your MR has no upvotes"
277 msg
+= ": No upvotes, please review"
279 msg
+= " but {}.".format(warning
)
283 return {'creation_time': creation_time
, 'msg': msg
}
285 def crontab_hook(self
, polled_time
):
287 Send a scheduled message to the rooms margebot is watching
288 about open MRs the room cares about.
291 self
.log
.info("crontab_hook triggered at {}".format(polled_time
))
293 reminder_msg
= {} # Map of reminder_msg['roomname@domain'] = [(created_at,msg)]
295 # initialize the reminders
298 reminder_msg
[a_room
.node
] = []
302 # Let's walk through the MRs we've seen already:
303 open_mrs
= self
['OPEN_MRS']
305 for (project_id
, mr_id
, notify_rooms
) in open_mrs
:
307 # Lookup the MR from the project/id
308 a_mr
= self
.gitlab
.getmergerequest(project_id
, mr_id
)
310 self
.log
.debug("Couldn't find project: {}, id: {}".format(project_id
, mr_id
))
313 # If the MR is tagged 'never-close' ignore it
314 if 'labels' in a_mr
and 'never-close' in a_mr
['labels']:
317 self
.log
.info("a_mr: {} {} {} {}".format(project_id
, mr_id
, notify_rooms
, a_mr
['state']))
319 # If the MR is no longer open, skip to the next MR,
320 # and don't include this MR in the next check
321 if 'opened' not in a_mr
['state']:
324 still_open_mrs
[(project_id
, mr_id
, notify_rooms
)] = True
326 msg_dict
= self
.mr_status_msg(a_mr
)
330 for a_room
in notify_rooms
.split(','):
331 reminder_msg
[a_room
].append(msg_dict
)
333 # Remind each of the rooms about open MRs
334 for a_room
, room_msg_list
in reminder_msg
.items():
335 if room_msg_list
!= []:
337 to_room
= a_room
+ '@' + self
.config
['CHATROOM_HOST']
338 msg
= tenv().get_template('reviews.md').render(msg_list
=room_msg_list
)
339 self
.send(self
.build_identifier(to_room
), msg
)
341 self
['OPEN_MRS'] = still_open_mrs
343 @botcmd(template
="reviews")
344 def reviews(self
, msg
, args
):
346 Returns a list of MRs that are waiting for some luv.
347 Also returns a list of MRs that have had enough luv but aren't merged in yet.
349 # Sending directly to Margbot: sender in the form sender@....
350 # Sending to a chatroom: snder in the form room@rooms/sender
352 log_template
= 'reviews: msg: {}, args: {}, \nmsg.frm: {}, \nmsg.msg.frm: {}, chatroom_host: {}'
353 self
.log
.info(log_template
.format(msg
, args
, msg
.frm
.__dict
__, dir(msg
.frm
), self
.config
['CHATROOM_HOST']))
354 self
.log
.info('reviews: bot mode: {}'.format(self
._bot
.mode
))
356 if self
._bot
.mode
== "xmpp":
357 if msg
.frm
.domain
== self
.config
['CHATROOM_HOST']:
358 sender
= msg
.frm
.resource
360 sender
= msg
.frm
.node
362 sender
= str(msg
.frm
).split('@')[0]
365 if 'OPEN_MRS' not in keys
:
366 self
.log
.error('OPEN_MRS not in {}'.format(keys
))
367 return "No MRs to review"
369 sender_gitlab_id
= None
370 sender_users
= self
.gitlab
.getusers(search
=(('username', sender
)))
372 self
.log
.error('problem mapping {} to a gitlab user'.format(sender
))
373 sender_gitlab_id
= None
375 sender_gitlab_id
= sender_users
[0]['id']
377 # Walk through the MRs we've seen already:
381 open_mrs
= self
['OPEN_MRS']
382 for (project
, mr_id
, notify_rooms
) in open_mrs
:
384 # Lookup the MR from the project/id
385 a_mr
= self
.gitlab
.getmergerequest(project
, mr_id
)
387 self
.log
.debug("Couldn't find project: {}, id: {}".format(project
, id))
390 self
.log
.info('project: {}, id: {}, a_mr: {}'.format(project
, id, a_mr
))
392 # If the MR is tagged 'never-close' ignore it
393 if 'labels' in a_mr
and 'never-close' in a_mr
['labels']:
396 # If the MR is no longer open, skip to the next MR,
397 # and don't include this MR in the next check
398 if 'opened' not in a_mr
['state']:
401 still_open_mrs
[(project
, mr_id
, notify_rooms
)] = True
403 msg_dict
= self
.mr_status_msg(a_mr
, author
=sender_gitlab_id
)
407 msg_list
.append(msg_dict
)
409 self
['OPEN_MRS'] = still_open_mrs
411 return {'sender': sender
, 'msg_list': msg_list
}
413 # -----------------------------------------------------------
414 # webhook maintenance commands
416 @arg_botcmd('rooms', type=str)
417 @arg_botcmd('repo', type=str)
418 def watchrepo(self
, msg
, repo
, rooms
):
420 Add the margebot webhook to a repo, and prepopulate any open MRs in the repo with margebot
421 args: repo: gitlab repo name in the 'NAMESPACE/PROJECT_NAME' format
422 rooms: comma separates list of rooms to notify when the webhook triggers
424 self
.log
.info("msg={}".format(msg
))
425 self
.log
.info("repo={}".format(repo
))
426 self
.log
.info("rooms={}".format(rooms
))
428 # get the group/repo repo, error out if it doesn't exist
429 project
= self
.gitlab
.getproject(repo
)
431 msg
= "Couldn't find repo {}".format(repo
)
432 self
.log
.info("watchrepo: {}".format(msg
))
435 self
.log
.info('project: {}'.format(project
))
437 target_project_id
= project
['id']
439 # Check is the project already includes the margebot hook
440 # If no hooks, will it return False or [] ?
442 hooks
= self
.gitlab
.getprojecthooks(target_project_id
)
444 msg
= "Couldn't find {} hooks".format(repo
)
445 self
.log
.error("watchrepo: {}".format(msg
))
449 self
.log
.info('a_hook: {}'.format(a_hook
))
450 if a_hook
['merge_requests_events'] and a_hook
['url'].startswith(self
.webhook_url
):
454 # If so replace it (or error out ?)
455 url
= "{}{}".format(self
.webhook_url
, rooms
) # webhooks_url will end in '/'
458 old_rooms
= marge_hook
['url'].split(self
.webhook_url
, 1)[1]
459 if old_rooms
== rooms
:
460 msg
= "Already reporting {} MRs to the {} room(s)".format(repo
, rooms
)
461 self
.log
.info('watchrepo: {}'.format(msg
))
464 hook_updated
= self
.gitlab
.editprojecthook_extra(target_project_id
, marge_hook
['id'], url
, merge_requests
=True, extra_data
={'enable_ssl_verification': False})
465 s_watch_msg
= "Updating room list for {} MRs from {} to {}".format(repo
, old_rooms
, rooms
)
468 hook_updated
= self
.gitlab
.addprojecthook_extra(target_project_id
, url
, merge_requests
=True, extra_data
={'enable_ssl_verification': False})
469 s_watch_msg
= "Now watching for new MRs in the {} repo to the {} roomi(s)".format(repo
, rooms
)
473 msg
= "Couldn't {} hook: {}".format(s_action
, repo
)
474 self
.log
.error("watchrepo: {}".format(msg
))
477 open_mrs
= self
['OPEN_MRS']
479 # get the open MRs in the repo
481 # If updating the room list, walk through the existing MR list
482 if s_action
== "update":
484 for (project_id
, mr_id
, old_rooms
) in open_mrs
:
485 # pragma pylint: disable=simplifiable-if-statement
486 if project_id
== target_project_id
:
487 new_open_mrs
[(project_id
, mr_id
, rooms
)] = True
489 new_open_mrs
[(project_id
, mr_id
, old_rooms
)] = True
490 # pragma pylint: enable=simplifiable-if-statement
491 open_mrs
= new_open_mrs
493 # If adding a new repo, check for existing opened/reopened MRs in the repo.
495 for state
in ['opened', 'reopened']:
497 mr_list
= self
.gitlab
.getmergerequests(target_project_id
, page
=page
, per_page
=100, state
=state
)
498 while not mr_list
and mr_list
!= []:
499 for an_mr
in mr_list
:
501 self
.log
.info('watchrepo: an_mr WATS THE ID\n{}'.format(an_mr
))
503 open_mrs
[(target_project_id
, mr_id
, rooms
)] = True
504 # Get the next page of MRs
506 mr_list
= self
.gitlab
.getmergerequests(target_project_id
, page
=page
, per_page
=100)
508 self
['OPEN_MRS'] = open_mrs
511 mr_msg
= "No open MRs were found in the repo."
513 mr_msg
= "1 open MR was found in the repo. Run !reviews to see the updated MR list."
515 mr_msg
= "{} open MRs were found in the repo. Run !reviews to see the updated MR list."
517 return "{}\n{}".format(s_watch_msg
, mr_msg
)
519 # pragma pylint: disable=unused-argument
521 def hello(self
, msg
, args
):
523 A simple command to check if the bot is responding
528 def xyzzy(self
, msg
, args
):
530 Don't call this command...
532 yield "/me whispers \"All open MRs have been merged into master.\""
534 yield "(just kidding)"
536 @re_botcmd(pattern
=r
"I blame [Mm]arge([Bb]ot)?")
537 def dont_blame_margebot(self
, msg
, match
):
539 margebot is innocent.
541 yield "(」゚ロ゚)」NOOOooooo say it ain't so."
543 @re_botcmd(pattern
=r
"good bot")
544 def best_bot(self
, msg
, match
):
546 margebot is the best.
550 # pragma pylint: enable=unused-argument