Skip to content

feat(api): add listing user contributed projects #3179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/gl_objects/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ Get the followings of a user::

user.following_users.list(get_all=True)

List a user's contributed projects::

user.contributed_projects.list(get_all=True)

List a user's starred projects::

user.starred_projects.list(get_all=True)
Expand Down
14 changes: 14 additions & 0 deletions gitlab/v4/objects/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
"UserMembershipManager",
"UserProject",
"UserProjectManager",
"UserContributedProject",
"UserContributedProjectManager",
]


Expand Down Expand Up @@ -182,6 +184,7 @@ class User(SaveMixin, ObjectDeleteMixin, RESTObject):
memberships: UserMembershipManager
personal_access_tokens: UserPersonalAccessTokenManager
projects: UserProjectManager
contributed_projects: UserContributedProjectManager
starred_projects: StarredProjectManager
status: UserStatusManager

Expand Down Expand Up @@ -665,6 +668,17 @@ def list(
return super().list(path=path, iterator=iterator, **kwargs)


class UserContributedProject(RESTObject):
_id_attr = "id"
_repr_attr = "path_with_namespace"


class UserContributedProjectManager(ListMixin[UserContributedProject]):
_path = "/users/{user_id}/contributed_projects"
_obj_cls = UserContributedProject
_from_parent_attrs = {"user_id": "id"}


class StarredProject(RESTObject):
pass

Expand Down
27 changes: 26 additions & 1 deletion tests/unit/objects/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import pytest
import responses

from gitlab.v4.objects import StarredProject, User, UserMembership, UserStatus
from gitlab.v4.objects import (
StarredProject,
User,
UserContributedProject,
UserMembership,
UserStatus,
)

from .test_projects import project_content

Expand Down Expand Up @@ -242,6 +248,19 @@ def resp_starred_projects():
yield rsps


@pytest.fixture
def resp_contributed_projects():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/users/1/contributed_projects",
json=[project_content],
content_type="application/json",
status=200,
)
yield rsps


@pytest.fixture
def resp_runner_create():
with responses.RequestsMock() as rsps:
Expand Down Expand Up @@ -314,6 +333,12 @@ def test_list_followers(user, resp_followers_following):
assert followings[1].id == 4


def test_list_contributed_projects(user, resp_contributed_projects):
projects = user.contributed_projects.list()
assert isinstance(projects[0], UserContributedProject)
assert projects[0].id == project_content["id"]


def test_list_starred_projects(user, resp_starred_projects):
projects = user.starred_projects.list()
assert isinstance(projects[0], StarredProject)
Expand Down