Repository: xanzy/go-gitlab
Pull request #1332 (2021-12-31 05:14:07)

Add SDK method for listing a user's starred projects
Fix for https://github.com/xanzy/go-gitlab/issues/1331


Patch:
diff --git a/projects.go b/projects.go
index cfc31d9..877a9d3 100644
--- a/projects.go
+++ b/projects.go
@@ -343,6 +343,31 @@ func (s *ProjectsService) ListUserProjects(uid interface{}, opt *ListProjectsOpt
 	return p, resp, err
 }
 
+// ListUserStarredProjects gets a list of projects starred by the given user.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/projects.html#list-projects-starred-by-a-user
+func (s *ProjectsService) ListUserStarredProjects(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/starred_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, err
+}
+
 // ProjectUser represents a GitLab project user.
 type ProjectUser struct {
 	ID        int    `json:"id"`


Test patch:
diff --git a/projects_test.go b/projects_test.go
index c4a091d..5ab4e6d 100644
--- a/projects_test.go
+++ b/projects_test.go
@@ -87,6 +87,36 @@ func TestListUserProjects(t *testing.T) {
 	}
 }
 
+func TestListUserStarredProjects(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/users/1/starred_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.ListUserStarredProjects(1, opt)
+	if err != nil {
+		t.Errorf("Projects.ListUserStarredProjects returned error: %v", err)
+	}
+
+	want := []*Project{{ID: 1}, {ID: 2}}
+	if !reflect.DeepEqual(want, projects) {
+		t.Errorf("Projects.ListUserStarredProjects returned %+v, want %+v", projects, want)
+	}
+}
+
 func TestListProjectsUsersByID(t *testing.T) {
 	mux, server, client := setup(t)
 	defer teardown(server)
diff --git a/runners_test.go b/runners_test.go
index 455934a..606c586 100644
--- a/runners_test.go
+++ b/runners_test.go
@@ -68,7 +68,7 @@ func TestListRunnersJobs(t *testing.T) {
 	}
 
 	want := []*Job{
-		&Job{
+		{
 			ID:             1,
 			Status:         "failed",
 			Stage:          "test",
diff --git a/users_test.go b/users_test.go
index 21aa6be..6f98e47 100644
--- a/users_test.go
+++ b/users_test.go
@@ -113,7 +113,7 @@ func TestGetUserAdmin(t *testing.T) {
 		ConfirmedAt:      Time(time.Date(2012, time.May, 23, 9, 05, 22, 0, time.UTC)),
 		TwoFactorEnabled: true,
 		Note:             "DMCA Request: 2018-11-05 | DMCA Violation | Abuse | https://gitlab.zendesk.com/agent/tickets/123",
-		Identities:       []*UserIdentity{&UserIdentity{Provider: "github", ExternUID: "2435223452345"}},
+		Identities:       []*UserIdentity{{Provider: "github", ExternUID: "2435223452345"}},
 	}
 	require.Equal(t, want, user)
 }