Skip to content
Snippets Groups Projects
Commit a9e8258d authored by Walter Lozano's avatar Walter Lozano Committed by Dylan Aïssi
Browse files

gitlab-check: Rework data handling


In order to improve consistncy and to make the code easier to read move
variables which set the behavior of the code to be in the object. While
doing this rework how config file is handled to avoid changing the values
in memory.

Signed-off-by: default avatarWalter Lozano <walter.lozano@collabora.com>
parent 82c51df3
No related branches found
No related tags found
1 merge request!198Improvements to release pipeline (5)
......@@ -12,10 +12,10 @@ RETRY_JOBS = 2
RETRY_RETRIGGER = 3
class GitlabCheck():
def __init__(self, url = None, token = None, instance = None):
if url:
self.gitlab = gitlab.Gitlab(url, token)
elif instance:
def __init__(self, config, args):
if args.url:
self.gitlab = gitlab.Gitlab(args.url, args.token)
elif args.instance:
self.gitlab = gitlab.Gitlab.from_config(config['instance'])
elif config['url']:
self.gitlab = gitlab.Gitlab(config['url'], config['token'])
......@@ -26,33 +26,41 @@ class GitlabCheck():
self.gitlab.auth()
def expand_projects(self, config):
projects_to_check = set(config['projects'])
self.all = args.all
self.config = args.config
self.expanded_projects = []
self.fetch_all = args.fetch_all
self.projects = None
self.ref = args.ref or config['ref']
self.retry = args.retry
self.status = args.status or config['status']
self.verbose = args.verbose
def expand_projects(self, projects):
projects_to_check = set(projects)
for project in projects_to_check:
print(f'Expanding {project} to:')
if project.endswith("/*"):
project_path = project.replace('/*', '')
project_group = gitlab_check.gitlab.groups.get(project_path, lazy=True)
project_group = self.gitlab.groups.get(project_path, lazy=True)
list_subprojects = project_group.projects.list(get_all=True)
for subproject in list_subprojects:
subproject_id = gitlab_check.gitlab.projects.get(subproject.id)
subproject_id = self.gitlab.projects.get(subproject.id)
subproject_branches = subproject_id.branches.list(get_all=True)
subproject_branches = [project.name for project in subproject_branches]
if set(config['ref']) & set(subproject_branches):
config['projects'].append(subproject_id.path_with_namespace)
if set(self.ref) & set(subproject_branches):
self.expanded_projects.append(subproject_id.path_with_namespace)
print(f' {subproject_id.path_with_namespace}')
else:
self.expanded_projects.append(project)
config['projects'].remove(project)
return config
def fetch_pipelines(self, project_name, fetch_all = False):
def fetch_pipelines(self, project_name):
project = self.gitlab.projects.get(project_name)
pipelines = project.pipelines.list(get_all=fetch_all)
pipelines = project.pipelines.list(get_all=self.fetch_all)
return pipelines
......@@ -80,21 +88,19 @@ class GitlabCheck():
return pipelines_cache.values()
def filter_pipelines(self, pipelines, ref = None, status = None):
ref = ref or config['ref']
if ref:
pipelines = [p for p in pipelines if p.ref in ref]
status = status or config['status']
if status and status != '-':
pipelines = [p for p in pipelines if p.status == status]
def filter_pipelines(self, pipelines):
if self.ref:
pipelines = [p for p in pipelines if p.ref in self.ref]
if self.status and self.status != '-':
pipelines = [p for p in pipelines if p.status == self.status]
return pipelines
def print_pipelines(self, project_name, pipelines, verbose = 1):
def print_pipelines(self, project_name, pipelines):
for p in pipelines:
if verbose == 1:
if self.verbose == 1:
print(project_name, p.id, p.ref, p.created_at, p.status)
elif verbose == 2:
elif self.verbose == 2:
print(project_name, p.id, p.ref, p.created_at, p.status, p.web_url)
else:
print(p)
......@@ -104,7 +110,7 @@ class GitlabCheck():
print(f'Retrying pipelines for {project_name} {p.ref}')
p.retry()
def retry_jobs(self, project_name, pipelines, fetch_all = False):
def retry_jobs(self, project_name, pipelines):
def is_in_cache(jobs_cache, job):
for j in jobs_cache:
if j.name == job.name and j.id > job.id:
......@@ -113,7 +119,7 @@ class GitlabCheck():
for p in pipelines:
project = self.gitlab.projects.get(project_name)
jobs = project.jobs.list(get_all=fetch_all)
jobs = project.jobs.list(get_all=self.fetch_all)
jobs_cache = []
for j in jobs:
if j.ref != p.ref:
......@@ -131,21 +137,21 @@ class GitlabCheck():
pipeline = project.pipelines.create({'ref': p.ref})
print(f'Pipeline {pipeline}')
def check_pipelines(self, all, ref, status, fetch_all = False, retry = False, verbose = 1):
def check_pipelines(self):
pipeline_count = 0
for p in config['projects']:
pipelines = self.fetch_pipelines(p, fetch_all)
if not all:
for p in self.expanded_projects:
pipelines = self.fetch_pipelines(p)
if not self.all:
pipelines = self.existing_branches(p, pipelines)
pipelines = self.newest_pipelines(pipelines)
pipelines = self.filter_pipelines(pipelines, ref, status)
pipelines = self.filter_pipelines(pipelines)
pipeline_count += len(pipelines)
self.print_pipelines(p, pipelines, verbose)
if retry == RETRY_PIPELINE:
self.print_pipelines(p, pipelines)
if self.retry == RETRY_PIPELINE:
self.retry_pipelines(p, pipelines)
if retry == RETRY_JOBS:
self.retry_jobs(p, pipelines, fetch_all)
elif retry == RETRY_RETRIGGER:
if self.retry == RETRY_JOBS:
self.retry_jobs(p, pipelines)
elif self.retry == RETRY_RETRIGGER:
self.retrigger_pipeline(p, pipelines)
return pipeline_count
......@@ -177,12 +183,12 @@ if __name__ == '__main__':
config.load_config(args)
gitlab_check = GitlabCheck(args.url, args.token, args.instance)
gitlab_check = GitlabCheck(config, args)
config = gitlab_check.expand_projects(config)
gitlab_check.expand_projects(config['projects'])
if args.action == 'check-pipelines':
pipeline_count = gitlab_check.check_pipelines(args.all, args.ref, args.status, args.fetch_all, args.retry, args.verbose)
pipeline_count = gitlab_check.check_pipelines()
if args.error and pipeline_count:
print(f'There are {pipeline_count} pipelines with failure')
sys.exit(1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment