Skip to content

Commit

Permalink
🚒 Fixes for gitlab API update
Browse files Browse the repository at this point in the history
  • Loading branch information
guyzmo committed Aug 26, 2019
1 parent 02b95df commit 3bf2052
Show file tree
Hide file tree
Showing 46 changed files with 2,243 additions and 1,261 deletions.
25 changes: 14 additions & 11 deletions git_repo/services/ext/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def connect(self):

def create(self, user, repo, add=False):
try:
group = self.gl.groups.search(user)
data = {'name': repo, 'path': repo}
group = self.gl.groups.list(search=user)
data = {'name': repo}
if group:
data['namespace_id'] = group[0].id
self.gl.projects.create(data=data)
except GitlabCreateError as err:
if json.loads(err.response_body.decode('utf-8'))['message']['name'][0] == 'has already been taken':
if json.loads(err.response_body.decode('utf-8')).get('message', {}).get('name', [None])[0] == 'has already been taken':
raise ResourceExistsError("Project already exists.") from err
else:
raise ResourceError("Unhandled error.") from err
Expand All @@ -60,7 +60,7 @@ def fork(self, user, repo):
try:
return self.gl.projects.get('{}/{}'.format(user, repo)).forks.create({}).path_with_namespace
except GitlabCreateError as err:
if json.loads(err.response_body.decode('utf-8'))['message']['name'][0] == 'has already been taken':
if json.loads(err.response_body.decode('utf-8')).get('message', {}).get('name', [None])[0] == 'has already been taken':
raise ResourceExistsError("Project already exists.") from err
else:
raise ResourceError("Unhandled error: {}".format(err)) from err
Expand All @@ -71,8 +71,8 @@ def delete(self, repo, user=None):
try:
repository = self.gl.projects.get('{}/{}'.format(user, repo))
if repository:
result = self.gl.delete(repository.__class__, repository.id)
if not repository or not result:
result = self.gl.projects.delete(repository.id)
if not repository:
raise ResourceNotFoundError("Cannot delete: repository {}/{} does not exists.".format(user, repo))
except GitlabGetError as err:
if err.response_code == 404:
Expand Down Expand Up @@ -197,7 +197,7 @@ def load_file(fname, path='.'):

data = {
'title': description,
'visibility_level': 0 if secret else 20
'visibility': 'private' if secret else 'public'
}

try:
Expand All @@ -211,12 +211,11 @@ def load_file(fname, path='.'):
namespace = self.username
gist_path = gist_pathes[1]
data.update({
'project_id': '/'.join([namespace, project]),
'code': load_file(gist_path),
'file_name': os.path.basename(gist_path),
}
)
gist = self.gl.project_snippets.create(data)
gist = self.gl.projects.get('/'.join([namespace, project])).snippets.create(data)

elif len(gist_pathes) == 1:
gist_path = gist_pathes[0]
Expand All @@ -231,6 +230,7 @@ def load_file(fname, path='.'):
except GitlabCreateError as err:
if err.response_code == 422:
raise ResourceNotFoundError('Feature not available, please upgrade your gitlab instance.') from err
breakpoint()
raise ResourceError('Cannot create snippet') from err

def gist_delete(self, snippet):
Expand Down Expand Up @@ -302,7 +302,7 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=N
if not title and not description:
raise ArgumentError('Missing message for request creation')

request = self.gl.projects.get(project.id).mergerequests.create(
request = self.gl.projects.get(from_project.id).mergerequests.create(
{
'source_branch': from_branch,
'target_branch': onto_branch,
Expand All @@ -323,14 +323,17 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=N

except GitlabGetError as err:
raise ResourceNotFoundError(err) from err
except GitlabCreateError as err:
raise ResourceError("Creation error: {}".format(err)) from err
except Exception as err:
breakpoint()
raise ResourceError("Unhandled error: {}".format(err)) from err

def request_list(self, user, repo):
project = self.gl.projects.get('/'.join([user, repo]))
yield "{:>3}\t{:<60}\t{:2}"
yield ('id', 'title', 'URL')
for mr in self.gl.projects.get(project.id).mergerequests.list() :
for mr in self.gl.projects.get(project).mergerequests.list() :
yield ( str(mr.iid),
mr.title,
mr.web_url
Expand Down
5 changes: 3 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ def action_request_fetch(self, namespace, repository, request, pull=False, fail=
with self.recorder.use_cassette(self._make_cassette_name()):
with self.mockup_git(namespace, repository):
self.set_mock_popen_commands([
('git init', b'Initialized empty Git repository in /tmp/bar/.git/', b'', 0),
('git remote add all {}'.format(local_slug), b'', b'', 0),
('git remote add {} {}'.format(self.service.name, local_slug), b'', b'', 0),
('git remote get-url --all all', local_slug.encode('utf-8'), b'', 0),
Expand Down Expand Up @@ -762,15 +763,15 @@ def prepare_project_for_test():
if will_record:
self.repository.git.push(self.service.name, 'master')
# create a new branch
new_branch = self.repository.create_head(source_branch, 'HEAD')
new_branch = self.repository.create_head(source_branch or 'master', 'HEAD')
self.repository.head.reference = new_branch
self.repository.head.reset(index=True, working_tree=True)
# make a modification, commit and push it to that branch
with open(os.path.join(self.repository.working_dir, 'second_file'), 'w') as test:
test.write('La meilleure façon de ne pas avancer est de suivre une idée fixe. J.Prévert')
self.repository.git.add('second_file')
self.repository.git.commit(message='Second commit')
self.repository.git.push(service, source_branch)
self.repository.git.push(self.service.name, source_branch or 'master')
else:
import git
self.service._extracts_ref = lambda *a: git.Reference(
Expand Down
Loading

0 comments on commit 3bf2052

Please sign in to comment.