Jira rest api,添加带过渡的附件

时间:2014-11-21 06:33:24

标签: jira jira-rest-api

在jira rest api中,我们正在使用transition api。 通过在/ rest / api / 2 / issue / {issueIdOrKey} / transition url上发布转换ID和注释以及其他字段,我们可以发布带有状态转换的注释和其他字段。

{
    "fields" : {"summary": "Test update 5"},
    "transition": { "id": "4"},
"update": {
      "comment": [
         {
            "add": {
               "body": "It is time to finish this task"
            }
         }
      ]
   }

}

最近我们开始知道jira也对附件进行了验证。意味着如果我做过渡,我需要添加附件。我们正在寻找如何使用rest api在转换期间添加附件。

任何帮助都会非常感激。

提前致谢。

2 个答案:

答案 0 :(得分:1)

不确定它是否会在转换过程中起作用,但这里是如何添加附件。

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

我必须进行2次调用 - 首先创建问题,然后再使用截屏更新它,因为根本无法在创建调用中添加附件。

答案 1 :(得分:0)

我不确定是否添加带过渡的附件(我从未这样做过),但我认为可以使用。以下是仅向JIRA添加附件的代码,您可以使用JRJC的Transition API并在下面的代码中应用/添加一些逻辑来完成工作。

关注链接[Listing All JIRA Transitions via API 根据过渡更新状态,希望你也可以从这里得到一些东西。

public void addAttachment(IssueRestClient issueRestClient,
        VersionOne versionOne, Issue issue, Epics epic) {

    try {

        URI attachmentsUri = new URI(
                applicationProperties.get(Constants.JIRAURL)
                        + "/rest/api/2/issue/" + issue.getKey()
                        + "/attachments");
        Iterable<Attachment> attachments = issue.getAttachments();
        Set<String> existingAttachments = new TreeSet<String>();
        String _jiraUser = applicationProperties.get(Constants.JIRAUSER);
        String _jiraPwd = applicationProperties.get(Constants.JIRAPWD);
        String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd));
        Set<String> files = new TreeSet<String>();

        for (Attachment attachment : attachments) {
            for (VAttachements vAttachement : epic
                    .getAttachement()) {
                files.add(vAttachement.getFileName());

            }
            existingAttachments.add(attachment.getFilename());
        }

        for (VAttachements vAttachement : epic.getvAttachement()) {

            if (!(existingAttachments.contains(vAttachement.getFileName()))) {

                Promise<Void> attachmentResult = issueRestClient
                        .addAttachment(attachmentsUri,
                                vAttachement.getInputStream(),
                                vAttachement.getFileName());
                attachmentResult.claim();
                Constants.REPORT.info(attachmentResult.isDone());

            }

        }
        for (Attachment attachment : attachments) {
            for (String checkAttachment : existingAttachments) {
                if (!files.contains(checkAttachment))
                    deleteJiraAttachment(attachment, auth, issue,
                            checkAttachment);

            }
        }

    } catch (Exception e) {
        Constants.ERROR.info(Level.INFO, e);

    }
}

-Here Epics是一个POJO类,它包含要通过getter / setter方法在Jira中添加的附件。

private void deleteJiraAttachment(Attachment attachment, String auth,
        Issue issue, String jiraFilename) {

    URI attachmentURL = attachment.getSelf();

    int status;
    try {
        if (jiraFilename.equalsIgnoreCase(attachment.getFilename())) {
            status = invokeDeleteMethod(auth, String.valueOf(attachmentURL));

            if (status == 204) {
                Constants.REPORT.info("Attachment deleted from Issue"
                        + issue.getKey());
            } else if (status == 403) {
                System.out
                        .println("attachments for Issue\t "
                                + issue.getKey()
                                + " is disabled or you don't have permission to remove");
            } else if (status == 404) {
                Constants.REPORT.info("No attachment is not found for"
                        + issue.getKey());
            }
        }
    } catch (AuthenticationException | ClientHandlerException e) {
        Constants.ERROR.info(Level.INFO, e);

    }

}

private static int invokeDeleteMethod(String auth, String url)
        throws AuthenticationException, ClientHandlerException {

    Client client = Client.create();
    WebResource webResource = client.resource(url);
    ClientResponse response = webResource
            .header("Authorization", "Basic " + auth)
            .type("application/json").accept("application/json")
            .delete(ClientResponse.class);
    int statusCode = response.getStatus();
    if (statusCode == 401) {
        throw new AuthenticationException("Invalid Username or Password");
    }
    return statusCode;