Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] runbot: fix branch search #1016

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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