Skip to content

Commit d30c2ef

Browse files
Merge pull request #354 from kp2401075/paginationExample
Adding example for Pagination
2 parents 5168cea + cac0fa9 commit d30c2ef

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ func main() {
221221
fmt.Printf("Status after transition: %+v\n", issue.Fields.Status.Name)
222222
}
223223
```
224+
### Get all the issues for JQL with Pagination
225+
Jira API has limit on maxResults it can return. You may have a usecase where you need to get all issues for given JQL.
226+
This example shows reference implementation of GetAllIssues function which does pagination on Jira API to get all the issues for given JQL
227+
228+
please look at [Pagination Example](https://github.com/andygrunwald/go-jira/blob/master/examples/pagination/main.go)
229+
230+
231+
224232

225233
### Call a not implemented API endpoint
226234

examples/pagination/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
jira "github.com/andygrunwald/go-jira"
7+
)
8+
9+
// GetAllIssues will implement pagination of api and get all the issues.
10+
// Jira API has limitation as to maxResults it can return at one time.
11+
// You may have usecase where you need to get all the issues according to jql
12+
// This is where this example comes in.
13+
func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error) {
14+
last := 0
15+
var issues []jira.Issue
16+
for {
17+
opt := &jira.SearchOptions{
18+
MaxResults: 1000, // Max results can go up to 1000
19+
StartAt: last,
20+
}
21+
22+
chunk, resp, err := client.Issue.Search(searchString, opt)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
total := resp.Total
28+
if issues == nil {
29+
issues = make([]jira.Issue, 0, total)
30+
}
31+
issues = append(issues, chunk...)
32+
last = resp.StartAt + len(chunk)
33+
if last >= total {
34+
return issues, nil
35+
}
36+
}
37+
38+
}
39+
40+
func main() {
41+
jiraClient, err := jira.NewClient(nil, "https://issues.apache.org/jira/")
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
jql := "project = Mesos and type = Bug and Status NOT IN (Resolved)"
47+
fmt.Printf("Usecase: Running a JQL query '%s'\n", jql)
48+
49+
issues, err := GetAllIssues(jiraClient, jql)
50+
if err != nil {
51+
panic(err)
52+
}
53+
fmt.Println(issues)
54+
55+
}

0 commit comments

Comments
 (0)