0

In jira rest api, we are working with transition api. By posting on /rest/api/2/issue/{issueIdOrKey}/transitions url with transition id and comments and other fields, we are able to post comments and other fields with state transition.

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

}

Recently we came to know that jira has validation for attachments as well. Means I need to add attachment if I do transition. We are in search of how to add attachment during transition using rest api.

Any help would be really appreciated.

Thanks in advance.

Gaurav
  • 811
  • 9
  • 21

2 Answers2

1

Not sure if it would work during transitions, but here's how to add attachments.

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

I had to make 2 calls -- first to create the issue and then another POST call to update it with the screenshot as there is no way to add attachments in the create call at all.

Inxsible
  • 700
  • 5
  • 27
0

I am Not sure about adding attachements with transition(I had never done), but I think can be clubbed. Here is the code to just add an attachment to JIRA, You can use Transition API of JRJC and apply/add some logic in below code to get work done.

Follow link [Listing All JIRA Transitions via API for updating status based on transition, Hope you may get something from here too.

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 is a POJO class which contains attachments to be added in Jira, through getter/ setter method.

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;
Community
  • 1
  • 1
Asad Ali
  • 389
  • 1
  • 10
  • 28