From 91f046b71e0ec46c957da3055a268ff8f0ba45c4 Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Wed, 13 May 2020 16:40:43 +0100 Subject: Implement getting and setting repo metadata for all host types * GitlabUpstream: Implement get_repo_metadata * {Gerrit,Gitlab,Local}Downstream: Use given metadata in prepare_repo --- lorrycontroller/gerrit.py | 20 +++++++++++++++----- lorrycontroller/gitlab.py | 32 +++++++++++++++++++++----------- lorrycontroller/local.py | 10 +++++++++- 3 files changed, 45 insertions(+), 17 deletions(-) (limited to 'lorrycontroller') diff --git a/lorrycontroller/gerrit.py b/lorrycontroller/gerrit.py index 6fde905..dd82ff8 100644 --- a/lorrycontroller/gerrit.py +++ b/lorrycontroller/gerrit.py @@ -66,12 +66,22 @@ class GerritDownstream(hosts.DownstreamHost): The 'lorry' user must have createProject capability in the Gerrit. ''' - # TODO: set metadata if self._has_project(name): logging.info('Project %s exists in local Gerrit already.', name) - return - - self._ssh_command(['gerrit', 'create-project', name]) - logging.info('Created %s project in local Gerrit.', name) + else: + self._ssh_command(['gerrit', 'create-project', name]) + logging.info('Created %s project in local Gerrit.', name) + + # We can only set this metadata if we're the owner of the + # repository. For now, ignore failures. + try: + if 'head' in metadata: + self._ssh_command(['gerrit', 'set-head', name, + '--new-head', metadata['head']]) + if 'description' in metadata: + self._ssh_command(['gerrit', 'set-project', name, + '-d', metadata['description']]) + except cliapp.AppException: + pass diff --git a/lorrycontroller/gitlab.py b/lorrycontroller/gitlab.py index ab6df63..4f70f0a 100644 --- a/lorrycontroller/gitlab.py +++ b/lorrycontroller/gitlab.py @@ -66,19 +66,22 @@ class GitlabDownstream(hosts.DownstreamHost): self.gl = _init_gitlab(host, app_settings['gitlab-private-token']) - def _has_project(self, repo_path): - try: - self.gl.projects.get(repo_path) - return True - except gitlab.GitlabGetError: - return False - def prepare_repo(self, repo_path, metadata): - # TODO: set metadata - if self._has_project(repo_path): + try: + project = self.gl.projects.get(repo_path) + except gitlab.GitlabGetError: + pass + else: logging.info('Project %s exists in local GitLab already.', repo_path) + if 'head' in metadata \ + and project.default_branch != metadata['head']: + project.default_branch = metadata['head'] + if 'description' in metadata \ + and project.description != metadata['description']: + project.description = metadata['description'] + project.save() return path_comps = repo_path.split('/') @@ -109,6 +112,8 @@ class GitlabDownstream(hosts.DownstreamHost): 'public': True, 'merge_requests_enabled': False, 'namespace_id': group.id, + 'default_branch': metadata.get('head'), + 'description': metadata.get('description'), } self.gl.projects.create(project) @@ -157,5 +162,10 @@ class GitlabUpstream(hosts.UpstreamHost): self._protocol, split.netloc, split.path, '', '')) def get_repo_metadata(self, repo_path): - # TODO - return {} + project = self.gl.projects.get(repo_path) + metadata = {} + if project.default_branch is not None: + metadata['head'] = project.default_branch + if project.description is not None: + metadata['description'] = project.description + return metadata diff --git a/lorrycontroller/local.py b/lorrycontroller/local.py index 15a4048..d55214d 100644 --- a/lorrycontroller/local.py +++ b/lorrycontroller/local.py @@ -15,6 +15,7 @@ import logging import os +import os.path import cliapp @@ -45,4 +46,11 @@ class LocalDownstream(hosts.DownstreamHost): # whether the repository already exists os.makedirs(repo_path, exist_ok=True) cliapp.runcmd(['git', 'init', '--bare', repo_path]) - # TODO: set metadata + + if 'head' in metadata: + cliapp.runcmd(['git', '--git-dir', repo_path, + 'symbolic-ref', 'HEAD', + 'refs/heads/' + metadata['head']]) + if 'description' in metadata: + with open(os.path.join(repo_path, 'description'), 'w') as f: + print(metadata['description'], file=f) -- cgit v1.2.1