Skip to content

Commit 1de6b7e

Browse files
author
Gauvain Pocentek
committed
implement star/unstar for projects
1 parent 24c283f commit 1de6b7e

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

gitlab/cli.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@
5252
gitlab.ProjectMilestone: {'issues': {'required': ['id', 'project-id']}},
5353
gitlab.Project: {'search': {'required': ['query']},
5454
'owned': {},
55-
'all': {}},
55+
'all': {},
56+
'starred': {},
57+
'star': {'required': ['id']},
58+
'unstar': {'required': ['id']}},
5659
gitlab.User: {'block': {'required': ['id']},
5760
'unblock': {'required': ['id']},
5861
'search': {'required': ['query']},
@@ -170,12 +173,32 @@ def do_project_all(self, cls, gl, what, args):
170173
except Exception as e:
171174
_die("Impossible to list all projects (%s)" % str(e))
172175

176+
def do_project_starred(self, cls, gl, what, args):
177+
try:
178+
return gl.projects.starred()
179+
except Exception as e:
180+
_die("Impossible to list starred projects (%s)" % str(e))
181+
173182
def do_project_owned(self, cls, gl, what, args):
174183
try:
175184
return gl.projects.owned()
176185
except Exception as e:
177186
_die("Impossible to list owned projects (%s)" % str(e))
178187

188+
def do_project_star(self, cls, gl, what, args):
189+
try:
190+
o = self.do_get(cls, gl, what, args)
191+
o.star()
192+
except Exception as e:
193+
_die("Impossible to star project (%s)" % str(e))
194+
195+
def do_project_unstar(self, cls, gl, what, args):
196+
try:
197+
o = self.do_get(cls, gl, what, args)
198+
o.unstar()
199+
except Exception as e:
200+
_die("Impossible to unstar project (%s)" % str(e))
201+
179202
def do_user_block(self, cls, gl, what, args):
180203
try:
181204
o = self.do_get(cls, gl, what, args)

gitlab/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ def raise_error_from_response(response, error, expected_code=200):
116116
class to raise. Should be inherited from GitLabError
117117
"""
118118

119-
if expected_code == response.status_code:
119+
if isinstance(expected_code, int):
120+
expected_codes = [expected_code]
121+
else:
122+
expected_codes = expected_code
123+
124+
if response.status_code in expected_codes:
120125
return
121126

122127
try:

gitlab/objects.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,34 @@ def delete_fork_relation(self):
16511651
r = self.gitlab._raw_delete(url)
16521652
raise_error_from_response(r, GitlabDeleteError)
16531653

1654+
def star(self):
1655+
"""Star a project.
1656+
1657+
Returns:
1658+
Project: the updated Project
1659+
1660+
Raises:
1661+
GitlabConnectionError: If the server cannot be reached.
1662+
"""
1663+
url = "/projects/%s/star" % self.id
1664+
r = self.gitlab._raw_post(url)
1665+
raise_error_from_response(r, GitlabGetError, [201, 304])
1666+
return Project(self.gitlab, r.json()) if r.status_code == 201 else self
1667+
1668+
def unstar(self):
1669+
"""Unstar a project.
1670+
1671+
Returns:
1672+
Project: the updated Project
1673+
1674+
Raises:
1675+
GitlabConnectionError: If the server cannot be reached.
1676+
"""
1677+
url = "/projects/%s/star" % self.id
1678+
r = self.gitlab._raw_delete(url)
1679+
raise_error_from_response(r, GitlabDeleteError, [200, 304])
1680+
return Project(self.gitlab, r.json()) if r.status_code == 200 else self
1681+
16541682

16551683
class TeamMember(GitlabObject):
16561684
_url = '/user_teams/%(team_id)s/members'
@@ -1727,6 +1755,18 @@ def owned(self, **kwargs):
17271755
"""
17281756
return self.gitlab._raw_list("/projects/owned", Project, **kwargs)
17291757

1758+
def starred(self, **kwargs):
1759+
"""List starred projects.
1760+
1761+
Args:
1762+
all (bool): If True, return all the items, without pagination
1763+
**kwargs: Additional arguments to send to GitLab.
1764+
1765+
Returns:
1766+
list(gitlab.Gitlab.Project): The list of starred projects.
1767+
"""
1768+
return self.gitlab._raw_list("/projects/starred", Project, **kwargs)
1769+
17301770

17311771
class UserProjectManager(BaseManager):
17321772
obj_cls = UserProject

tools/python_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,9 @@
228228
mr.merge()
229229
except gitlab.GitlabMRClosedError:
230230
pass
231+
232+
# stars
233+
admin_project = admin_project.star()
234+
assert(admin_project.star_count == 1)
235+
admin_project = admin_project.unstar()
236+
assert(admin_project.star_count == 0)

0 commit comments

Comments
 (0)