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

Conversation

kp2401075
Copy link

Description

Adding example of pagination to get all issues for given jql.
code was taken from example given by @eparis at #55

Information that is useful here:

  • **Adding an example that shows how to get all the issues for given JQL with paginaiton
  • **This will be helpful for users with usecases where jira's maxResult doen't give all the issues for given JQL
  • **Documentation
  • Breaking change: NO
  • Related to an issue: Add support for pagination #55
  • Jira Version + Type:In-Cloud (Company manged)

Example:

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)

}

@andygrunwald please help with below section. I'm unsure.

Checklist

@kp2401075
Copy link
Author

@andygrunwald
Can you please have a look at checklist and advise If I need to take any action.

thanks.

Copy link
Contributor

@benjivesterby benjivesterby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be wise to just link to the example in examples/pagination/main.go instead of copying into the README. That way there isn't duplication but you have the same effect. Thoughts?

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
examples/pagination/main.go Show resolved Hide resolved
examples/pagination/main.go Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
examples/pagination/main.go Outdated Show resolved Hide resolved
@kp2401075
Copy link
Author

@benjivesterby Thanks alot for your help, I've made more changes as you requested.

Can you please verify that is satisfy all the requirements.

thanks again for looking into this.

Copy link
Contributor

@benjivesterby benjivesterby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good except for the vet issue and a typo once those are fixed I'll approve. Also should add a link to the file in the readme as I mentioned in the comment.

README.md Outdated Show resolved Hide resolved
examples/pagination/main.go Outdated Show resolved Hide resolved
examples/pagination/main.go Outdated Show resolved Hide resolved
@kp2401075
Copy link
Author

Updated accordingly @benjivesterby Thanks for your help.

Copy link
Contributor

@benjivesterby benjivesterby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@benjivesterby benjivesterby merged commit d30c2ef into andygrunwald:master Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants