Skip to content

Commit 3214887

Browse files
committed
[REF] runbot: Get repo data from json request of github webhook
1 parent a368702 commit 3214887

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

runbot/runbot.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import operator
1111
import os
1212
import psycopg2
13+
import pprint
1314
import re
1415
import resource
1516
import shutil
@@ -1208,7 +1209,7 @@ def repo(self, repo=None, search='', limit='100', refresh='', **post):
12081209
repo_ids = repo_obj.search(cr, uid, [])
12091210
repos = repo_obj.browse(cr, uid, repo_ids)
12101211
if not repo and repos:
1211-
repo = repos[0]
1212+
repo = repos[0]
12121213

12131214
context = {
12141215
'repos': repos,
@@ -1301,11 +1302,25 @@ def branch_info(branch):
13011302

13021303
return request.render("runbot.repo", context)
13031304

1304-
@http.route(['/runbot/hook/<int:repo_id>'], type='http', auth="public", website=True)
1305+
@http.route(['/runbot/hook/<int:repo_id>', '/runbot/hook/org'], type='json', auth="public", website=True)
13051306
def hook(self, repo_id=None, **post):
1306-
# TODO if repo_id == None parse the json['repository']['ssh_url'] and find the right repo
1307-
repo = request.registry['runbot.repo'].browse(request.cr, SUPERUSER_ID, [repo_id])
1308-
repo.hook_time = datetime.datetime.now().strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT)
1307+
if repo_id is None:
1308+
repo_data = request.jsonrequest.get('repository')
1309+
event = request.httprequest.headers.get("X-Github-Event")
1310+
if repo_data and event in ['push', 'pull_request']:
1311+
repo_domain = [
1312+
'|', ('name', '=', repo_data['ssh_url']),
1313+
('name', '=', repo_data['clone_url']),
1314+
]
1315+
repo = request.registry['runbot.repo'].search(
1316+
request.cr, SUPERUSER_ID, repo_domain, limit=1)
1317+
repo_id = repo[0] if repo else None
1318+
1319+
if repo_id:
1320+
repo = request.registry['runbot.repo'].browse(request.cr, SUPERUSER_ID, [repo_id])
1321+
repo.hook_time = datetime.datetime.now().strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT)
1322+
else:
1323+
_logger.debug('Repo not found from request data: %s', pprint.pformat(request.jsonrequest)[:450])
13091324
return ""
13101325

13111326
@http.route(['/runbot/dashboard'], type='http', auth="public", website=True)

0 commit comments

Comments
 (0)