【问题标题】:Access JIRA Agile sprints with JIRA REST Client api使用 JIRA REST Client api 访问 JIRA 敏捷冲刺
【发布时间】:2016-10-14 09:15:33
【问题描述】:

有谁知道如何使用 JIRA java REST Client api 或以其他方式使用 java 访问 JIRA Agile sprint。

1) 我正在尝试检索 JIRA 数据(项目、冲刺、问题等)。我需要一种方法来查询项目,也需要使用不同的过滤器进行冲刺。我猜 JRJC 无法获得敏捷数据。是否有针对整个 JIRA 数据的一站式解决方案?

2) JIRA 在其文档中提供了 2 种类型的 api:REST Api 和 Java Api。什么时候使用这些

【问题讨论】:

  • 你在用greenhopper吗?
  • 能否提供一些文档的链接

标签: jira jira-rest-api jira-agile jira-rest-java-api


【解决方案1】:
  1. 您想要的所有信息都在这里:

https://docs.atlassian.com/jira/REST/latest/

请注意,您可以通过以下方式获取项目信息:https://docs.atlassian.com/jira/REST/latest/#api/2/project

a) 至于 sprint 信息 - 您可以通过 JQL 获得所需的信息吗?示例 - 您可以转到问题 > 搜索问题并使用基本搜索吗?在这里,在“更多”选项下,您将看到您可以指定要过滤的 Sprint。

当您添加/删除 sprint 时,您会看到 URL 发生变化。例如,如果我选择查看 Sprint 1 和 Sprint 6 中的所有问题类型,我的 JQL 如下所示:

?jql=Sprint%20in%20(227%2C%20229)

请注意,这些是 sprint id。

现在您可以像这样通过搜索调用其余 api:

https://jira.domain.com/rest/api/2/search?jql=Sprint%20in%20(227%2C%20229)

更多细节在这里: https://docs.atlassian.com/jira/REST/latest/#api/2/search

b) 如果您可以使用过滤器获得所需的信息,您可以简单地保存过滤器并使用其余 api 调用过滤器

https://jira.domain.com/rest/api/2/filter/{filter-id}

更多细节在这里:https://docs.atlassian.com/jira/REST/latest/#api/2/filter-getFilter

  1. 您问题的第二部分实际上是另一个 stackoverflow 问题: jira REST API 只是一个 REST API——它是用于获取数据的 HTTP 方法调用。您可以使用 PHP、Java(例如,使用 Jersey)、bash(使用 curl)甚至只是使用带有 Chrome 邮递员的浏览器来调用它们。

Java API 或 Jira 的 REST Java 客户端是一组不受支持的库,如果您特别想使用 Java 进行这些 REST 调用,则可以使用这些库。当然,您可以使用 Jersey 或其他任何您想进行 Rest 调用的方法,但 Java 客户端提供了一些可能有用的库。

【讨论】:

    【解决方案2】:

    试试这个..我用下面的代码,

        private int setSprint(String sprint, String key, String boardId) {
        String _jiraUser = applicationProperties.get(Constants.JIRAUSER);
        String _jiraPwd = applicationProperties.get(Constants.JIRAPWD);
        String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd));
    
        String agileURL = applicationProperties.get(Constants.JIRAURL)
                + "/rest/agile/1.0/board/" + boardId + "/sprint";
    
        int sprintId = invokeGetMethod(auth, agileURL, sprint);
        // Constants.REPORT.info(sprintId);
    
        String issueURL = applicationProperties.get(Constants.JIRAURL)
                + "/rest/agile/1.0/issue/" + key;
    
        int issueId = getIssueId(auth, issueURL, key);
        // Constants.REPORT.info(issueId);
        return invokePostMethod(auth, sprintId, key, issueId);
        // Constants.REPORT.info(statusCode);
    
    }
    
    private static int invokeGetMethod(String auth, String agileURL,
            String sprint) {
    
        int sprintId = 0;
        int startAt = 0;
        agileURL = agileURL + "?startAt=" + startAt;
        sprintId = getSprintResult(agileURL, auth, sprint, startAt);
        // sprintId = getSprintId(content, sprint, agileURL);
    
        return sprintId;
    }
    
    private static int getSprintResult(String agileURL, String auth,
            String sprint, int startAt) {
        HttpGet post = new HttpGet(agileURL);
        org.apache.http.impl.client.DefaultHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
        post.setHeader("Content-type", "application/json");
        post.setHeader("Authorization", "Basic " + auth);
        HttpResponse response = null;
        String content = null;
        try {
            response = httpClient.execute(post);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            content = CharStreams.toString(new InputStreamReader(response
                    .getEntity().getContent(), Charsets.UTF_8));
        } catch (IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int SprintId = getSprintId(content, sprint, agileURL, startAt, auth);
        if (SprintId != 0)
            return SprintId;
        return SprintId;
    }
    
    static int getSprintId(String content, String sprint, String agileURL,
            int startAt, String auth) {
        boolean flag = false;
    
        int sprintId = 0;
        Gson gson = new Gson(); // Class<String> data=new Class<String>();
        Data values = gson.fromJson(content, Data.class);
        for (Values data : values.getValues()) { // data.get
            if (data.getName().equalsIgnoreCase(sprint))
            // Constants.REPORT.info(sprintId=data.getId());
            {
                flag = true;
                // Constants.REPORT.info(data);
                // statusCode = response.getEntityInputStream().;
                return sprintId = data.getId();
            }
    
        }
        if (!flag) {
        //Pagination
            agileURL = agileURL.replaceAll(
                    "startAt=" + String.valueOf(startAt),
                    "startAt=" + String.valueOf(startAt += 50));
            sprintId = getSprintResult(agileURL, auth, sprint, startAt);
            if (sprintId != 0)
                return sprintId;
    
        }
        return sprintId;
    
    }
    
    private static int getIssueId(String auth, String agileURL, String key) {
    
        int issueId = 0;
        try {
    
            org.apache.http.impl.client.DefaultHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
            HttpGet post = new HttpGet(agileURL);
    
            post.setHeader("Content-type", "application/json");
            post.setHeader("Authorization", "Basic " + auth);
            HttpResponse response = httpClient.execute(post);
            String content = CharStreams.toString(new InputStreamReader(
                    response.getEntity().getContent(), Charsets.UTF_8));
    
            Gson gson = new Gson(); // Class<String> data=new Class<String>();
    
            IssueData issueValues = gson.fromJson(content, IssueData.class);
            if (issueValues.getKey().equalsIgnoreCase(key))
                issueId = issueValues.getId();
            // Constants.REPORT.info(issueValues);
        } catch (Exception e) {
            Constants.ERROR.info(Level.INFO, e);
            // vjErrorLog.info(Level.INFO, e);
        }
        return issueId;
    }
    private static int invokePostMethod(String auth, int sprintId, String key,
            int issueId) {
    
        int statusCode = 0;
        try {
            String postUrl = applicationProperties.get(Constants.JIRAURL)
                    + "/rest/agile/1.0/sprint/" + sprintId + "/issue";
            String str = "{\"issues\": [\"" + key + "\",\"" + issueId + "\"]}";
            statusCode = invokePostMethod(auth, postUrl, str);
    
            return statusCode;
        } catch (Exception e) {
            Constants.ERROR.info(Level.INFO, e);
            // vjErrorLog.info(Level.INFO, e);
        }
        return statusCode;
    }
    

    创建具有以下属性的 IssueData 类。并创建 getter/setter 也覆盖 toString() 方法。

    private int id;
    private String self;
    private String key;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 2017-01-23
      • 2013-10-23
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多