Commit 1df78c3b authored by Peter Hutterer's avatar Peter Hutterer
Browse files

ci-fairy: limit the project path search to only exact matches



Where we have more than one match, search all of them for an exact match and
use that.
Signed-off-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
parent 2797c5c7
......@@ -26,10 +26,11 @@ def gitlab_project(gitlab, project=None):
p = gitlab.projects.get(project_id)
else:
p = gitlab.projects.list(search=project, search_namespaces=True)
if len(p) != 1:
logger.error('Invalid or ambiguous project path')
return None
p = p[0]
# gitlab does substring matches, so foo/bar matches foo/barbaz as
# well
if len(p) > 1:
p = [x for x in p if x.name == project]
p = p[0] if len(p) == 1 else None
return p
......
......@@ -67,6 +67,58 @@ def mock_gitlab(gitlab):
return ctx, project, repos
@patch('ci_fairy.Gitlab')
def test_project_lookup(gitlab, caplog, gitlab_default_env, monkeypatch):
# instantiate here so ci-fairy will instantiate the same objects
ctx = gitlab(GITLAB_TEST_URL)
monkeypatch.setenv('CI_PROJECT_ID', '3')
class Project():
def __init__(self, name, id):
self.name = name
self.id = id
projects = []
for idx, name in enumerate(['foo', 'user/foo', 'foobar', 'foo/foo']):
projects.append(Project(name, idx))
def pget(pid):
return [p for p in projects if p.id == int(pid)][0]
ctx.projects.get = MagicMock(side_effect=pget)
def plist(search, search_namespaces=True):
return [p for p in projects if search in p.name]
ctx.projects.list = MagicMock(side_effect=plist)
# default is CI_PROJECT_ID
p = ci_fairy.gitlab_project(ctx, None)
assert p.name == 'foo/foo'
assert p.id == 3
# exact match
p = ci_fairy.gitlab_project(ctx, 'user/foo')
assert p.name == 'user/foo'
assert p.id == 1
# require exact match over substring match
p = ci_fairy.gitlab_project(ctx, 'foo')
assert p.name == 'foo'
assert p.id == 0
# exact match
p = ci_fairy.gitlab_project(ctx, 'foobar')
assert p.name == 'foobar'
assert p.id == 2
# exact match
p = ci_fairy.gitlab_project(ctx, 'foo/foo')
assert p.name == 'foo/foo'
assert p.id == 3
def test_missing_url(caplog, gitlab_default_env):
args = ['delete-image', '--all', '--dry-run'] # need one subcommand
......@@ -175,7 +227,7 @@ def test_delete_image_invalid_project(gitlab, caplog, gitlab_default_env):
runner = CliRunner(env=gitlab_default_env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 1
assert 'Invalid or ambiguous project path' in [r.msg for r in caplog.records]
assert 'Unable to find or identify project' in [r.msg for r in caplog.records]
@pytest.mark.parametrize('reponame', [None, 'repository-3'])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment