mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-03-19 20:57:47 +02:00
Merge pull request #354 from kp2401075/paginationExample
Adding example for Pagination
This commit is contained in:
commit
d30c2ef40d
@ -221,6 +221,14 @@ func main() {
|
|||||||
fmt.Printf("Status after transition: %+v\n", issue.Fields.Status.Name)
|
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
|
||||||
|
|
||||||
|
please look at [Pagination Example](https://github.com/andygrunwald/go-jira/blob/master/examples/pagination/main.go)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Call a not implemented API endpoint
|
### Call a not implemented API endpoint
|
||||||
|
|
||||||
|
55
examples/pagination/main.go
Normal file
55
examples/pagination/main.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
jira "github.com/andygrunwald/go-jira"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
for {
|
||||||
|
opt := &jira.SearchOptions{
|
||||||
|
MaxResults: 1000, // Max results can go up to 1000
|
||||||
|
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 {
|
||||||
|
return issues, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
jiraClient, err := jira.NewClient(nil, "https://issues.apache.org/jira/")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user