Skip to content

Instantly share code, notes, and snippets.

@davidecavestro
Last active May 10, 2019 17:55
Show Gist options
  • Save davidecavestro/7445178 to your computer and use it in GitHub Desktop.
Save davidecavestro/7445178 to your computer and use it in GitHub Desktop.
An iterator providing paginated access to JIRA REST services issue search results, in order to simplify 'maxResults' management. Every element is an iterator on the results of a single REST service call, hence obtaining ondemand fetches.
public class JiraIssuesIterator implements java.util.Iterator<java.util.Iterator<Issue>>{
SearchRestClient searchClient
int pageSize
String jql
Set<String> fields
int startAt = 0
int consumedIssuesCounter = 0
int issueTotal
SearchResult searchResult
/**
* the 1st hasNext() call should fetch data
*/
protected boolean firstStep
@Override
public boolean hasNext() {
if (searchResult==null) {
firstStep = true
search()
}
return consumedIssuesCounter < issueTotal;
}
@Override
public Iterator<Issue> next() {
if (firstStep) {
firstStep = false
} else {
search()
}
Iterator<Issue> issues = searchResult.issues.iterator()
return [
hasNext:{ issues.hasNext() },
next: {
consumedIssuesCounter++
issues.next()
}
] as Iterator<Issue>
}
def search() {
searchResult = searchClient.searchJql(jql, pageSize, startAt, fields).claim()
issueTotal = searchResult.total
}
@Override
public void remove() {
throw new UnsupportedOperationException()
}
}
...
Iterable<Issue> getRestIssuesWithJQL (){
final JiraRestClient restClient = ...
SearchRestClient searchClient = restClient.getSearchClient ()
Set<String> fields = ...
//concatenates the iterators using Guava Iterators.concat(Iterator<Iterator>) logic
return [iterator: {Iterators.concat(new JiraIssuesIterator (jql: jqlSearchExpr, pageSize: jiraPageSize, fields: fields, searchClient: searchClient))}] as Iterable<Issue>
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment