From f446474988851a0f9dbccb8706c3be52289bb72e Mon Sep 17 00:00:00 2001 From: "David V. Lu" Date: Sat, 29 Sep 2018 09:36:32 -0400 Subject: [PATCH 1/3] Gitlab library --- bloom/gitlab.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 bloom/gitlab.py diff --git a/bloom/gitlab.py b/bloom/gitlab.py new file mode 100644 index 00000000..ac1be5cb --- /dev/null +++ b/bloom/gitlab.py @@ -0,0 +1,90 @@ +import requests + + +class GitlabException(Exception): + def __init__(self, msg, code=None): + if code: + msg = "{msg}: {code}".format(**locals()) + super(GitlabException, self).__init__(msg) + + +class GitlabAuthException(GitlabException): + def __init__(self, msg, code=None): + super(GitlabAuthException, self).__init__(msg, code) + + +class Gitlab(object): + def __init__(self, server, token=None): + self.server = server + self.token = token + self.api_version = 4 + self.base_api_url = 'http://{server}/api/v{api_version}'.format(server=server, api_version=self.api_version) + + def update_params(self, params): + if self.token: + if params is None: + params = {} + params['private_token'] = self.token + return params + + def api_get(self, query, params=None): + r = requests.get(self.base_api_url + query, params=self.update_params(params)) + if r.status_code == 401: + raise GitlabAuthException('Authentication Failed', r.status_code) + elif r.status_code == 200: + return r.json() + else: + raise GitlabException('Query {} failed.'.format(query), r.status_code) + + def api_post(self, query, params=None): + r = requests.post(self.base_api_url + query, params=self.update_params(params)) + if r.status_code == 401: + raise GitlabAuthException('Authentication Failed', r.status_code) + elif r.status_code == 201: + return r.json() + else: + raise GitlabException('Query {} failed.'.format(query), r.status_code) + + def auth(self): + """ Authenticate by trying to get the projects """ + self.api_get('/projects') + + def get_repo(self, owner, repo): + path = '{}/{}'.format(owner, repo) + for repo_d in self.api_get('/projects', {'search': repo}): + if repo_d.get('path_with_namespace', '') == path: + return repo_d + + def list_branches(self, repo): + res = self.api_get('/projects/{}/repository/branches'.format(repo['id'])) + return [d['name'] for d in res] + + def create_branch(self, repo, new_branch, base_branch): + params = {'branch': new_branch, 'ref': base_branch} + return self.api_post('/projects/{}/repository/branches'.format(repo['id']), params) + + def create_commit(self, repo, branch, commit_message, actions): + params = { + 'branch': branch, + 'commit_message': commit_message, + 'actions': actions + } + return self.api_post('/projects/{}/repository/commits'.format(repo['id']), params) + + def update_file(self, repo, branch, commit_message, file_path, new_contents): + actions = [{ + 'action': 'update', + 'file_path': file_path, + 'content': new_contents + } + ] + return self.create_commit(repo, branch, commit_message, actions) + + def create_pull_request(self, repo, source_branch, target_branch, title, body=''): + params = { + 'source_branch': source_branch, + 'target_branch': target_branch, + 'title': title, + 'description': body + } + return self.api_post('/projects/{}/merge_requests'.format(repo['id']), params) From d9141662e7f9d8fc4bf5f8fc04fcf821ed8b620e Mon Sep 17 00:00:00 2001 From: "David V. Lu" Date: Thu, 11 Oct 2018 15:03:43 -0400 Subject: [PATCH 2/3] Update dependencies --- setup.py | 1 + stdeb.cfg | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index c7cbb394..d6224bd9 100755 --- a/setup.py +++ b/setup.py @@ -9,6 +9,7 @@ 'empy', 'python-dateutil', 'PyYAML', + 'requests >= 2.2', 'rosdep >= 0.10.25', 'rosdistro >= 0.4.0', 'vcstools >= 0.1.22', diff --git a/stdeb.cfg b/stdeb.cfg index f0c2bf54..2837cf4c 100755 --- a/stdeb.cfg +++ b/stdeb.cfg @@ -1,6 +1,6 @@ [DEFAULT] -Depends: python-yaml, python-empy, python-argparse, python-rosdep (>= 0.10.25), python-rosdistro (>= 0.4.0), python-vcstools (>= 0.1.22), python-setuptools, python-catkin-pkg (>= 0.4.3) -Depends3: python3-yaml, python3-empy, python3-rosdep (>= 0.10.25), python3-rosdistro (>= 0.4.0), python3-vcstools (>= 0.1.22), python3-setuptools, python3-catkin-pkg (>= 0.4.3) +Depends: python-yaml, python-empy, python-argparse, python-rosdep (>= 0.10.25), python-rosdistro (>= 0.4.0), python-vcstools (>= 0.1.22), python-setuptools, python-catkin-pkg (>= 0.4.3), python-requests (>= 2.2) +Depends3: python3-yaml, python3-empy, python3-rosdep (>= 0.10.25), python3-rosdistro (>= 0.4.0), python3-vcstools (>= 0.1.22), python3-setuptools, python3-catkin-pkg (>= 0.4.3), python3-requests (>= 2.2) Conflicts: python3-bloom Conflicts3: python-bloom Copyright-File: LICENSE.txt From aac9b637b93738512865c8595723c23003db31fe Mon Sep 17 00:00:00 2001 From: "David V. Lu" Date: Mon, 5 Nov 2018 11:09:11 -0500 Subject: [PATCH 3/3] Remove req from setup.py --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index d6224bd9..c7cbb394 100755 --- a/setup.py +++ b/setup.py @@ -9,7 +9,6 @@ 'empy', 'python-dateutil', 'PyYAML', - 'requests >= 2.2', 'rosdep >= 0.10.25', 'rosdistro >= 0.4.0', 'vcstools >= 0.1.22',