[IMP] runbot: handle PR re-targeting

When a PR branch target is changed on Github, the change is not applied
in the runbot DB.

With this commit, the Github hook payload is taken into account to
detect such a change and the branch infos are recomputed accordingly.

Also, a button is now available on the branch form in order to manually
recompute those changes.
This commit is contained in:
Christophe Monniez 2020-02-11 15:00:20 +01:00 committed by XavierDo
parent 7bd136b9d2
commit 464893d248
3 changed files with 12 additions and 2 deletions

View File

@ -32,6 +32,11 @@ class RunbotHook(http.Controller):
# force update of dependencies to in case a hook is lost
if not payload or event == 'push' or (event == 'pull_request' and payload.get('action') in ('synchronize', 'opened', 'reopened')):
(repo | repo.dependency_ids).set_hook_time(time.time())
elif event == 'pull_request' and payload and payload.get('action', '') == 'edited' and 'base' in payload.get('changes'):
# handle PR that have been re-targeted
pr_number = payload.get('pull_request', {}).get('number', '')
branch = request.env['runbot.branch'].sudo().search([('repo_id', '=', repo.id), ('name', '=', 'refs/pull/%s' % pr_number)])
branch._get_branch_infos(payload.get('pull_request', {}))
else:
_logger.debug('Ignoring unsupported hook %s %s', event, payload.get('action', ''))
return ""

View File

@ -102,12 +102,12 @@ class runbot_branch(models.Model):
branch.branch_config_id = branch.config_id
@api.depends('name')
def _get_branch_infos(self):
def _get_branch_infos(self, pull_info=None):
"""compute branch_name, branch_url, pull_head_name and target_branch_name based on name"""
for branch in self:
if branch.name:
branch.branch_name = branch.name.split('/')[-1]
pi = branch._get_pull_info()
pi = pull_info or branch._get_pull_info()
if pi:
branch.target_branch_name = pi['base']['ref']
if not _re_patch.match(pi['head']['label']):
@ -116,6 +116,10 @@ class runbot_branch(models.Model):
else:
branch.branch_name = ''
def recompute_infos(self):
""" public method to recompute infos on demand """
self._get_branch_infos()
@api.depends('branch_name')
def _get_branch_url(self):
"""compute the branch url based on branch_name"""

View File

@ -6,6 +6,7 @@
<field name="arch" type="xml">
<form>
<header>
<button name="recompute_infos" string="Recompute Infos" type="object" class="oe_highlight"/>
</header>
<sheet>
<div class="oe_button_box" name="button_box">