Skip to content

Commit 47a5606

Browse files
nejchJohnVillalovos
authored andcommitted
feat(objects): list starred projects of a user
1 parent 79785f0 commit 47a5606

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

docs/gl_objects/projects.rst

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ Results can also be sorted using the following parameters:
5555
# Search projects
5656
projects = gl.projects.list(search='keyword')
5757

58+
.. note::
59+
60+
To list the starred projects of another user, see the
61+
:ref:`Users API docs <users_examples>`.
62+
5863
.. note::
5964

6065
Fetching a list of projects, doesn't include all attributes of all projects.

docs/gl_objects/users.rst

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _users_examples:
2+
13
######################
24
Users and current user
35
######################
@@ -19,7 +21,10 @@ References
1921
+ :class:`gitlab.v4.objects.UserManager`
2022
+ :attr:`gitlab.Gitlab.users`
2123

22-
* GitLab API: https://docs.gitlab.com/ce/api/users.html
24+
* GitLab API:
25+
26+
+ https://docs.gitlab.com/ce/api/users.html
27+
+ https://docs.gitlab.com/ee/api/projects.html#list-projects-starred-by-a-user
2328

2429
Examples
2530
--------
@@ -97,6 +102,9 @@ Get the followings of a user
97102

98103
user.following_users.list()
99104

105+
List a user's starred projects
106+
107+
user.starred_projects.list()
100108

101109
User custom attributes
102110
======================

gitlab/v4/objects/users.py

+34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
GitLab API:
3+
https://docs.gitlab.com/ee/api/users.html
4+
https://docs.gitlab.com/ee/api/projects.html#list-projects-starred-by-a-user
5+
"""
16
from typing import Any, cast, Dict, List, Union
27

38
import requests
@@ -38,6 +43,8 @@
3843
"UserManager",
3944
"ProjectUser",
4045
"ProjectUserManager",
46+
"StarredProject",
47+
"StarredProjectManager",
4148
"UserEmail",
4249
"UserEmailManager",
4350
"UserActivities",
@@ -129,6 +136,7 @@ class User(SaveMixin, ObjectDeleteMixin, RESTObject):
129136
memberships: "UserMembershipManager"
130137
personal_access_tokens: UserPersonalAccessTokenManager
131138
projects: "UserProjectManager"
139+
starred_projects: "StarredProjectManager"
132140
status: "UserStatusManager"
133141

134142
@cli.register_custom_action("User")
@@ -502,6 +510,32 @@ def list(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
502510
return ListMixin.list(self, path=path, **kwargs)
503511

504512

513+
class StarredProject(RESTObject):
514+
pass
515+
516+
517+
class StarredProjectManager(ListMixin, RESTManager):
518+
_path = "/users/%(user_id)s/starred_projects"
519+
_obj_cls = StarredProject
520+
_from_parent_attrs = {"user_id": "id"}
521+
_list_filters = (
522+
"archived",
523+
"membership",
524+
"min_access_level",
525+
"order_by",
526+
"owned",
527+
"search",
528+
"simple",
529+
"sort",
530+
"starred",
531+
"statistics",
532+
"visibility",
533+
"with_custom_attributes",
534+
"with_issues_enabled",
535+
"with_merge_requests_enabled",
536+
)
537+
538+
505539
class UserFollowersManager(ListMixin, RESTManager):
506540
_path = "/users/%(user_id)s/followers"
507541
_obj_cls = User

tests/unit/objects/test_users.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
"""
2-
GitLab API: https://docs.gitlab.com/ce/api/users.html
2+
GitLab API:
3+
https://docs.gitlab.com/ce/api/users.html
4+
https://docs.gitlab.com/ee/api/projects.html#list-projects-starred-by-a-user
35
"""
46
import pytest
57
import responses
68

7-
from gitlab.v4.objects import User, UserMembership, UserStatus
9+
from gitlab.v4.objects import StarredProject, User, UserMembership, UserStatus
10+
11+
from .test_projects import project_content
812

913

1014
@pytest.fixture
@@ -174,6 +178,19 @@ def resp_followers_following():
174178
yield rsps
175179

176180

181+
@pytest.fixture
182+
def resp_starred_projects():
183+
with responses.RequestsMock() as rsps:
184+
rsps.add(
185+
method=responses.GET,
186+
url="http://localhost/api/v4/users/1/starred_projects",
187+
json=[project_content],
188+
content_type="application/json",
189+
status=200,
190+
)
191+
yield rsps
192+
193+
177194
def test_get_user(gl, resp_get_user):
178195
user = gl.users.get(1)
179196
assert isinstance(user, User)
@@ -215,3 +232,9 @@ def test_list_followers(user, resp_followers_following):
215232
assert followers[0].id == 2
216233
assert isinstance(followings[0], User)
217234
assert followings[1].id == 4
235+
236+
237+
def test_list_starred_projects(user, resp_starred_projects):
238+
projects = user.starred_projects.list()
239+
assert isinstance(projects[0], StarredProject)
240+
assert projects[0].id == project_content["id"]

0 commit comments

Comments
 (0)