summaryrefslogtreecommitdiff
path: root/lorrycontroller/gitlab.py
diff options
context:
space:
mode:
Diffstat (limited to 'lorrycontroller/gitlab.py')
-rw-r--r--lorrycontroller/gitlab.py70
1 files changed, 51 insertions, 19 deletions
diff --git a/lorrycontroller/gitlab.py b/lorrycontroller/gitlab.py
index 13becfe..90ba192 100644
--- a/lorrycontroller/gitlab.py
+++ b/lorrycontroller/gitlab.py
@@ -13,7 +13,15 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+'''
+Run commands on a GitLab instance.
+This uses the python wrapper around the GitLab API.
+Use of the API requires the private token of a user with master access
+to the targetted group.
+'''
+
+import logging
import re
import urllib.parse
@@ -22,40 +30,57 @@ try:
except ImportError:
gitlab = None
+from . import hosts
+
class MissingGitlabModuleError(Exception):
pass
-class Gitlab(object):
+def _init_gitlab(host, token):
+ if gitlab:
+ url = "http://" + host
+ return gitlab.Gitlab(url, token)
+ else:
+ raise MissingGitlabModuleError('gitlab module missing\n'
+ '\tpython-gitlab is required with GitLab as the git server')
- '''Run commands on a GitLab instance.
- This uses the python wrapper around the GitLab API.
- Use of the API requires the private token of a user with master access
- to the targetted group.
+class GitlabDownstream(hosts.DownstreamHost):
+ @staticmethod
+ def add_app_settings(app_settings):
+ app_settings.string(
+ ['gitlab-private-token'],
+ 'private token for GitLab API access')
- '''
+ @staticmethod
+ def check_app_settings(app_settings):
+ if not app_settings['gitlab-private-token']:
+ logging.error('A private token must be provided to create '
+ 'repositories on a GitLab instance.')
+ app_settings.require('gitlab-private-token')
- def __init__(self, host, token):
- if gitlab:
- url = "http://" + host
- self.gl = gitlab.Gitlab(url, token)
- else:
- raise MissingGitlabModuleError('gitlab module missing\n'
- '\tpython-gitlab is required with GitLab as the git server')
+ def __init__(self, app_settings):
+ # XXX This needs to be configurable
+ host = 'localhost'
- def find_project(self, repo_path):
- return self.gl.projects.get(repo_path)
+ self.gl = _init_gitlab(host, app_settings['gitlab-private-token'])
- def has_project(self, repo_path):
+ def _has_project(self, repo_path):
try:
- self.find_project(repo_path)
+ self.gl.projects.get(repo_path)
return True
except gitlab.GitlabGetError:
return False
- def create_project(self, repo_path):
+ def prepare_repo(self, repo_path, metadata):
+ # TODO: set metadata
+
+ if self._has_project(repo_path):
+ logging.info('Project %s exists in local GitLab already.',
+ repo_path)
+ return
+
path_comps = repo_path.split('/')
if len(path_comps) < 2:
@@ -87,6 +112,13 @@ class Gitlab(object):
}
self.gl.projects.create(project)
+ logging.info('Created %s project in local GitLab.', repo_path)
+
+
+class Gitlab(object):
+ def __init__(self, host, token):
+ self.gl = _init_gitlab(host, token)
+
def list_projects(self):
'''List projects on a GitLab instance.'''
@@ -105,7 +137,7 @@ class Gitlab(object):
format matching 'http(s)://host/group/project.git'.
'''
- project = self.find_project(project_path)
+ project = self.gl.projects.get(repo_path)
if protocol == 'ssh':
return project.ssh_url_to_repo