summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/cli.py25
-rw-r--r--gitlab/exceptions.py7
-rw-r--r--gitlab/objects.py40
-rw-r--r--tools/python_test.py6
4 files changed, 76 insertions, 2 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py
index cd64ada..02dac8e 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -52,7 +52,10 @@ EXTRA_ACTIONS = {
gitlab.ProjectMilestone: {'issues': {'required': ['id', 'project-id']}},
gitlab.Project: {'search': {'required': ['query']},
'owned': {},
- 'all': {}},
+ 'all': {},
+ 'starred': {},
+ 'star': {'required': ['id']},
+ 'unstar': {'required': ['id']}},
gitlab.User: {'block': {'required': ['id']},
'unblock': {'required': ['id']},
'search': {'required': ['query']},
@@ -170,12 +173,32 @@ class GitlabCLI(object):
except Exception as e:
_die("Impossible to list all projects (%s)" % str(e))
+ def do_project_starred(self, cls, gl, what, args):
+ try:
+ return gl.projects.starred()
+ except Exception as e:
+ _die("Impossible to list starred projects (%s)" % str(e))
+
def do_project_owned(self, cls, gl, what, args):
try:
return gl.projects.owned()
except Exception as e:
_die("Impossible to list owned projects (%s)" % str(e))
+ def do_project_star(self, cls, gl, what, args):
+ try:
+ o = self.do_get(cls, gl, what, args)
+ o.star()
+ except Exception as e:
+ _die("Impossible to star project (%s)" % str(e))
+
+ def do_project_unstar(self, cls, gl, what, args):
+ try:
+ o = self.do_get(cls, gl, what, args)
+ o.unstar()
+ except Exception as e:
+ _die("Impossible to unstar project (%s)" % str(e))
+
def do_user_block(self, cls, gl, what, args):
try:
o = self.do_get(cls, gl, what, args)
diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py
index 8190696..49a3728 100644
--- a/gitlab/exceptions.py
+++ b/gitlab/exceptions.py
@@ -116,7 +116,12 @@ def raise_error_from_response(response, error, expected_code=200):
class to raise. Should be inherited from GitLabError
"""
- if expected_code == response.status_code:
+ if isinstance(expected_code, int):
+ expected_codes = [expected_code]
+ else:
+ expected_codes = expected_code
+
+ if response.status_code in expected_codes:
return
try:
diff --git a/gitlab/objects.py b/gitlab/objects.py
index 94bf1d9..139a92e 100644
--- a/gitlab/objects.py
+++ b/gitlab/objects.py
@@ -1651,6 +1651,34 @@ class Project(GitlabObject):
r = self.gitlab._raw_delete(url)
raise_error_from_response(r, GitlabDeleteError)
+ def star(self):
+ """Star a project.
+
+ Returns:
+ Project: the updated Project
+
+ Raises:
+ GitlabConnectionError: If the server cannot be reached.
+ """
+ url = "/projects/%s/star" % self.id
+ r = self.gitlab._raw_post(url)
+ raise_error_from_response(r, GitlabGetError, [201, 304])
+ return Project(self.gitlab, r.json()) if r.status_code == 201 else self
+
+ def unstar(self):
+ """Unstar a project.
+
+ Returns:
+ Project: the updated Project
+
+ Raises:
+ GitlabConnectionError: If the server cannot be reached.
+ """
+ url = "/projects/%s/star" % self.id
+ r = self.gitlab._raw_delete(url)
+ raise_error_from_response(r, GitlabDeleteError, [200, 304])
+ return Project(self.gitlab, r.json()) if r.status_code == 200 else self
+
class TeamMember(GitlabObject):
_url = '/user_teams/%(team_id)s/members'
@@ -1727,6 +1755,18 @@ class ProjectManager(BaseManager):
"""
return self.gitlab._raw_list("/projects/owned", Project, **kwargs)
+ def starred(self, **kwargs):
+ """List starred projects.
+
+ Args:
+ all (bool): If True, return all the items, without pagination
+ **kwargs: Additional arguments to send to GitLab.
+
+ Returns:
+ list(gitlab.Gitlab.Project): The list of starred projects.
+ """
+ return self.gitlab._raw_list("/projects/starred", Project, **kwargs)
+
class UserProjectManager(BaseManager):
obj_cls = UserProject
diff --git a/tools/python_test.py b/tools/python_test.py
index d071435..d09d24b 100644
--- a/tools/python_test.py
+++ b/tools/python_test.py
@@ -228,3 +228,9 @@ try:
mr.merge()
except gitlab.GitlabMRClosedError:
pass
+
+# stars
+admin_project = admin_project.star()
+assert(admin_project.star_count == 1)
+admin_project = admin_project.unstar()
+assert(admin_project.star_count == 0)