from time import sleep
import gitlab
-from Secret import admin_token
-
# TODO: Add certificate verification to the gitlab API calls.
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):
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)
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()
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 = {}
# 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
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"
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:
# 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':