From 89e9da43697a701128fdbc7d1dc62cc1cd08c082 Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Fri, 19 Feb 2021 08:34:23 +1100 Subject: [PATCH 1/4] Adding example for Pagination --- README.md | 58 +++++++++++++++++++++++++++++++++++++ examples/pagination/main.go | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 examples/pagination/main.go diff --git a/README.md b/README.md index 46479971..1eb6c3c3 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,65 @@ func main() { fmt.Printf("Status after transition: %+v\n", issue.Fields.Status.Name) } ``` +### Get all the issues for JQL with Pagination +Jira API has limit on maxResults it can return. YOu may have a usecase where you need to get all issues for given JQL. +This example shows reference implementation of GetAllIssues function which does pagination on Jira API to get all the issues for given JQL +```go +package main + +import ( + "fmt" + + jira "github.com/andygrunwald/go-jira" +) +//GetAllIssues takes a jira client and returns all issues for given JQL +func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error) { + last := 0 + var issues []jira.Issue = nil + for { + opt := &jira.SearchOptions{ + MaxResults: 100, + StartAt: last, + } + + chunk, resp, err := client.Issue.Search(searchString, opt) + if err != nil { + return nil, err + } + + total := resp.Total + if issues == nil { + issues = make([]jira.Issue, 0, total) + } + issues = append(issues, chunk...) + last = resp.StartAt + len(chunk) + if last >= total { + break + } + } + return issues, nil +} + +func main() { + jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/") + + // Jira API has limitation as to maxResults it can return at one time. + // You may have usecase where you need to get all the issues according to jql + // This is where this example comes in. + jql := "project = Mesos and type = Bug and Status NOT IN (Resolved)" + fmt.Printf("Usecase: Running a JQL query '%s'\n", jql) + + issues, err := GetAllIssues(jiraClient, jql) + if err != nil { + panic(err) + } + fmt.Println(issues) + +} + + +``` ### Call a not implemented API endpoint Not all API endpoints of the Jira API are implemented into *go-jira*. diff --git a/examples/pagination/main.go b/examples/pagination/main.go new file mode 100644 index 00000000..8fc8976a --- /dev/null +++ b/examples/pagination/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + + jira "github.com/andygrunwald/go-jira" +) + +//GetAllIssues takes a jira client and returns all issues for given JQL +func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error) { + last := 0 + var issues []jira.Issue = nil + for { + opt := &jira.SearchOptions{ + MaxResults: 100, + StartAt: last, + } + + chunk, resp, err := client.Issue.Search(searchString, opt) + if err != nil { + return nil, err + } + + total := resp.Total + if issues == nil { + issues = make([]jira.Issue, 0, total) + } + issues = append(issues, chunk...) + last = resp.StartAt + len(chunk) + if last >= total { + break + } + } + return issues, nil +} + +func main() { + jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/") + + // Jira API has limitation as to maxResults it can return at one time. + // You may have usecase where you need to get all the issues according to jql + // This is where this example comes in. + jql := "project = Mesos and type = Bug and Status NOT IN (Resolved)" + fmt.Printf("Usecase: Running a JQL query '%s'\n", jql) + + issues, err := GetAllIssues(jiraClient, jql) + if err != nil { + panic(err) + } + fmt.Println(issues) + +} From 97404d7e0caa6e4d8d5e7732b078ff507ba15c01 Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Wed, 3 Mar 2021 10:00:55 +1100 Subject: [PATCH 2/4] update as requested --- README.md | 56 ++----------------------------------- examples/pagination/main.go | 21 ++++++++------ 2 files changed, 14 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 1eb6c3c3..105c625c 100644 --- a/README.md +++ b/README.md @@ -222,62 +222,10 @@ func main() { } ``` ### Get all the issues for JQL with Pagination -Jira API has limit on maxResults it can return. YOu may have a usecase where you need to get all issues for given JQL. +Jira API has limit on maxResults it can return. You may have a usecase where you need to get all issues for given JQL. This example shows reference implementation of GetAllIssues function which does pagination on Jira API to get all the issues for given JQL -```go -package main - -import ( - "fmt" - - jira "github.com/andygrunwald/go-jira" -) - -//GetAllIssues takes a jira client and returns all issues for given JQL -func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error) { - last := 0 - var issues []jira.Issue = nil - for { - opt := &jira.SearchOptions{ - MaxResults: 100, - StartAt: last, - } - - chunk, resp, err := client.Issue.Search(searchString, opt) - if err != nil { - return nil, err - } - - total := resp.Total - if issues == nil { - issues = make([]jira.Issue, 0, total) - } - issues = append(issues, chunk...) - last = resp.StartAt + len(chunk) - if last >= total { - break - } - } - return issues, nil -} - -func main() { - jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/") - - // Jira API has limitation as to maxResults it can return at one time. - // You may have usecase where you need to get all the issues according to jql - // This is where this example comes in. - jql := "project = Mesos and type = Bug and Status NOT IN (Resolved)" - fmt.Printf("Usecase: Running a JQL query '%s'\n", jql) - - issues, err := GetAllIssues(jiraClient, jql) - if err != nil { - panic(err) - } - fmt.Println(issues) - -} +please look at examples/pagination/main.go ``` ### Call a not implemented API endpoint diff --git a/examples/pagination/main.go b/examples/pagination/main.go index 8fc8976a..6104959a 100644 --- a/examples/pagination/main.go +++ b/examples/pagination/main.go @@ -6,13 +6,16 @@ import ( jira "github.com/andygrunwald/go-jira" ) -//GetAllIssues takes a jira client and returns all issues for given JQL +// GetAllIssues will implement pagination of api and get all the issues. +// Jira API has limitation as to maxResults it can return at one time. +// You may have usecase where you need to get all the issues according to jql +// This is where this example comes in. func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error) { last := 0 - var issues []jira.Issue = nil + var issues []jira.Issue for { opt := &jira.SearchOptions{ - MaxResults: 100, + MaxResults: 1000, // Max results can go upto 1000 StartAt: last, } @@ -21,25 +24,25 @@ func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error return nil, err } - total := resp.Total if issues == nil { + total := resp.Total issues = make([]jira.Issue, 0, total) } issues = append(issues, chunk...) last = resp.StartAt + len(chunk) if last >= total { - break + return issues, nil } } return issues, nil } func main() { - jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/") + jiraClient, err := jira.NewClient(nil, "https://issues.apache.org/jira/") + if err != nil { + panic(err) + } - // Jira API has limitation as to maxResults it can return at one time. - // You may have usecase where you need to get all the issues according to jql - // This is where this example comes in. jql := "project = Mesos and type = Bug and Status NOT IN (Resolved)" fmt.Printf("Usecase: Running a JQL query '%s'\n", jql) From 9c2ab8f85683e8999a04217076002da95e2feadc Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Wed, 3 Mar 2021 10:05:31 +1100 Subject: [PATCH 3/4] var total was having an issues --- examples/pagination/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pagination/main.go b/examples/pagination/main.go index 6104959a..bc92cb69 100644 --- a/examples/pagination/main.go +++ b/examples/pagination/main.go @@ -24,8 +24,8 @@ func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error return nil, err } + total := resp.Total if issues == nil { - total := resp.Total issues = make([]jira.Issue, 0, total) } issues = append(issues, chunk...) From cac0fa9648cbe8e8b674c6acd19c9416b2b3064b Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Wed, 3 Mar 2021 12:52:56 +1100 Subject: [PATCH 4/4] Updates as requested --- README.md | 6 ++++-- examples/pagination/main.go | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 105c625c..74cd52a5 100644 --- a/README.md +++ b/README.md @@ -225,9 +225,11 @@ func main() { Jira API has limit on maxResults it can return. You may have a usecase where you need to get all issues for given JQL. This example shows reference implementation of GetAllIssues function which does pagination on Jira API to get all the issues for given JQL -please look at examples/pagination/main.go +please look at [Pagination Example](https://github.com/andygrunwald/go-jira/blob/master/examples/pagination/main.go) + + + -``` ### Call a not implemented API endpoint Not all API endpoints of the Jira API are implemented into *go-jira*. diff --git a/examples/pagination/main.go b/examples/pagination/main.go index bc92cb69..571e9e12 100644 --- a/examples/pagination/main.go +++ b/examples/pagination/main.go @@ -15,7 +15,7 @@ func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error var issues []jira.Issue for { opt := &jira.SearchOptions{ - MaxResults: 1000, // Max results can go upto 1000 + MaxResults: 1000, // Max results can go up to 1000 StartAt: last, } @@ -34,7 +34,7 @@ func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error return issues, nil } } - return issues, nil + } func main() {