Reputation: 43
I'm trying to get all the tasks from Jirà using the API. I found the working code:
public class CustomJiraRestClient {
private static final String JIRA_URL = "http://jira-dev:8080";
private static final String JIRA_ADMIN_USERNAME = "admin";
private static final String JIRA_ADMIN_PASSWORD = "admin";
public static void main(String[] args) throws Exception {
// Construct the JRJC client
System.out.println(String.format("Logging in to %s with username '%s' and password '%s'", JIRA_URL, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD));
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
URI uri = new URI(JIRA_URL);
JiraRestClient client = factory.createWithBasicHttpAuthentication(uri, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD);
// Invoke the JRJC Client
Promise<User> promise = client.getUserClient().getUser("admin");
User user = promise.claim();
Promise<SearchResult> searchJqlPromise = client.getSearchClient().searchJql("project = MYPURRJECT AND status in (Closed, Completed, Resolved) ORDER BY assignee, resolutiondate");
for (Issue issue : searchJqlPromise.claim().getIssues()) {
System.out.println(issue.getSummary());
}
// Done
System.out.println("Example complete. Now exiting.");
System.exit(0);
}
}
but there is one problem: - only the first 50 entries are displayed. As in the usual, graphical interface - a page with 50 entries on it. Please help me, how do I get or all the records at once? Or how can I specify the desired page or item.
I found methods:
searchResult.getMaxResults();
- the number of all records (n)searchResult.getTotal();
number of entries per page (50)searchResult.getStartIndex();
- the number of the first element (0)but I can not put them anywhere for use. I can recognize them, but it does not make any sense.
Here are the software versions I use:
Upvotes: 3
Views: 1914
Reputation: 1
I suggest to use following function.
public List<Issue> getIssuesList(String query) {
boolean flag = false;
int start = 0, end=Constants.MAX_RESULTS_PER_PAGE, totalResults = 0;
List<Issue> issueList = new ArrayList<Issue>();
SearchResult searchResult;
do {
Promise<SearchResult> searchJqlPromise = client.getSearchClient().searchJql(query,end,start, null);
searchResult = searchJqlPromise.claim();
if(flag==false) {
totalResults = searchResult.getTotal();
flag=true;
};
List<Issue> res = (List<Issue>) searchResult.getIssues();
issueList.addAll(res);
start = start + Constants.MAX_RESULTS_PER_PAGE;
totalResults -= Constants.MAX_RESULTS_PER_PAGE;
} while(totalResults>0);
return issueList;
}
Upvotes: 0
Reputation: 896
I use this simply loop for pagination.
List<Issue> issues = new ArrayList<>();
SearchRestClient searchClient = restClient.getSearchClient();
Set<String> set = new HashSet<String>();
set.add("*all"); /* you can use simply null set if you dont need all values */
int start = 0;
int maxPerPage = 25; /* batch size (max 50) */
int total = 0;
do {
SearchResult result = searchClient.searchJql(JQL, maxPerPage, start, set)
.claim();
total = result.getTotal();
start += maxPerPage;
result.getIssues().iterator().forEachRemaining(issues::add);
} while (total > start );
All issue now in issues
list.
Upvotes: 0
Reputation: 5105
From a quick glance at the API docs, I would say you need to use the overload searchJql(String jql, int maxResults, int startAt)
and specify the maxResults
argument.
Upvotes: 2