Skip to content

Commit b789a94

Browse files
committed
[FIX] runbot: branch computation
1 parent 2fa5a6a commit b789a94

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

runbot/models/branch.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Branch(models.Model):
3030
pr_body = fields.Char('Pr Body')
3131
pr_author = fields.Char('Pr Author')
3232

33-
pull_head_name = fields.Char(compute='_compute_branch_infos', string='PR HEAD name', readonly=True, store=True)
34-
pull_head_remote_id = fields.Many2one('runbot.remote', 'Pull head repository', compute='_compute_branch_infos', store=True, index=True)
35-
target_branch_name = fields.Char(compute='_compute_branch_infos', string='PR target branch', store=True)
33+
pull_head_name = fields.Char(string='PR HEAD name', readonly=True, store=True)
34+
pull_head_remote_id = fields.Many2one('runbot.remote', 'Pull head repository', store=True, index=True)
35+
target_branch_name = fields.Char( string='PR target branch', store=True)
3636
reviewers = fields.Char('Reviewers')
3737

3838
reflog_ids = fields.One2many('runbot.ref.log', 'branch_id')
@@ -41,7 +41,7 @@ class Branch(models.Model):
4141
dname = fields.Char('Display name', compute='_compute_dname', search='_search_dname')
4242

4343
alive = fields.Boolean('Alive', default=True)
44-
draft = fields.Boolean('Draft', compute='_compute_branch_infos', store=True)
44+
draft = fields.Boolean('Draft', store=True)
4545

4646
@api.depends('name', 'remote_id.short_name')
4747
def _compute_dname(self):
@@ -64,7 +64,7 @@ def _compute_reference_name(self):
6464
- pull_head_name (organisation:branch_name) for external pr
6565
"""
6666
for branch in self:
67-
if branch.is_pr:
67+
if branch.is_pr and branch.pull_head_name:
6868
_, name = branch.pull_head_name.split(':')
6969
if branch.pull_head_remote_id:
7070
reference_name = name
@@ -79,8 +79,7 @@ def _compute_reference_name(self):
7979
reference_name = f'{forced_version.name}---{reference_name}'
8080
branch.reference_name = reference_name
8181

82-
@api.depends('name')
83-
def _compute_branch_infos(self, pull_info=None):
82+
def _update_branch_infos(self, pull_info=None):
8483
"""compute branch_url, pull_head_name and target_branch_name based on name"""
8584
name_to_remote = {}
8685
prs = self.filtered(lambda branch: branch.is_pr)
@@ -140,18 +139,15 @@ def _compute_branch_url(self):
140139
else:
141140
branch.branch_url = ''
142141

143-
@api.depends('reference_name', 'remote_id.repo_id.project_id')
144-
def _compute_bundle_id(self):
142+
def _update_bundle_id(self):
145143
for branch in self:
146144
dummy = branch.remote_id.repo_id.project_id.dummy_bundle_id
147-
if branch.bundle_id == dummy:
148-
continue
149145
name = branch.reference_name
150146
project = branch.remote_id.repo_id.project_id or self.env.ref('runbot.main_project')
151147
project.ensure_one()
152148
bundle = self.env['runbot.bundle'].search([('name', '=', name), ('project_id', '=', project.id)])
153-
need_new_base = not bundle and branch._match_is_base(name)
154-
if (bundle.is_base or need_new_base) and branch.remote_id != branch.remote_id.repo_id.main_remote_id:
149+
is_base = bundle.is_base if bundle else branch._match_is_base(name)
150+
if is_base and branch.remote_id != branch.remote_id.repo_id.main_remote_id:
155151
_logger.warning('Trying to add a dev branch to base bundle, falling back on dummy bundle')
156152
bundle = dummy
157153
elif name and branch.remote_id and branch.remote_id.repo_id._is_branch_forbidden(name):
@@ -165,7 +161,7 @@ def _compute_bundle_id(self):
165161
'name': name,
166162
'project_id': project.id,
167163
}
168-
if need_new_base:
164+
if is_base:
169165
values['is_base'] = True
170166

171167
if branch.is_pr and branch.target_branch_name: # most likely external_pr, use target as version
@@ -183,6 +179,8 @@ def _compute_bundle_id(self):
183179
@api.model_create_multi
184180
def create(self, value_list):
185181
branches = super().create(value_list)
182+
branches._update_branch_infos()
183+
branches._update_bundle_id()
186184
for branch in branches:
187185
if branch.head:
188186
self.env['runbot.ref.log'].create({'commit_id': branch.head.id, 'branch_id': branch.id})
@@ -214,8 +212,9 @@ def _recompute_infos(self, payload=None):
214212
was_draft = self.draft
215213
was_alive = self.alive
216214
init_target_branch_name = self.target_branch_name
217-
self._compute_branch_infos(payload)
215+
self._update_branch_infos(payload)
218216
if self.target_branch_name != init_target_branch_name:
217+
#retarget
219218
_logger.info('retargeting %s to %s', self.name, self.target_branch_name)
220219
base = self.env['runbot.bundle'].search([
221220
('name', '=', self.target_branch_name),
@@ -230,6 +229,10 @@ def _recompute_infos(self, payload=None):
230229
if self.draft:
231230
self.reviewers = '' # reset reviewers on draft
232231

232+
if not self.bundle_id:
233+
self._update_bundle_id()
234+
return
235+
233236
if was_alive and not self.alive and self.bundle_id.for_next_freeze:
234237
if not any(branch.alive and branch.is_pr for branch in self.bundle_id.branch_ids):
235238
self.bundle_id.for_next_freeze = False
@@ -246,10 +249,13 @@ def _match_is_base(self, name):
246249
regex = icp.get_param('runbot.runbot_is_base_regex', False)
247250
if regex:
248251
return re.match(regex, name)
249-
252+
250253
def action_recompute_infos(self):
251254
return self._recompute_infos()
252255

256+
def action_update_bundle_id(self):
257+
return self._update_bundle_ids()
258+
253259

254260
class RefLog(models.Model):
255261
_name = 'runbot.ref.log'

runbot/models/build_config_codeowner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def _run_codeowner(self, build):
149149

150150
build._log('', 'Requesting review for pull request [%s](%s): %s' % (pr.dname, pr.branch_url, ', '.join(new_reviewers)), log_type='markdown')
151151
response = pr.remote_id._github('/repos/:owner/:repo/pulls/%s/requested_reviewers' % pr.name, {"team_reviewers": list(new_reviewers)}, ignore_errors=False)
152-
pr._compute_branch_infos(response)
152+
pr._update_branch_infos(response)
153153
pr['reviewers'] = ','.join(sorted(reviewers))
154154
else:
155155
build._log('', 'All reviewers are already on pull request [%s](%s)' % (pr.dname, pr.branch_url,), log_type='markdown')

runbot/tests/common.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,17 @@ def setUp(self):
125125
'is_pr': False,
126126
'remote_id': self.remote_server.id,
127127
})
128-
self.dev_pr = self.Branch.create({
129-
'name': '1234',
130-
'is_pr': True,
131-
'remote_id': self.remote_server.id,
132-
'target_branch_name': self.dev_bundle.base_id.name,
133-
'pull_head_remote_id': self.remote_server.id,
134-
})
135-
self.dev_pr.pull_head_name = f'{self.remote_server.owner}:{self.dev_branch.name}'
136-
self.dev_pr.bundle_id = self.dev_bundle.id,
128+
with patch('odoo.addons.runbot.models.branch.Branch._update_branch_infos', return_value=None):
129+
self.dev_pr = self.Branch.create({
130+
'name': '1234',
131+
'is_pr': True,
132+
'alive': True,
133+
'remote_id': self.remote_server.id,
134+
'target_branch_name': self.dev_bundle.base_id.name,
135+
'pull_head_remote_id': self.remote_server.id,
136+
'pull_head_name': f'{self.remote_server.owner}:{self.dev_branch.name}',
137+
})
138+
self.assertEqual(self.dev_pr.bundle_id.id, self.dev_bundle.id)
137139

138140
self.dev_batch = self.Batch.create({
139141
'bundle_id': self.dev_bundle.id,

runbot/tests/test_build_config_step.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def test_codeowner_pr_duplicate(self):
9696
'remote_id': self.remote_server.id,
9797
'target_branch_name': self.dev_bundle.base_id.name,
9898
'pull_head_remote_id': self.remote_server.id,
99+
'pull_head_name': f'{self.remote_server.owner}:{self.dev_branch.name}',
99100
})
100-
second_pr.pull_head_name = f'{self.remote_server.owner}:{self.dev_branch.name}'
101-
second_pr.bundle_id = self.dev_bundle.id
101+
self.assertEqual(second_pr.bundle_id.id, self.dev_bundle.id)
102102
self.config_step._run_codeowner(self.parent_build)
103103
self.assertEqual(self.parent_build.log_ids.mapped('message'), [
104104
"More than one open pr in this bundle for server: ['1234', '1235']"

runbot/views/branch_views.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<form>
88
<header>
99
<button name="action_recompute_infos" string="Recompute Infos" type="object" class="oe_highlight"/>
10+
<button name="action_update_bundle_id" string="Update bundle" type="object" class="oe_highlight"/>
1011
</header>
1112
<sheet>
1213
<group name="branch_group">

0 commit comments

Comments
 (0)