如何获取50多个Jira问题?

时间:2018-04-10 07:26:39

标签: java api jira

我正在尝试使用API​​从Jirà获取所有任务。 我找到了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);
    }
}

但有一个问题: - 仅显示前50个条目。 与通常的图形界面一样 - 一个包含50个条目的页面。 请帮帮我,我如何获得或所有记录?或者我如何指定所需的页面或项目。

我找到了方法:

  • searchResult.getMaxResults(); - 所有记录的数量(n)
  • searchResult.getTotal();每页的条目数(50)
  • searchResult.getStartIndex(); - 第一个元素的编号(0)

但我无法将它们放在任何地方使用。我可以认出它们,但它没有任何意义。

以下是我使用的软件版本:

  • Jira版本:7.2.13
  • atlassian-image-consumer(com.atlassian.image:atlassian-image-consumer:1.0.1:jar)
  • 卡罗尔(卡罗尔:卡罗尔:1.5.2:罐子)
  • carol-properties(carol:carol-properties:1.5.2:jar)
  • datafile(datafile:datafile:1.3.3:jar)
  • hibernate(hibernate:hibernate:2.1.8-atlassian:jar)
  • image4j(org.jclarion:image4j:0.7:jar)
  • JCaptcha(com.octo.captcha:jcaptcha-api:2.0-alpha-1:jar)
  • JCaptcha(com.octo.captcha:jcaptcha:2.0-alpha-1:jar)
  • jcommon(jfree:jcommon:1.0.8:jar)
  • jfreechart(jfree:jfreechart:1.0.13:jar)
  • JSCalendar(1.0)
  • jStyleParser(net.sf.cssbox:jstyleparser:1.16-atlassian-1:jar)
  • XML的流式API(javax.xml.stream:stax-api:1.0-2:jar)
  • TinyMCE-TinyMCE(4.4.0)
  • xapool(xapool:xapool:1.3.1:jar)

3 个答案:

答案 0 :(得分:2)

从快速浏览一下API文档,我会说你需要使用重载searchJql(String jql, int maxResults, int startAt)并指定maxResults参数。

答案 1 :(得分:0)

我使用此简单循环进行分页。

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

issues列表中的所有问题。

答案 2 :(得分:0)

我建议使用以下功能。

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;

}