Commit a5ba2c86 authored by dongmx's avatar dongmx Committed by Sander van Harmelen

# 204 add support for GET /users/:user_id/projects (#234)

parent e6f4b9d4
...@@ -197,6 +197,26 @@ func (s *ProjectsService) ListProjects(opt *ListProjectsOptions, options ...Opti ...@@ -197,6 +197,26 @@ func (s *ProjectsService) ListProjects(opt *ListProjectsOptions, options ...Opti
return p, resp, err return p, resp, err
} }
// ListUserProjects gets a list of projects for the given user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/projects.html#list-user-projects
func (s *ProjectsService) ListUserProjects(user int, opt *ListProjectsOptions, options ...OptionFunc) ([]*Project, *Response, error) {
u := fmt.Sprintf("users/%d/projects", user)
req, err := s.client.NewRequest("GET", 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
}
// GetProject gets a specific project, identified by project ID or // GetProject gets a specific project, identified by project ID or
// NAMESPACE/PROJECT_NAME, which is owned by the authenticated user. // NAMESPACE/PROJECT_NAME, which is owned by the authenticated user.
// //
......
...@@ -40,6 +40,36 @@ func TestListProjects(t *testing.T) { ...@@ -40,6 +40,36 @@ func TestListProjects(t *testing.T) {
} }
} }
func TestListUserProjects(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/users/1/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
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.ListUserProjects(1, opt)
if err != nil {
t.Errorf("Projects.ListUserProjects returned error: %v", err)
}
want := []*Project{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(want, projects) {
t.Errorf("Projects.ListUserProjects returned %+v, want %+v", projects, want)
}
}
func TestListOwnedProjects(t *testing.T) { func TestListOwnedProjects(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment