[REF] runbot: hook - Find the right repo from payload

This change allow use a global webhook from github based on the organization without use a configuration for each repository.

closes: #5
This commit is contained in:
Moises Lopez - https://www.vauxoo.com/ 2018-04-19 02:59:27 -05:00 committed by Christophe Monniez
parent 4a558741e0
commit fa6a0944f4

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import datetime
import json
from odoo import http, tools
from odoo.http import request
@ -8,9 +9,21 @@ from odoo.http import request
class RunbotHook(http.Controller):
@http.route(['/runbot/hook/<int:repo_id>'], type='http', auth="public", website=True, csrf=False)
@http.route(['/runbot/hook/<int:repo_id>', '/runbot/hook/org'], type='http', auth="public", website=True, csrf=False)
def hook(self, repo_id=None, **post):
# TODO if repo_id == None parse the json['repository']['ssh_url'] and find the right repo
if repo_id is None:
event = request.httprequest.headers.get("X-Github-Event")
repo_data = json.loads(request.params['payload']).get('repository')
if repo_data and event in ['push', 'pull_request']:
repo_domain = [
'|', '|', ('name', '=', repo_data['ssh_url']),
('name', '=', repo_data['clone_url']),
('name', '=', repo_data['clone_url'].rstrip('.git')),
]
repo = request.env['runbot.repo'].sudo().search(
repo_domain, limit=1)
repo_id = repo.id
repo = request.env['runbot.repo'].sudo().browse([repo_id])
repo.hook_time = datetime.datetime.now().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
return ""