Commit a8610ab3 authored by A. Wilcox's avatar A. Wilcox 🦊
Browse files

Emily now works for push events from GitLab :D

parent d109b936
......@@ -2,6 +2,11 @@
import asyncio
from collections import defaultdict
import logging
from taillight.signal import Signal
logging.basicConfig(level=logging.DEBUG)
in_plugins = []
......@@ -13,6 +18,22 @@ out_plugins = []
event_loop = asyncio.get_event_loop()
push_event = Signal(('source', 'push'))
"""A push occurred.
Caller is project name (group if available, otherwise 'default').
Push details are:
* User {name, email}
* Commits [{id,msg,url,author:{name,email}}]
* Branch
"""
issue_open_event = Signal(('issue', 'open'))
issue_comment_event = Signal(('issue', 'comment'))
issue_close_event = Signal(('issue', 'close'))
def register_in(plugin):
inst = plugin()
......
import asyncio
from emily.core import push_event
from emily.protocols.http import http_handle, HTTPProtocol
import json
class GitLab:
""" Processes information from GitLab Web Hooks. """
......@@ -8,8 +10,29 @@ class GitLab:
def __init__(self):
http_handle.add(self.http_handle)
@asyncio.coroutine
def http_handle(self, request):
if 'X-Gitlab-Event' not in request.headers:
return False
json = yield from request.json()
if 'object_kind' not in json or json['object_kind'] != 'push':
return False # why?!
push_info = {}
push_info['user'] = {'name': json['user_name'],
'email': json['user_email']}
push_info['branch'] = json['ref'].split('/')[-1]
push_info['commits'] = []
for commit in json['commits']:
push_info['commits'].append({'id': commit['id'],
'url': commit['url'],
'msg': commit['message'],
'author': commit['author']})
project = request.match_info.get('grp', 'default') + '/' + \
request.match_info.get('project')
yield from push_event.call_async(project, push_info)
return True
......@@ -5,6 +5,7 @@ from PyIRC.io.asyncio import IRCProtocol
from PyIRC.signal import event
from emily import config
from emily.core import push_event
class IRCNetwork(IRCProtocol):
"""The basic IRC network representation. Each network has an instance."""
......@@ -33,6 +34,8 @@ class IRC:
network_channel_projects = comma separated list of projects.
"""
def __init__(self, loop):
push_event.add(self.push)
myconf = config['irc']
self.networks = [network.strip()
for network in myconf['networks'].split(',')]
......@@ -62,7 +65,7 @@ class IRC:
args['join'].append('#' + channel)
proj_setting = network + '_' + channel + '_projects'
for project in [proj.strip()
for proj in myconf[proj_setting]]:
for proj in myconf[proj_setting].split(',')]:
self.projects[project].append('{}:{}'.format(network,
'#' + channel))
......@@ -73,4 +76,34 @@ class IRC:
return [inst.connect() for inst in self.network_instances.values()]
def push(self, project, push):
pass
\ No newline at end of file
channels = set(self.projects[project] +
self.projects[project.split('/', 1)[0] + '/*'] +
self.projects['*/*'])
commit_msgs = []
push_msg = '\x02{}\x02: {} pushed '.format(project, push['user']['name'])
count = len(push['commits'])
if count > 1:
push_msg += '{} commits '.format(count)
else:
push_msg += 'a commit '
push_msg += 'to the \x02{}\x02 branch:'.format(push['branch'])
for commit in push['commits']:
msg = '{} ({}) by \x02{}\x02: {}'.format(commit['id'][:7],
commit['url'],
commit['author']['name'],
commit['msg'])
commit_msgs.append(msg)
if len(commit_msgs) > 2:
msg += ' [... and {} more]'.format(count - 3)
commit_msgs[-1] = msg
break
for channel in channels:
network, irc_chan = channel.split(':', 1)
self.network_instances[network].send('PRIVMSG', [irc_chan,
push_msg])
for msg in commit_msgs:
self.network_instances[network].send('PRIVMSG', [irc_chan, msg])
......@@ -23,7 +23,7 @@ class HTTPProtocol():
self.app.router.add_route('POST', '/{grp}/{project}', self.handler)
self.server = loop.create_server(self.app.make_handler(),
'0.0.0.0', int(config['http']['port']))
'::', int(config['http']['port']))
def coros(self):
return [self.server]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment