Skip to content

Commit

Permalink
Fix a few remaining methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Jun 11, 2017
1 parent 68f4114 commit 3488c5c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 100 deletions.
12 changes: 11 additions & 1 deletion gitlab/base.py
Expand Up @@ -552,6 +552,7 @@ def __init__(self, manager, attrs):
'_updated_attrs': {},
'_module': importlib.import_module(self.__module__)
})
self.__dict__['_parent_attrs'] = self.manager.parent_attrs

# TODO(gpocentek): manage the creation of new objects from the received
# data (_constructor_types)
Expand All @@ -565,7 +566,10 @@ def __getattr__(self, name):
try:
return self.__dict__['_attrs'][name]
except KeyError:
raise AttributeError(name)
try:
return self.__dict__['_parent_attrs'][name]
except:
raise AttributeError(name)

def __setattr__(self, name, value):
self.__dict__['_updated_attrs'][name] = value
Expand Down Expand Up @@ -660,14 +664,20 @@ def __init__(self, gl, parent=None):
self._parent = parent # for nested managers
self._computed_path = self._compute_path()

@property
def parent_attrs(self):
return self._parent_attrs

def _compute_path(self, path=None):
self._parent_attrs = {}
if path is None:
path = self._path
if self._parent is None or not hasattr(self, '_from_parent_attrs'):
return path

data = {self_attr: getattr(self._parent, parent_attr)
for self_attr, parent_attr in self._from_parent_attrs.items()}
self._parent_attrs = data
return path % data

@property
Expand Down
150 changes: 51 additions & 99 deletions gitlab/v4/objects.py
Expand Up @@ -906,44 +906,35 @@ def cancel_merge_when_pipeline_succeeds(self, **kwargs):
self._update_attrs(server_data)

def closes_issues(self, **kwargs):
"""List issues closed by the MR.
"""List issues that will close on merge."
Returns:
list (ProjectIssue): List of closed issues
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabGetError: If the server fails to perform the request.
list (ProjectIssue): List of issues
"""
# FIXME(gpocentek)
url = ('/projects/%s/merge_requests/%s/closes_issues' %
(self.project_id, self.iid))
return self.gitlab._raw_list(url, ProjectIssue, **kwargs)
path = '%s/%s/closes_issues' % (self.manager.path, self.get_id())
data_list = self.manager.gitlab.http_list(path, **kwargs)
manager = ProjectIssueManager(self.manager.gitlab,
parent=self.manager._parent)
return RESTObjectList(manager, ProjectIssue, data_list)

def commits(self, **kwargs):
"""List the merge request commits.
Returns:
list (ProjectCommit): List of commits
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabListError: If the server fails to perform the request.
"""
# FIXME(gpocentek)
url = ('/projects/%s/merge_requests/%s/commits' %
(self.project_id, self.iid))
return self.gitlab._raw_list(url, ProjectCommit, **kwargs)

path = '%s/%s/commits' % (self.manager.path, self.get_id())
data_list = self.manager.gitlab.http_list(path, **kwargs)
manager = ProjectCommitManager(self.manager.gitlab,
parent=self.manager._parent)
return RESTObjectList(manager, ProjectCommit, data_list)

def changes(self, **kwargs):
"""List the merge request changes.
Returns:
list (dict): List of changes
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabListError: If the server fails to perform the request.
"""
path = '%s/%s/changes' % (self.manager.path, self.get_id())
return self.manager.gitlab.http_get(path, **kwargs)
Expand All @@ -960,14 +951,6 @@ def merge(self, merge_commit_message=None,
branch
merged_when_build_succeeds (bool): Wait for the build to succeed,
then merge
Returns:
ProjectMergeRequest: The updated MR
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabMRForbiddenError: If the user doesn't have permission to
close thr MR
GitlabMRClosedError: If the MR is already closed
"""
path = '%s/%s/merge' % (self.manager.path, self.get_id())
data = {}
Expand Down Expand Up @@ -1002,23 +985,31 @@ class ProjectMilestone(SaveMixin, RESTObject):
_short_print_attr = 'title'

def issues(self, **kwargs):
url = '/projects/%s/milestones/%s/issues' % (self.project_id, self.id)
return self.gitlab._raw_list(url, ProjectIssue, **kwargs)
"""List issues related to this milestone
Returns:
list (ProjectIssue): The list of issues
"""

path = '%s/%s/issues' % (self.manager.path, self.get_id())
data_list = self.manager.gitlab.http_list(path, **kwargs)
manager = ProjectCommitManager(self.manager.gitlab,
parent=self.manager._parent)
# FIXME(gpocentek): the computed manager path is not correct
return RESTObjectList(manager, ProjectIssue, data_list)

def merge_requests(self, **kwargs):
"""List the merge requests related to this milestone
Returns:
list (ProjectMergeRequest): List of merge requests
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabListError: If the server fails to perform the request.
"""
# FIXME(gpocentek)
url = ('/projects/%s/milestones/%s/merge_requests' %
(self.project_id, self.id))
return self.gitlab._raw_list(url, ProjectMergeRequest, **kwargs)
path = '%s/%s/merge_requests' % (self.manager.path, self.get_id())
data_list = self.manager.gitlab.http_list(path, **kwargs)
manager = ProjectCommitManager(self.manager.gitlab,
parent=self.manager._parent)
# FIXME(gpocentek): the computed manager path is not correct
return RESTObjectList(manager, ProjectMergeRequest, data_list)


class ProjectMilestoneManager(RetrieveMixin, CreateMixin, DeleteMixin,
Expand Down Expand Up @@ -1425,20 +1416,29 @@ def repository_tree(self, path='', ref='', **kwargs):
Returns:
str: The json representation of the tree.
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabGetError: If the server fails to perform the request.
"""
path = '/projects/%s/repository/tree' % self.get_id()
gl_path = '/projects/%s/repository/tree' % self.get_id()
query_data = {}
if path:
query_data['path'] = path
if ref:
query_data['ref'] = ref
return self.manager.gitlab.http_get(path, query_data=query_data,
return self.manager.gitlab.http_get(gl_path, query_data=query_data,
**kwargs)

def repository_blob(self, sha, **kwargs):
"""Returns a blob by blob SHA.
Args:
sha(str): ID of the blob
Returns:
str: The blob as json
"""

path = '/projects/%s/repository/blobs/%s' % (self.get_id(), sha)
return self.manager.gitlab.http_get(path, **kwargs)

def repository_raw_blob(self, sha, streamed=False, action=None,
chunk_size=1024, **kwargs):
"""Returns the raw file contents for a blob by blob SHA.
Expand All @@ -1454,13 +1454,9 @@ def repository_raw_blob(self, sha, streamed=False, action=None,
Returns:
str: The blob content
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabGetError: If the server fails to perform the request.
"""
path = '/projects/%s/repository/raw_blobs/%s' % (self.get_id(), sha)
result = self.gitlab._raw_get(path, streamed=streamed, **kwargs)
path = '/projects/%s/repository/blobs/%s/raw' % (self.get_id(), sha)
result = self.manager.gitlab.http_get(path, streamed=streamed, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)

def repository_compare(self, from_, to, **kwargs):
Expand All @@ -1472,10 +1468,6 @@ def repository_compare(self, from_, to, **kwargs):
Returns:
str: The diff
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabGetError: If the server fails to perform the request.
"""
path = '/projects/%s/repository/compare' % self.get_id()
query_data = {'from': from_, 'to': to}
Expand All @@ -1486,11 +1478,7 @@ def repository_contributors(self, **kwargs):
"""Returns a list of contributors for the project.
Returns:
list: The contibutors
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabGetError: If the server fails to perform the request.
list: The contributors
"""
path = '/projects/%s/repository/contributors' % self.get_id()
return self.manager.gitlab.http_get(path, **kwargs)
Expand All @@ -1510,38 +1498,26 @@ def repository_archive(self, sha=None, streamed=False, action=None,
Returns:
str: The binary data of the archive.
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabGetError: If the server fails to perform the request.
"""
path = '/projects/%s/repository/archive' % self.get_id()
query_data = {}
if sha:
query_data['sha'] = sha
result = self.gitlab._raw_get(path, query_data=query_data,
streamed=streamed, **kwargs)
result = self.manager.gitlab.http_get(path, query_data=query_data,
streamed=streamed, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)

def create_fork_relation(self, forked_from_id, **kwargs):
"""Create a forked from/to relation between existing projects.
Args:
forked_from_id (int): The ID of the project that was forked from
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabCreateError: If the server fails to perform the request.
"""
path = '/projects/%s/fork/%s' % (self.get_id(), forked_from_id)
self.manager.gitlab.http_post(path, **kwargs)

def delete_fork_relation(self, **kwargs):
"""Delete a forked relation between existing projects.
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabDeleteError: If the server fails to perform the request.
"""
path = '/projects/%s/fork' % self.get_id()
self.manager.gitlab.http_delete(path, **kwargs)
Expand All @@ -1551,10 +1527,6 @@ def star(self, **kwargs):
Returns:
Project: the updated Project
Raises:
GitlabCreateError: If the action cannot be done
GitlabConnectionError: If the server cannot be reached.
"""
path = '/projects/%s/star' % self.get_id()
server_data = self.manager.gitlab.http_post(path, **kwargs)
Expand All @@ -1565,10 +1537,6 @@ def unstar(self, **kwargs):
Returns:
Project: the updated Project
Raises:
GitlabDeleteError: If the action cannot be done
GitlabConnectionError: If the server cannot be reached.
"""
path = '/projects/%s/unstar' % self.get_id()
server_data = self.manager.gitlab.http_post(path, **kwargs)
Expand All @@ -1579,10 +1547,6 @@ def archive(self, **kwargs):
Returns:
Project: the updated Project
Raises:
GitlabCreateError: If the action cannot be done
GitlabConnectionError: If the server cannot be reached.
"""
path = '/projects/%s/archive' % self.get_id()
server_data = self.manager.gitlab.http_post(path, **kwargs)
Expand All @@ -1593,10 +1557,6 @@ def unarchive(self, **kwargs):
Returns:
Project: the updated Project
Raises:
GitlabDeleteError: If the action cannot be done
GitlabConnectionError: If the server cannot be reached.
"""
path = '/projects/%s/unarchive' % self.get_id()
server_data = self.manager.gitlab.http_post(path, **kwargs)
Expand All @@ -1608,10 +1568,6 @@ def share(self, group_id, group_access, expires_at=None, **kwargs):
Args:
group_id (int): ID of the group.
group_access (int): Access level for the group.
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabCreateError: If the server fails to perform the request.
"""
path = '/projects/%s/share' % self.get_id()
data = {'group_id': group_id,
Expand All @@ -1628,10 +1584,6 @@ def trigger_pipeline(self, ref, token, variables={}, **kwargs):
ref (str): Commit to build; can be a commit SHA, a branch name, ...
token (str): The trigger token
variables (dict): Variables passed to the build script
Raises:
GitlabConnectionError: If the server cannot be reached.
GitlabCreateError: If the server fails to perform the request.
"""
path = '/projects/%s/trigger/pipeline' % self.get_id()
form = {r'variables[%s]' % k: v for k, v in six.iteritems(variables)}
Expand Down

0 comments on commit 3488c5c

Please sign in to comment.