Skip to content

Commit

Permalink
[FIX] runbot: fix branch search
Browse files Browse the repository at this point in the history
Allows searching branch through pr url or full branch name.
Also fixes a crash when searching invalid values.
  • Loading branch information
Williambraecky committed Jan 15, 2025
1 parent 33bef24 commit f95c2be
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
18 changes: 13 additions & 5 deletions runbot/models/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ def _compute_dname(self):
branch.dname = '%s:%s' % (branch.remote_id.short_name, branch.name)

def _search_dname(self, operator, value):
if ':' not in value:
return [('name', operator, value)]
repo_short_name, branch_name = value.split(':')
owner, repo_name = repo_short_name.split('/')
return ['&', ('remote_id', '=', self.env['runbot.remote'].search([('owner', '=', owner), ('repo_name', '=', repo_name)]).id), ('name', operator, branch_name)]
# Match format (owner?, repo, branch)
owner = repo = branch = None
if (m := re.match(r'(?:([\w-]+)/)?([\w-]+):([\w\.-]+)', value)):
owner, repo, branch = m.groups()
# Match PR url format
if (m := re.search(r'/([\w-]+)/([\w-]+)/pull/(\d+)', value)):
owner, repo, branch = m.groups()
if repo and branch:
domain = [('name', operator, branch), ('remote_id.repo_name', '=', repo)]
if owner:
domain.append(('remote_id.owner', '=', owner))
return domain
return [('name', operator, value)]

@api.depends('name', 'is_pr', 'target_branch_name', 'pull_head_name', 'pull_head_remote_id')
def _compute_reference_name(self):
Expand Down
32 changes: 32 additions & 0 deletions runbot/tests/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ def test_pull_request(self):
self.assertEqual(pr.target_branch_name, 'master')
self.assertEqual(pr.pull_head_name, 'foo-dev:bar_branch')

def test_branch_dname_search(self):
# Basic branch
self.assertEqual(
self.branch_server,
self.Branch.search([('dname', '=', self.branch_server.dname)]),
)
# Basic pr
self.assertEqual(
self.dev_pr,
self.Branch.search([('dname', '=', self.dev_pr.dname)]),
)
# PR from pull request url
self.assertEqual(
self.dev_pr,
self.Branch.search([('dname', '=', self.dev_pr.branch_url)]),
)
# With subtree of PR url
self.assertEqual(
self.dev_pr,
self.Branch.search([('dname', '=', self.dev_pr.branch_url + '/files')]),
)
# Branch with a . inside of it
branch = self.Branch.create({
'name': '18.0-test',
'remote_id': self.remote_server.id,
'is_pr': False,
})
self.assertEqual(
branch,
self.Branch.search([('dname', '=', branch.dname)]),
)

class TestBranchRelations(RunbotCase):

def setUp(self):
Expand Down

0 comments on commit f95c2be

Please sign in to comment.