Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding example for Pagination #354

Merged
merged 4 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
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
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
for {
opt := &jira.SearchOptions{
MaxResults: 100,
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
StartAt: last,
}

chunk, resp, err := client.Issue.Search(searchString, opt)
if err != nil {
return nil, err
}

total := resp.Total
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
if issues == nil {
issues = make([]jira.Issue, 0, total)
}
issues = append(issues, chunk...)
last = resp.StartAt + len(chunk)
if last >= total {
break
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
}
}
return issues, nil
}

func main() {
jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/")
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved

// 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*.
Expand Down
52 changes: 52 additions & 0 deletions examples/pagination/main.go
Original file line number Diff line number Diff line change
@@ -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) {
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
last := 0
var issues []jira.Issue = nil
for {
opt := &jira.SearchOptions{
MaxResults: 100,
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
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
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved
}

func main() {
jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/")
benjivesterby marked this conversation as resolved.
Show resolved Hide resolved

// 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)

}