From 99ba9e2fe62b78fc0da22d3272ba27eea24b727c Mon Sep 17 00:00:00 2001 From: Moises Lopez Date: Tue, 13 Oct 2015 05:07:58 +0000 Subject: [PATCH] [REF] runbot: Get repo data from json request of github webhook --- runbot/runbot.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/runbot/runbot.py b/runbot/runbot.py index 9e1a2cce..6bff170a 100644 --- a/runbot/runbot.py +++ b/runbot/runbot.py @@ -8,6 +8,7 @@ import logging import operator import os +import pprint import re import resource import shutil @@ -1243,11 +1244,27 @@ def branch_info(branch): return request.render("runbot.repo", context) - @http.route(['/runbot/hook/'], type='http', auth="public", website=True) + @http.route(['/runbot/hook/', '/runbot/hook/org'], type='json', auth="public", website=True) def hook(self, repo_id=None, **post): - # TODO if repo_id == None parse the json['repository']['ssh_url'] and find the right repo - repo = request.registry['runbot.repo'].browse(request.cr, SUPERUSER_ID, [repo_id]) - repo.hook_time = datetime.datetime.now().strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT) + if repo_id is None: + repo_data = request.jsonrequest.get('repository') + event = request.httprequest.headers.get("X-Github-Event") + if repo_data and event in ['push', 'pull_request']: + repo_owner = repo_data['owner'].get('name') or repo_data['owner'].get('login') + repo_name = repo_data['name'] + repo_domain = [ + '|', ('name', '=', 'git@github.com:%s/%s.git' % (repo_owner, repo_name)), + ('name', '=', 'https://github.com/%s/%s.git' % (repo_owner, repo_name)), + ] + repo = request.registry['runbot.repo'].search( + request.cr, SUPERUSER_ID, repo_domain, limit=1) + repo_id = repo[0] if len(repo) else None + + if repo_id: + repo = request.registry['runbot.repo'].browse(request.cr, SUPERUSER_ID, [repo_id]) + repo.hook_time = datetime.datetime.now().strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT) + else: + _logger.debug('Repo not found from request data: %s', pprint.pformat(request.jsonrequest)[:450]) return "" @http.route(['/runbot/dashboard'], type='http', auth="public", website=True)