import gitlab
from Secret import admin_token
-git_host = "gitlab.services.zz" #TODO: move this to some sort of plugin config
class Marge(BotPlugin, CrontabMixin):
'0 11,17 * * * .crontab_hook' # 7:00AM and 1:00PM EST warnings
]
-# def __init__(self, bot):
-# self.gitlab = None
-
- def configure(self, configuration):
- ## TODO: set up a connection to the gitlab API
-
- self.gitlab = gitlab.Gitlab(git_host,admin_token,verify_ssl=False)
-
-# notify_re = re.compile('notify_(.*)')
-
-# def get_mr_rooms(self, project_id):
-# """
-# Return a list of errbot room '<roomname>@domain' names which have a 'notify_<roomname>' label on the project
-# Log an error if you found a 'notify_<roomname>' but margebot isn't in a <roomname> room...
-# """
-# retval = []
-# labels = this.gitlab.getlabels(project_id)
-# for a_label in labels:
-# notify_match = self.notify_re.search(a_label['name'])
-# if notify_match:
-# roomname = notify_match.group(1)
-#
-# b_room_found = False
-# marge_rooms = xmpp.rooms()
-# for a_room in marge_rooms:
-# if a_room.node() == roomname:
-# retval.append(a_room.person()) # yeah rooms are people: person = node@domain
-# b_room_found = True
-# if not b_room_found:
-# self.log.error("Label of {} found, but margebot isn't tracking that room".format(roomname))
-# else:
-# retval.append(roomname)
-# return retval
-
- @webhook('/margebot/<rooms>/'
- def gitlab_hook(self, request):
+ def __init__(self, *args, **kwargs):
+ self.git_host = None
+ self.chatroom_host = None
+ super().__init__(*args, **kwargs)
+
+ def get_configuration_template(self):
+ return {'GIT_HOST': 'gitlab.example.com',
+ 'CHATROOM_HOST': 'conference.jabber.example.com'}
+
+ def check_configuration(self, configuration):
+ super().check_configuration(configuration)
+
+ def activate(self):
+ if not self.config:
+ self.log.info('Margebot is not configured. Forbid activation')
+ return
+ self.git_host = self.config['GIT_HOST']
+ self.chatroom_host = self.config['CHATROOM_HOST']
+ self.gitlab = gitlab.Gitlab(self.git_host,admin_token,verify_ssl=False)
+
+ super().activate()
+
+ def deactivate(self):
+ # TODO: Anything special for closing gitlab ?
+ super().deactivate()
+
+# def configure(self, configuration):
+# ## TODO: set up a connection to the gitlab API
+# if self.config:
+# self.gitlab = gitlab.Gitlab(self.git_host,admin_token,verify_ssl=False)
+
+ @webhook('/margebot/<rooms>/')
+ def gitlab_hook(self, request, rooms):
"""
Webhook that listens on http://<server>:<port>/gitlab
"""
# TODO: Will errbot return a json struct or not ?
# verify it's a merge request
- if requests['object_kind'] != 'merge_request':
- self.log.error('expecting object_kind of merge_request but got {}'.format(requests['object_kind']))
+ if request['object_kind'] != 'merge_request':
+ self.log.error('expecting object_kind of merge_request but got {}'.format(request['object_kind']))
+ self.log.error('request: {}'.format(request))
elif request['object_attributes']['state'] == 'opened':
if request['object_attributes']['work_in_progress']:
wip = "WIP"
# TODO: Maybe also check the notify_<room> labels assigned to the MR as well ?
#mr_rooms = self.get_mr_rooms(target_project_id)
for a_room in rooms.split(','):
- self.send( self.build_identifier(a_room), message)
+ if self.config:
+ self.send( self.build_identifier(a_room + '@' + self.chatroom_host ), message)
with self.mutable('OPEN_MRS') as open_mrs:
open_mrs[(target_project_id,iid,rooms)] = True
return "OK"
-# def get_open_mrs(self, roomname=None, log_warnings=False):
-# mrs = []
-# for a_project in self.gitlab.getprojects():
-# for a_mr in self.gitlab.getmergerequests(a_project['id'], state='opened'):
-# rooms = self.get_mr_rooms(a_mr['target_project_id'])
-# if len(rooms) == 0 and log_warnings:
-# self.log.warning('No notify room with MRs in project: {}'.format(a_mr['target_project_id']))
-# elif not roomname:
-# mrs.append(a_mr)
-# else:
-# for a_room in rooms:
-# if a_room.startswith(roomname):
-# mrs.append(a_mr)
-# return mrs
-
def crontab_hook(self):
"""
Send a scheduled message to the rooms margebot is watching about open MRs the room cares about.
if a_mr['state'] != 'open':
continue
else:
- still_open_mrs[(project, iid, notify_rooms) = True
+ still_open_mrs[(project, iid, notify_rooms)] = True
# TODO: Warn if an open MR has has conflicts (merge_status == ??)
# TODO: Include the count of opened MR notes (does the API show resolved state ??)
# Remind each of the rooms about open MRs
for a_room, room_msg in reminder_msg.iteritems():
if room_msg != "":
- self.send(self.build_identifier(a_room), "Heads up these MRs need some luv:{}\n You can get a list of open reviews I know about by sending me a 'Marge, reviews' command.".format(room_msg))
+ if self.config:
+ self.send(self.build_identifier(a_room + '@' + self.config['CHATROOM_HOST'] ), "Heads up these MRs need some luv:{}\n You can get a list of open reviews I know about by sending me a 'Marge, reviews' command.".format(room_msg))
self['OPEN_MRS'] = still_open_mrs
-# def callback_message(self, mess):
-# """
-# Look for messages that include an URL that looks like 'https://<gitlab_server>/<group_name>/<project_name>/merge_request/<iid>'
-# Check if gitlab mergerequest webhook already exists for this group/project, and this it reports to this room ?
-# Add the project to the OPEN_MRS list
-# """
-# TODO: compiled re check against mess.body searching for
-# https://<gitlabe_server/(group_name)/(project_name)/merge_request/(iid)
-# project = self.gitlab.getprojects(group_name+'%2F'+project_name)
-# orig_mrs = self['OPEN_MRS']
-# orig_mrs[(project['id'],iid,mess.to.node()] = True
-# self.send(mess.to, "Another MR ! YUM !")
-# return
-
- @botcmd # flags a command
+ @botcmd()
def reviews(self, msg, args): # a command callable with !mrs
"""
Returns a list of MRs that are waiting for some luv.
if a_mr['state'] != 'open':
continue
else:
- still_open_mrs[(project, iid, notify_rooms) = True
+ still_open_mrs[(project, iid, notify_rooms)] = True
authored = (a_mr['author_id'] == sender_gitlab_id)
already_approved = False