Skip to content

Fix handling of Skip and Start parameters for pagination #98

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 5 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
8 changes: 2 additions & 6 deletions changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,12 @@ type QueryOptions struct {
// Clients are allowed to specify more than one query by setting the q parameter multiple times.
// In this case the result is an array of arrays, one per query in the same order the queries were given in.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/user-search.html#_search_operators
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/user-search.html#search-operators
Query []string `url:"q,omitempty"`

// The n parameter can be used to limit the returned results.
// If the n query parameter is supplied and additional changes exist that match the query beyond the end, the last change object has a _more_changes: true JSON field set.
Limit int `url:"n,omitempty"`

// The S or start query parameter can be supplied to skip a number of changes from the list.
Start int `url:"start,omitempty"`
}

// QueryChangeOptions specifies the parameters to the ChangesService.QueryChanges.
Expand All @@ -521,8 +518,7 @@ type QueryOptions struct {
type QueryChangeOptions struct {
QueryOptions

// The S or start query parameter can be supplied to skip a number of changes from the list.
Skip int `url:"S,omitempty"`
// The `S` or `start` query parameter can be supplied to skip a number of changes from the list.
Start int `url:"start,omitempty"`

ChangeOptions
Expand Down
51 changes: 51 additions & 0 deletions examples/list_branches_with_pagination/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"fmt"

"github.com/andygrunwald/go-gerrit"
)

func main() {
instance := "https://gerrit-review.googlesource.com/"
client, err := gerrit.NewClient(instance, nil)
if err != nil {
panic(err)
}

projectName := "gerrit"

i := 0
limit := 2
skip := 0
for i < 3 {
// Showcasing pagination
opt := &gerrit.BranchOptions{
Limit: limit,
Skip: skip,
}
fmt.Printf("ListBranches with skip %d and limit %d\n", skip, limit)
branches, _, err := client.Projects.ListBranches(projectName, opt)
if err != nil {
panic(err)
}

for _, branch := range *branches {
fmt.Printf("%s -> %s\n", branch.Ref, branch.Revision)
}

// Raising pagination pointer
i++
skip += limit
}

// ListBranches with skip 0 and limit 2
// HEAD -> master
// refs/meta/config -> 35fc56a537db06bbb9e5bf92f3a3e2e096d4f4c9
// ListBranches with skip 2 and limit 2
// refs/heads/infra/config -> 54bf8ddf9bcb4ffc748dd6fead98ececa95c98e5
// refs/heads/master -> 5f8903effc18583a4796f2adfba13d416f874444
// ListBranches with skip 4 and limit 2
// refs/heads/stable-2.10 -> ed2d5cedd8d79ee224fcc2280a6f11e8175fc2b0
// refs/heads/stable-2.11 -> 854e55ec22dadfc76b1112eb086f0d20dd4a977c
}
File renamed without changes.
52 changes: 52 additions & 0 deletions examples/list_projects_with_pagination/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"

"github.com/andygrunwald/go-gerrit"
)

func main() {
instance := "https://gerrit-review.googlesource.com/"
client, err := gerrit.NewClient(instance, nil)
if err != nil {
panic(err)
}

i := 0
limit := 2
skip := 0
for i < 3 {
// Showcasing pagination
opt := &gerrit.ProjectOptions{
ProjectBaseOptions: gerrit.ProjectBaseOptions{
Limit: limit,
Skip: skip,
},
Description: true,
}
fmt.Printf("ListProjects with skip %d and limit %d\n", skip, limit)
projects, _, err := client.Projects.ListProjects(opt)
if err != nil {
panic(err)
}

for name, p := range *projects {
fmt.Printf("%s - State: %s\n", name, p.State)
}

// Raising pagination pointer
i++
skip += limit
}

// ListProjects with skip 0 and limit 2
// Core-Plugins - State: ACTIVE
// Public-Plugins - State: ACTIVE
// ListProjects with skip 2 and limit 2
// Public-Projects - State: ACTIVE
// TestRepo - State: ACTIVE
// ListProjects with skip 4 and limit 2
// apps/analytics-etl - State: ACTIVE
// apps/kibana-dashboard - State: ACTIVE
}
51 changes: 51 additions & 0 deletions examples/list_tags_with_pagination/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"fmt"

"github.com/andygrunwald/go-gerrit"
)

func main() {
instance := "https://gerrit-review.googlesource.com/"
client, err := gerrit.NewClient(instance, nil)
if err != nil {
panic(err)
}

projectName := "gerrit"

i := 0
limit := 2
skip := 0
for i < 3 {
// Showcasing pagination
opt := &gerrit.ProjectBaseOptions{
Limit: limit,
Skip: skip,
}
fmt.Printf("ListTags with skip %d and limit %d\n", skip, limit)
tags, _, err := client.Projects.ListTags(projectName, opt)
if err != nil {
panic(err)
}

for _, tag := range *tags {
fmt.Printf("%s -> %s\n", tag.Message, tag.Ref)
}

// Raising pagination pointer
i++
skip += limit
}

// ListTags with skip 0 and limit 2
// gerrit 2.0 -> refs/tags/v2.0
// gerrit 2.0-rc0 -> refs/tags/v2.0-rc0
// ListTags with skip 2 and limit 2
// gerrit 2.0.1 -> refs/tags/v2.0.1
// gerrit 2.0.10 -> refs/tags/v2.0.10
// ListTags with skip 4 and limit 2
// gerrit 2.0.11 -> refs/tags/v2.0.11
// gerrit 2.0.12 -> refs/tags/v2.0.12
}
56 changes: 56 additions & 0 deletions examples/query_changes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"

"github.com/andygrunwald/go-gerrit"
)

func main() {
instance := "https://gerrit-review.googlesource.com/"
client, err := gerrit.NewClient(instance, nil)
if err != nil {
panic(err)
}

i := 0
limit := 2
start := 0
for i < 3 {
// Showcasing
// - Pagination
// - How to query for a project
opt := &gerrit.QueryChangeOptions{
QueryOptions: gerrit.QueryOptions{
Query: []string{
"project:gerrit",
},
Limit: limit,
},
Start: start,
}
fmt.Printf("QueryChanges with start %d and limit %d\n", start, limit)
changes, _, err := client.Changes.QueryChanges(opt)
if err != nil {
panic(err)
}

for _, changeInfo := range *changes {
fmt.Printf("%+v -> %+v\n", changeInfo.ID, changeInfo.Subject)
}

// Raising pagination pointer
i++
start += limit
}

// QueryChanges with start 0 and limit 2
// gerrit~stable-3.3~I48896badc4a14927b98fef0311c4a63ba5b2251d -> Allow loading change notes from an existing Repository
// gerrit~master~I644cdc74b679844b21054445e68b3e171e836834 -> Add support for 'is:attention' and 'has:attention'
// QueryChanges with start 2 and limit 2
// gerrit~master~Id3b9e395c977821f8957180a70de5379402d3621 -> Fix gr-identities "Link Another Identity" button test
// gerrit~stable-3.2~I85db585f9f4799f35dfc0913ec10f473fe08c25a -> Fix serialization of AllUsersName and AllProjectsName
// QueryChanges with start 4 and limit 2
// gerrit~stable-3.2~Ic6520c1fc44ef51de8bb5d33e307e60ece630163 -> Log the result of git-upload-pack command in httpd_log
// gerrit~stable-3.3~I668e0a322feb09a8dad47233baa6fd9585b4a8a9 -> Do not avertise ALL refs when HEAD is requested
}
7 changes: 2 additions & 5 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ type ProjectBaseOptions struct {
// Limit the number of projects to be included in the results.
Limit int `url:"n,omitempty"`

// Skip the given number of branches from the beginning of the list.
Skip string `url:"s,omitempty"`
// Skip the given number of tags from the beginning of the list.
Skip int `url:"S,omitempty"`
}

// ProjectOptions specifies the parameters to the ProjectsService.ListProjects.
Expand All @@ -192,9 +192,6 @@ type ProjectOptions struct {
// For example: the regex 'test.*' will match any projects that start with 'test' and regex '.*test' will match any project that end with 'test'.
Regex string `url:"r,omitempty"`

// Skip the given number of projects from the beginning of the list.
Skip string `url:"S,omitempty"`

// Limit the results to those projects that match the specified substring.
Substring string `url:"m,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion projects_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type BranchOptions struct {
Limit int `url:"n,omitempty"`

// Skip the given number of branches from the beginning of the list.
Skip string `url:"s,omitempty"`
Skip int `url:"S,omitempty"`

// Substring limits the results to those projects that match the specified substring.
Substring string `url:"m,omitempty"`
Expand Down