Commit 5f0e3528 authored by Thomas Lotze's avatar Thomas Lotze

re #22: preselect listed projects by searching for group and project

This is no replacement for applying the filters later but speeds up
project retrieval noticeably for large Gitlab instances.
parent f12b5346
...@@ -38,10 +38,12 @@ logging.basicConfig(format=logging.BASIC_FORMAT) ...@@ -38,10 +38,12 @@ logging.basicConfig(format=logging.BASIC_FORMAT)
@click.option( @click.option(
'--group', '--group',
help='Filter for projects in group', help='Filter for projects in group',
default='',
) )
@click.option( @click.option(
'--project', '--project',
help='Filter for project name', help='Filter for project name',
default='',
) )
@click.option( @click.option(
'--username', '--username',
......
...@@ -12,30 +12,35 @@ def fetch(url, token, filter): ...@@ -12,30 +12,35 @@ def fetch(url, token, filter):
""" """
gitlab = Gitlab(url, token) gitlab = Gitlab(url, token)
projects = gitlab.projects.list(all=True)
projects.sort(key=lambda p: p.path_with_namespace)
# We iterate over all projects regardless of our filter settings. When # We iterate over all projects loosely matching group and project filters
# and then apply the filters in a more precisely defined manner. When
# filtering for a group, we cannot list all groups first; if we try to do # filtering for a group, we cannot list all groups first; if we try to do
# so, Gitlab will only return groups associated with the api token. When # so, Gitlab will only return groups associated with the api token. When
# filtering for a project name, we have no better way of searching a # filtering for a project name, we have no way of searching a project
# project by name. When filtering for a user, we have no way at all of # by exact name. When filtering for a user, we have no way at all of
# querying all notes authored by that user. As we want to catch all time # querying all notes authored by that user. As we want to catch all time
# spent by the user in the past regardless of their then or present # spent by the user in the past regardless of their then or present
# affiliations with groups, projects, issues etc, we won't try to be # affiliations with groups, projects, issues etc, we won't try to be
# clever and optimise here, even if this results in a full-database scan. # clever and optimise here, even if this results in a full-database scan.
# Let's see whether we'll be forced to optimise by production data. # Let's see whether we'll be forced to optimise by production data.
pattern = ' '.join((filter['group'], filter['project']))
projects = gitlab.projects.list(
search=pattern, search_namespaces=True, all=True)
projects.sort(key=lambda p: p.path_with_namespace)
for project in projects: for project in projects:
if filter['project'] not in ( if filter['project'] not in (
project.name, project.name,
project.path, project.path,
project.path_with_namespace, project.path_with_namespace,
None): ''):
continue continue
if filter['group'] not in ( if filter['group'] not in (
project.namespace['name'], project.namespace['name'],
project.namespace['path'], project.namespace['path'],
None): ''):
continue continue
yield project, fetch_project(project, filter) yield project, fetch_project(project, filter)
......
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