Commit 5e7863f8 authored by Peter Hutterer's avatar Peter Hutterer
Browse files

ci-fairy: factor out fetching the project from gitlab


Signed-off-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
parent 639808ac
......@@ -14,6 +14,26 @@ import yaml
from pathlib import Path
def gitlab_project(gitlab, project=None):
'''
Returns the Gitlab Project object for the given project name. Where name
is None, it is taken from the CI environment.
'''
if project is None:
project_id = os.getenv('CI_PROJECT_ID')
if project_id is None:
return None
p = gitlab.projects.get(project_id)
else:
p = gitlab.projects.list(search=project)
if len(p) != 1:
logger.error('Invalid or ambiguous project path')
return None
p = p[0]
return p
class ColorFormatter(logging.Formatter):
def format(self, record):
COLORS = {
......@@ -264,19 +284,10 @@ def delete_image(ctx, repository, project, all, exclude_tag, tag, dry_run):
logger.error('One of --tag, --exclude-tag, or --all is required.')
sys.exit(1)
if project is None:
project_id = os.getenv('CI_PROJECT_ID')
if project_id is None:
logger.error('Missing project identifier')
sys.exit(1)
p = ctx.obj.gitlab.projects.get(project_id)
else:
p = ctx.obj.gitlab.projects.list(search=project)
if len(p) != 1:
logger.error('Invalid or ambiguous project path')
sys.exit(1)
p = p[0]
p = gitlab_project(ctx.obj.gitlab, project)
if not p:
logger.error('Unable to find or identify project')
sys.exit(1)
repos = [r for r in p.repositories.list() if repository is None or repository == r.name]
for repo in repos:
logger.debug('Repository {}'.format(repo.name))
......
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