Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gitlab library #490

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions bloom/gitlab.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions stdeb.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[DEFAULT]
; release with a high debinc to avoid conflict with upstream debian of the same release version
Debian-Version: 100
Depends: python-yaml, python-empy, python-argparse, python-rosdep (>= 0.15.0), python-rosdistro (>= 0.8.0), python-vcstools (>= 0.1.22), python-setuptools, python-catkin-pkg (>= 0.4.3)
Depends3: python3-yaml, python3-empy, python3-rosdep (>= 0.15.0), python3-rosdistro (>= 0.8.0), python3-vcstools (>= 0.1.22), python3-setuptools, python3-catkin-pkg (>= 0.4.3)
==== BASE ====
Depends: python-yaml, python-empy, python-argparse, python-rosdep (>= 0.15.0), python-rosdistro (>= 0.7.5), 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.15.0), python3-rosdistro (>= 0.7.5), python3-vcstools (>= 0.1.22), python3-setuptools, python3-catkin-pkg (>= 0.4.3), python3-requests (>= 2.2)
==== BASE ====
Conflicts: python3-bloom
Conflicts3: python-bloom
Copyright-File: LICENSE.txt
Expand Down