From e4a1ec7d5b483adb4d3e94380f476a718a2d1894 Mon Sep 17 00:00:00 2001 From: Lucas Stephens Date: Mon, 11 Sep 2023 21:46:06 -0500 Subject: [PATCH 1/2] add method to list user's contributed projects --- projects.go | 25 +++++++++++++++++++++++++ projects_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/projects.go b/projects.go index 6d4cfe77a..19d889d53 100644 --- a/projects.go +++ b/projects.go @@ -390,6 +390,31 @@ func (s *ProjectsService) ListUserProjects(uid interface{}, opt *ListProjectsOpt return p, resp, nil } +// ListUserProjects gets a list of visible projects a given user has contributed to. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/projects.html#list-projects-a-user-has-contributed-to +func (s *ProjectsService) ListUserContributedProjects(uid interface{}, opt *ListProjectsOptions, options ...RequestOptionFunc) ([]*Project, *Response, error) { + user, err := parseID(uid) + if err != nil { + return nil, nil, err + } + u := fmt.Sprintf("users/%s/contributed_projects", user) + + req, err := s.client.NewRequest(http.MethodGet, u, opt, options) + if err != nil { + return nil, nil, err + } + + var p []*Project + resp, err := s.client.Do(req, &p) + if err != nil { + return nil, resp, err + } + + return p, resp, nil +} + // ListUserStarredProjects gets a list of projects starred by the given user. // // GitLab API docs: diff --git a/projects_test.go b/projects_test.go index 7500357a4..895791f21 100644 --- a/projects_test.go +++ b/projects_test.go @@ -88,6 +88,35 @@ func TestListUserProjects(t *testing.T) { } } +func TestListUserContributedProjects(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/users/1/contributed_projects", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + fmt.Fprint(w, `[{"id":1},{"id":2}]`) + }) + + opt := &ListProjectsOptions{ + ListOptions: ListOptions{2, 3}, + Archived: Bool(true), + OrderBy: String("name"), + Sort: String("asc"), + Search: String("query"), + Simple: Bool(true), + Visibility: Visibility(PublicVisibility), + } + + projects, _, err := client.Projects.ListUserContributedProjects(1, opt) + if err != nil { + t.Errorf("Projects.ListUserContributedProjects returned error: %v", err) + } + + want := []*Project{{ID: 1}, {ID: 2}} + if !reflect.DeepEqual(want, projects) { + t.Errorf("Projects.ListUserContributedProjects returned %+v, want %+v", projects, want) + } +} + func TestListUserStarredProjects(t *testing.T) { mux, client := setup(t) From a949a37c8d2ce7f14c8871bb356f6022583d2d29 Mon Sep 17 00:00:00 2001 From: Lucas Stephens Date: Tue, 12 Sep 2023 21:15:37 -0500 Subject: [PATCH 2/2] update comment --- projects.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects.go b/projects.go index 19d889d53..f211467bc 100644 --- a/projects.go +++ b/projects.go @@ -390,7 +390,7 @@ func (s *ProjectsService) ListUserProjects(uid interface{}, opt *ListProjectsOpt return p, resp, nil } -// ListUserProjects gets a list of visible projects a given user has contributed to. +// ListUserContributedProjects gets a list of visible projects a given user has contributed to. // // GitLab API docs: // https://docs.gitlab.com/ee/api/projects.html#list-projects-a-user-has-contributed-to