From: JudeN Date: Sun, 12 Nov 2017 15:57:50 +0000 (-0500) Subject: Checkpoint / crontab_hook working now X-Git-Url: https://pwan.org/git/?p=margebot.git;a=commitdiff_plain;h=90ad86381e4db14a08b0a749d954f046abd41226 Checkpoint / crontab_hook working now --- diff --git a/Secret.py b/Secret.py deleted file mode 100755 index 18d15e5..0000000 --- a/Secret.py +++ /dev/null @@ -1,9 +0,0 @@ -# go away - -# https://docs.gitlab.com/ee/api/README.html#personal-access-tokens - -# token for a normal, non-admin user -my_token="get-this-from-gitlab" - -# token for actions that require admin access -admin_token="get-this-from-gitlab" diff --git a/marge.py b/marge.py index 87743ef..6f9f87f 100755 --- a/marge.py +++ b/marge.py @@ -5,8 +5,6 @@ from errcron.bot import CrontabMixin from time import sleep import gitlab -from Secret import admin_token - # TODO: Add certificate verification to the gitlab API calls. @@ -26,9 +24,8 @@ class Marge(BotPlugin, CrontabMixin): rooms margebot should join """ - # TODO: The rontab schedule should be part of the configuration CRONTAB = [ - '0 11,17 * * * .crontab_hook' # 7:00AM and 1:00PM EST warnings + # Set in config now: '0 11,17 * * * .crontab_hook' # 7:00AM and 1:00PM EST warnings ] def __init__(self, *args, **kwargs): @@ -37,8 +34,18 @@ class Marge(BotPlugin, CrontabMixin): super().__init__(*args, **kwargs) def get_configuration_template(self): - return {'GIT_HOST': 'gitlab.example.com', - 'CHATROOM_HOST': 'conference.jabber.example.com'} + """ + GITLAB_HOST: Host name of your gitlab server + GITLAB_ADMIN_TOKEN: PAT from an admin's https://${GIT_HOST}/profile/personal_access_tokens page. + CHATROOM_HOST: Chatroom host. Usually 'chatroom' + FQDN of Jabber server + CRONTAB: Schedule of automated merge request checks in '%M %H %d %m %w' format + VERIFY_SSL : True, False, or path to CA cert to verify cert + """ + return {'GITLAB_HOST': 'gitlab.example.com', + 'GITLAB_ADMIN_TOKEN' : 'gitlab-admin-user-private-token', + 'CHATROOM_HOST': 'conference.jabber.example.com', + 'CRONTAB' : '0 11,17 * * *', + 'VERIFY_SSL' : True} def check_configuration(self, configuration): super().check_configuration(configuration) @@ -47,9 +54,11 @@ class Marge(BotPlugin, CrontabMixin): if not self.config: self.log.info('Margebot is not configured. Forbid activation') return - self.git_host = self.config['GIT_HOST'] + self.git_host = self.config['GITLAB_HOST'] self.chatroom_host = self.config['CHATROOM_HOST'] - self.gitlab = gitlab.Gitlab(self.git_host, admin_token, verify_ssl=False) + Marge.CRONTAB = ['{} .crontab_hook'.format(self.config['CRONTAB']) ] + self.gitlab = gitlab.Gitlab(self.git_host, self.config['GITLAB_ADMIN_TOKEN'], verify_ssl=self.config['VERIFY_SSL']) + self.activate_crontab() super().activate() @@ -112,18 +121,20 @@ class Marge(BotPlugin, CrontabMixin): return "OK" - def crontab_hook(self): + def crontab_hook(self, polled_time): """ Send a scheduled message to the rooms margebot is watching about open MRs the room cares about. """ + self.log.info("crontab_hook triggered at {}".format(polled_time)) + reminder_msg = {} # Map of reminder_msg['roomname@domain'] = msg # initialize the reminders - rooms = xmpp.rooms() + rooms = self.rooms() for a_room in rooms: - reminder_msg[a_room.node()] = '' + reminder_msg[a_room.node] = '' msg = "" still_open_mrs = {} @@ -135,9 +146,11 @@ class Marge(BotPlugin, CrontabMixin): # Lookup the MR from the project/iid a_mr = self.gitlab.getmergerequest(project, iid) + self.log.info("a_mr: {} {} {} {}".format(project, iid, notify_rooms, a_mr['state'])) + # If the MR is no longer open, skip to the next MR, # and don't include this MR in the next check - if a_mr['state'] != 'open': + if a_mr['state'] != 'opened': continue else: still_open_mrs[(project, iid, notify_rooms)] = True @@ -164,7 +177,7 @@ class Marge(BotPlugin, CrontabMixin): reminder_msg[a_room] += msg # Remind each of the rooms about open MRs - for a_room, room_msg in reminder_msg.iteritems(): + for a_room, room_msg in reminder_msg.items(): if room_msg != "": if self.config: msg_template = "Heads up these MRs need some attention:{}\n" @@ -180,31 +193,28 @@ class Marge(BotPlugin, CrontabMixin): Returns a list of MRs that are waiting for some luv. Also returns a list of MRs that have had enough luv but aren't merged in yet. """ - sender = msg._from._resource - self.log.info('juden: reviews: frm: {} nick: {}\n{}'.format(msg.frm, msg.nick, msg._from.__dict__)) + ## Sending directly to Margbot: sender in the form sender@.... + ## Sending to a chatroom: snder in the form room@rooms/sender + + if msg.frm.domain == self.config['CHATROOM_HOST']: + sender = msg.frm.resource + else: + sender = msg.frm.node if 'OPEN_MRS' not in self.keys(): return "No MRs to review" sender_gitlab_id = None for user in self.gitlab.getusers(): - # self.log.info('juden: users: {} {}'.format(sender, user)) if user['username'] == sender: sender_gitlab_id = user['id'] - self.log.info('juden: sender_gitlab_id = {}'.format(sender_gitlab_id)) break if not sender_gitlab_id: self.log.error('problem mapping {} to a gitlab user'.format(sender)) - # self.send(self.build_identifier(msg.frm), "Sorry I couldn't find your gitlab ID") return "Sorry, I couldn't find your gitlab account." - # TODO: how to get the room the message was sent from ? I'm assuming this will either be in msg.frm or msg.to - # TODO: weed out MRs the sender opened or otherwise indicate they've opened or have already +1'd - - roomname = msg.to.domain # ??? - - # Let's walk through the MRs we've seen already: + # Walk through the MRs we've seen already: msg = "" still_open_mrs = {} with self.mutable('OPEN_MRS') as open_mrs: @@ -213,8 +223,6 @@ class Marge(BotPlugin, CrontabMixin): # Lookup the MR from the project/iid a_mr = self.gitlab.getmergerequest(project, iid) - self.log.info('juden: a_mr: {} {} {} {}'.format(project, iid, notify_rooms, a_mr)) - # If the MR is no longer open, skip to the next MR, # and don't include this MR in the next check if a_mr['state'] != 'opened':