-
Notifications
You must be signed in to change notification settings - Fork 74
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 { | ||
url := g.ResourceUrl( | ||
u, | ||
map[string]string{ | ||
":page": strconv.FormatInt(page, 10), | ||
":per_page": strconv.FormatInt(max_page_size, 10)}) | ||
|
||
var pageProjects []*Project | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
|
||
contents, err := g.buildAndExecRequest("GET", url, nil) | ||
if err == nil { | ||
err = json.Unmarshal(contents, &pageProjects) | ||
if err == nil { | ||
projects = append(projects, pageProjects...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just using default |
||
page++ | ||
if len(pageProjects) < max_page_size { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you'll have an extra call if the last page contains extactly |
||
break | ||
} | ||
continue | ||
} | ||
} | ||
return projects, err | ||
} | ||
|
||
return projects, err | ||
return projects, nil | ||
} | ||
|
||
/* | ||
|
There was a problem hiding this comment.
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()
andfunc (g *Gitlab) AllProjects()
, I think it's more useful and reusable, otherwise we'll have no way to customizemax_page_size
(which prehaps should be namedper_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.
There was a problem hiding this comment.
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.