Skip to content

fix get projects limit #41

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 3 additions & 1 deletion gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
func TestResourceUrl(t *testing.T) {
gitlab := NewGitlab("http://base_url/", "api_path", "token")

assert.Equal(t, gitlab.ResourceUrl(projects_url, nil), "http://base_url/api_path/projects?private_token=token")
assert.Equal(t, gitlab.ResourceUrl(
projects_url, map[string]string{":page": "1", ":per_page": "20"}),
"http://base_url/api_path/projects?page=1&per_page=20&private_token=token")
assert.Equal(t, gitlab.ResourceUrl(project_url, map[string]string{":id": "123"}), "http://base_url/api_path/projects/123?private_token=token")
}
62 changes: 48 additions & 14 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
)

const (
projects_url = "/projects" // Get a list of projects owned by the authenticated user
projects_all = "/projects/all" // Get a list of all GitLab projects (admin only)
projects_search_url = "/projects/search/:query" // Search for projects by name
project_url = "/projects/:id" // Get a specific project, identified by project ID or NAME
project_url_events = "/projects/:id/events" // Get project events
project_url_branches = "/projects/:id/repository/branches" // Lists all branches of a project
project_url_members = "/projects/:id/members" // List project team members
project_url_member = "/projects/:id/members/:user_id" // Get project team member
projects_url = "/projects?page=:page&per_page=:per_page" // Get a list of projects owned by the authenticated user
projects_all = "/projects/all?page=:page&per_page=:per_page" // Get a list of all GitLab projects (admin only)
projects_search_url = "/projects/search/:query" // Search for projects by name
project_url = "/projects/:id" // Get a specific project, identified by project ID or NAME
project_url_events = "/projects/:id/events" // Get project events
project_url_branches = "/projects/:id/repository/branches" // Lists all branches of a project
project_url_members = "/projects/:id/members" // List project team members
project_url_member = "/projects/:id/members/:user_id" // Get project team member
max_page_size = 20
)

type Member struct {
Expand Down Expand Up @@ -58,17 +59,50 @@ type Project struct {
SharedRunners bool `json:"shared_runners_enabled"`
}

func append(slice []*Project, elements ...*Project) []*Project {
n := len(slice)
total := len(slice) + len(elements)
if total > cap(slice) {
// Reallocate. Grow to 1.5 times the new size, so we can still grow.
newSize := total*3/2 + 1
newSlice := make([]*Project, total, newSize)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[:total]
copy(slice[n:], elements)
return slice
}

func projects(u string, g *Gitlab) ([]*Project, error) {
url := g.ResourceUrl(u, nil)

var projects []*Project

contents, err := g.buildAndExecRequest("GET", url, nil)
if err == nil {
err = json.Unmarshal(contents, &projects)
var page int64 = 1
for {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pagination options should be exposed in func (g *Gitlab) Projects() and func (g *Gitlab) AllProjects(), I think it's more useful and reusable, otherwise we'll have no way to customize max_page_size (which prehaps should be named per_page).
Recursively fetching all the pages should not be part of the default logic, perhaps another dedicated function.
I'm aware this lib really miss pagination capabilities, and I'm really thankful for your contribution, but it has to be addressed in a more generic way IMO.

Copy link
Author

@ccl0326 ccl0326 Sep 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plouc yep, you are right, i think i can rewrite a pr for missing pagination capabilities.
but this pr can not be forward compatibility.If you can design a good interface for it. I can finish it quickly.

url := g.ResourceUrl(
u,
map[string]string{
":page": strconv.FormatInt(page, 10),
":per_page": strconv.FormatInt(max_page_size, 10)})

var pageProjects []*Project
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be projectsPage


contents, err := g.buildAndExecRequest("GET", url, nil)
if err == nil {
err = json.Unmarshal(contents, &pageProjects)
if err == nil {
projects = append(projects, pageProjects...)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just using default append ?

page++
if len(pageProjects) < max_page_size {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll have an extra call if the last page contains extactly max_page_size items.

break
}
continue
}
}
return projects, err
}

return projects, err
return projects, nil
}

/*
Expand Down