1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-04-02 21:55:33 +02:00

56 lines
1.2 KiB
Go
Raw Normal View History

2021-02-19 08:34:23 +11:00
package main
import (
"fmt"
jira "github.com/andygrunwald/go-jira"
)
2021-03-03 10:00:55 +11:00
// 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.
2021-02-19 08:34:23 +11:00
func GetAllIssues(client *jira.Client, searchString string) ([]jira.Issue, error) {
last := 0
2021-03-03 10:00:55 +11:00
var issues []jira.Issue
2021-02-19 08:34:23 +11:00
for {
opt := &jira.SearchOptions{
2021-03-03 12:52:56 +11:00
MaxResults: 1000, // Max results can go up to 1000
2021-02-19 08:34:23 +11:00
StartAt: last,
}
chunk, resp, err := client.Issue.Search(searchString, opt)
if err != nil {
return nil, err
}
2021-03-03 10:05:31 +11:00
total := resp.Total
2021-02-19 08:34:23 +11:00
if issues == nil {
issues = make([]jira.Issue, 0, total)
}
issues = append(issues, chunk...)
last = resp.StartAt + len(chunk)
if last >= total {
2021-03-03 10:00:55 +11:00
return issues, nil
2021-02-19 08:34:23 +11:00
}
}
2021-03-03 12:52:56 +11:00
2021-02-19 08:34:23 +11:00
}
func main() {
2021-03-03 10:00:55 +11:00
jiraClient, err := jira.NewClient(nil, "https://issues.apache.org/jira/")
if err != nil {
panic(err)
}
2021-02-19 08:34:23 +11:00
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)
}