Commit 85c5274e authored by Mikhail Grigorev's avatar Mikhail Grigorev Committed by Sander van Harmelen

Refactor ListProject calls to support the v4 API (#170)

* Fixed GitLab APIv4 support
Signed-off-by: 's avatarMikhail Grigorev <sleuthhound@gmail.com>

* Fixed test for APIv4
Signed-off-by: 's avatarMikhail Grigorev <sleuthhound@gmail.com>

* Fixed Statistics options
Signed-off-by: 's avatarMikhail Grigorev <sleuthhound@gmail.com>
parent 36fc720f
......@@ -33,7 +33,7 @@ import (
)
const (
libraryVersion = "0.1.1"
libraryVersion = "0.2.0"
defaultBaseURL = "https://gitlab.com/api/v3/"
userAgent = "go-gitlab/" + libraryVersion
)
......
......@@ -157,6 +157,9 @@ type ListProjectsOptions struct {
Simple *bool `url:"simple,omitempty" json:"simple,omitempty"`
Visibility *VisibilityLevelValue `url:"visibility,omitempty" json:"visibility,omitempty"`
Statistics *bool `url:"statistics,omitempty" json:"statistics,omitempty"`
Owned *bool `url:"owned,omitempty" json:"owned,omitempty"`
Membership *bool `url:"membership,omitempty" json:"membership,omitempty"`
Starred *bool `url:"starred,omitempty" json:"starred,omitempty"`
}
// ListProjects gets a list of projects accessible by the authenticated user.
......@@ -177,65 +180,6 @@ func (s *ProjectsService) ListProjects(opt *ListProjectsOptions, options ...Opti
return p, resp, err
}
// ListOwnedProjects gets a list of projects which are owned by the
// authenticated user.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/projects.html#list-owned-projects
func (s *ProjectsService) ListOwnedProjects(opt *ListProjectsOptions, options ...OptionFunc) ([]*Project, *Response, error) {
req, err := s.client.NewRequest("GET", "projects/owned", 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
}
// ListStarredProjects gets a list of projects which are starred by the
// authenticated user.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/projects.html#list-starred-projects
func (s *ProjectsService) ListStarredProjects(opt *ListProjectsOptions, options ...OptionFunc) ([]*Project, *Response, error) {
req, err := s.client.NewRequest("GET", "projects/starred", 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
}
// ListAllProjects gets a list of all GitLab projects (admin only).
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/projects.html#list-all-projects
func (s *ProjectsService) ListAllProjects(opt *ListProjectsOptions, options ...OptionFunc) ([]*Project, *Response, error) {
req, err := s.client.NewRequest("GET", "projects/all", 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
// NAMESPACE/PROJECT_NAME, which is owned by the authenticated user.
//
......
......@@ -22,12 +22,15 @@ func TestListProjects(t *testing.T) {
"search": "query",
"simple": "true",
"visibility": "public",
"statistics": "true",
"statistics": "false",
"owned": "false",
"membership": "false",
"starred": "false",
})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
opt := &ListProjectsOptions{ListOptions{2, 3}, Bool(true), String("name"), String("asc"), String("query"), Bool(true), VisibilityLevel(PublicVisibility), Bool(true)}
opt := &ListProjectsOptions{ListOptions{2, 3}, Bool(true), String("name"), String("asc"), String("query"), Bool(true), VisibilityLevel(PublicVisibility), Bool(false), Bool(false), Bool(false), Bool(false)}
projects, _, err := client.Projects.ListProjects(opt)
if err != nil {
......@@ -44,7 +47,7 @@ func TestListOwnedProjects(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/owned", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
......@@ -56,12 +59,15 @@ func TestListOwnedProjects(t *testing.T) {
"simple": "true",
"visibility": "public",
"statistics": "false",
"owned": "true",
"membership": "false",
"starred": "false",
})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
opt := &ListProjectsOptions{ListOptions{2, 3}, Bool(true), String("name"), String("asc"), String("query"), Bool(true), VisibilityLevel(PublicVisibility), Bool(false)}
projects, _, err := client.Projects.ListOwnedProjects(opt)
opt := &ListProjectsOptions{ListOptions{2, 3}, Bool(true), String("name"), String("asc"), String("query"), Bool(true), VisibilityLevel(PublicVisibility), Bool(false), Bool(true), Bool(false), Bool(false)}
projects, _, err := client.Projects.ListProjects(opt)
if err != nil {
t.Errorf("Projects.ListOwnedProjects returned error: %v", err)
......@@ -77,7 +83,7 @@ func TestListStarredProjects(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/starred", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
......@@ -89,12 +95,15 @@ func TestListStarredProjects(t *testing.T) {
"simple": "true",
"visibility": "public",
"statistics": "false",
"owned": "false",
"membership": "false",
"starred": "true",
})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
opt := &ListProjectsOptions{ListOptions{2, 3}, Bool(true), String("name"), String("asc"), String("query"), Bool(true), VisibilityLevel(PublicVisibility), Bool(false)}
projects, _, err := client.Projects.ListStarredProjects(opt)
opt := &ListProjectsOptions{ListOptions{2, 3}, Bool(true), String("name"), String("asc"), String("query"), Bool(true), VisibilityLevel(PublicVisibility), Bool(false), Bool(false), Bool(false), Bool(true)}
projects, _, err := client.Projects.ListProjects(opt)
if err != nil {
t.Errorf("Projects.ListStarredProjects returned error: %v", err)
......@@ -106,39 +115,6 @@ func TestListStarredProjects(t *testing.T) {
}
}
func TestListAllProjects(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/all", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
"per_page": "3",
"archived": "true",
"order_by": "name",
"sort": "asc",
"search": "query",
"simple": "true",
"visibility": "public",
"statistics": "false",
})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
opt := &ListProjectsOptions{ListOptions{2, 3}, Bool(true), String("name"), String("asc"), String("query"), Bool(true), VisibilityLevel(PublicVisibility), Bool(false)}
projects, _, err := client.Projects.ListAllProjects(opt)
if err != nil {
t.Errorf("Projects.ListAllProjects returned error: %v", err)
}
want := []*Project{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(want, projects) {
t.Errorf("Projects.ListAllProjects returned %+v, want %+v", projects, want)
}
}
func TestGetProject_byID(t *testing.T) {
mux, server, client := setup()
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