From 5981f3b22941b1a2c1e94181fd519b6d1b30835f Mon Sep 17 00:00:00 2001 From: Andy Grunwald Date: Sun, 19 Sep 2021 11:31:46 +0200 Subject: [PATCH 1/4] Reverting 101051e002059e0c8c3143008b47108daf483a60 partially to avoid struct collision of Start parameter --- changes.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/changes.go b/changes.go index 938656d..5e014ac 100644 --- a/changes.go +++ b/changes.go @@ -510,9 +510,6 @@ type QueryOptions struct { // 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. From 8eb1af80428e8fa387a748fcdc87d0a9d8bb2ba9 Mon Sep 17 00:00:00 2001 From: Andy Grunwald Date: Sun, 19 Sep 2021 12:18:30 +0200 Subject: [PATCH 2/4] Fix docs for QueryChanges -> search-operators --- changes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changes.go b/changes.go index 5e014ac..9290ce0 100644 --- a/changes.go +++ b/changes.go @@ -504,7 +504,7 @@ 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. From 24294c11df1a807420927e8177aa76b5bf14a897 Mon Sep 17 00:00:00 2001 From: Andy Grunwald Date: Sun, 19 Sep 2021 12:20:30 +0200 Subject: [PATCH 3/4] Fix handling of Skip and Start parameters The way we treated Skip and Start parameters was inconsistent and wrong. In some calls we send both parameters (s, start, S). This causes and undefined behaviour at Gerrit itself. Additional the Skip/Start parameters are numbers and not strings. Here we fixed the Go types. Thanks a lot to @michaeldorner for his initial work in #48 Related: - https://github.com/andygrunwald/go-gerrit/pull/48 --- changes.go | 3 +-- projects.go | 7 ++----- projects_branch.go | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/changes.go b/changes.go index 9290ce0..ee515c8 100644 --- a/changes.go +++ b/changes.go @@ -518,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 diff --git a/projects.go b/projects.go index 17afaff..905be21 100644 --- a/projects.go +++ b/projects.go @@ -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. @@ -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"` diff --git a/projects_branch.go b/projects_branch.go index 285f20f..3974e1a 100644 --- a/projects_branch.go +++ b/projects_branch.go @@ -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"` From 3685df191bf6020307f84817b06a4f3b519ab346 Mon Sep 17 00:00:00 2001 From: Andy Grunwald Date: Sun, 19 Sep 2021 12:23:18 +0200 Subject: [PATCH 4/4] New examples with Pagination for ListBranches, ListProjects and ListTags --- .../list_branches_with_pagination/main.go | 51 +++++++++++++++++ .../main.go | 0 .../list_projects_with_pagination/main.go | 52 +++++++++++++++++ examples/list_tags_with_pagination/main.go | 51 +++++++++++++++++ examples/query_changes/main.go | 56 +++++++++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 examples/list_branches_with_pagination/main.go rename examples/{get_public_projects => list_projects}/main.go (100%) create mode 100644 examples/list_projects_with_pagination/main.go create mode 100644 examples/list_tags_with_pagination/main.go create mode 100644 examples/query_changes/main.go diff --git a/examples/list_branches_with_pagination/main.go b/examples/list_branches_with_pagination/main.go new file mode 100644 index 0000000..d0ef761 --- /dev/null +++ b/examples/list_branches_with_pagination/main.go @@ -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 +} diff --git a/examples/get_public_projects/main.go b/examples/list_projects/main.go similarity index 100% rename from examples/get_public_projects/main.go rename to examples/list_projects/main.go diff --git a/examples/list_projects_with_pagination/main.go b/examples/list_projects_with_pagination/main.go new file mode 100644 index 0000000..1e6c036 --- /dev/null +++ b/examples/list_projects_with_pagination/main.go @@ -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 +} diff --git a/examples/list_tags_with_pagination/main.go b/examples/list_tags_with_pagination/main.go new file mode 100644 index 0000000..df036a7 --- /dev/null +++ b/examples/list_tags_with_pagination/main.go @@ -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 +} diff --git a/examples/query_changes/main.go b/examples/query_changes/main.go new file mode 100644 index 0000000..b823f2e --- /dev/null +++ b/examples/query_changes/main.go @@ -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 +}