Asad Ali
Asad Ali

Reputation: 417

How to update description in Jira through rest api with json

Below is the JSON data which contains rich text/wiki text. I want to pass this data to to one of the issues in Jira through REST API. Here Java is the technology, I am using.

{"update":{"summary": [{"set": "CRF-397 – For Virgin Mobile, alert must be sent via email when Tier Mismatch exception is encountered."}]},"fields":{"description":{"set":"*Clients Impacted** Virgin Mobile *Background Information*<br>All UK schemes are experiencing at different levels some issues of:* Customers being billed on the wrong premium* Excess Fees paid at point of claim do not correspond to what has been communicated to the customer at the point of sale.* Welcome packs not being issued due to a mismatch *CRF Scope*<br>The scope of this project consists of identifying whenever a new device is communicated to Asurion by a client system and ensuring the data in each of those instances is validated to confirm that the device premium and excess fees are correctly aligned.*User Story Scope*<br>While doing enrollment if any record goes into exception due to Tier is match we have to send consolidated list of such records via email so that Asurion Team can communicate with Virgin Mobile to resolve the Tier Mismatch issues.*Requirement** An email alert must be sent when Tier Mismatch exception is encountered.* Flag based development must be done for triggering an email.* Email must be sent to Client Service, SCM and BI teams* Recipient email IDs must be configurable.* Exception list must contain below records:-      * The list of devices for which there was an exception * The Feature Code sent by Virgin Mobile * The feature code configured in Client Convention for the given device*"}}}

Above JSON I am storing in jiraUpdateFromBuilder.

I am calling PUT method to update description in Jira, as below.

String _jiraUrl = applicationProperties.get(Constants.JIRAURL)
            + "/rest/api/2/issue/" + reference;
String _jiraUser = applicationProperties.get(Constants.JIRAUSER);
String _jiraPwd = applicationProperties.get(Constants.JIRAPWD);
String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd));
int statusCode = invokePutMethod(auth, _jiraUrl.trim(),
            jiraUpdateFromBuilder.toString().trim());

public static int invokePutMethod(String auth, String url, String data) {

    int statusCode = 0;
    try {
        Client client = Client.create();
        WebResource webResource = client.resource(url);
        ClientResponse response = webResource
                .header("Authorization", "Basic " + auth)
                .type("application/json").accept("application/json")
                .put(ClientResponse.class, data);
        statusCode = response.getStatus();
        return statusCode;
    } catch (Exception e) {
        Constants.ERROR.info(Level.INFO, e);

    }
    return statusCode;
}

Doing so, I am unable to update description of issue in Jira, through any REST API, because here getting status other than 201. And the same problem is with all the field of an Issue in JIRA which contains rich text. Kindly let me know if JRJC can help else if I need to change in JSON or any other approach.

Upvotes: 2

Views: 8086

Answers (2)

Keet Sugathadasa
Keet Sugathadasa

Reputation: 13522

The Jira documentations and these answers seem to be outdated. With API version 3 of Jira. (API docs to edit issue)

Updating the description of an Issue in Jira:

{
    "fields": {
        "description": {
            "type": "doc",
            "version": 1,
            "content": [
                {
                    "type": "paragraph",
                    "content": [
                        {
                            "type": "text",
                            "text": "add your description here"
                        }
                    ]
                }
            ]
        }
    }
}

If you use a different request body, you will get the following error:

{
    "errorMessages": [],
    "errors": {
        "description": "Operation value must be an Atlassian Document (see the Atlassian Document Format)"
    }
}

Upvotes: 1

GlennV
GlennV

Reputation: 3660

Your json looks like this:

{
  "update": {
    "summary": [
      {
        "set": "CRF-397 ..."
      }
    ]
  },
  "fields": {
    "description": {
      "set": "..."
    }
  }
}

But the "fields" part does not require to use the 'set' keyword, so it should be something like this:

{
  "update": {
    "summary": [
      {
        "set": "CRF-397 ..."
      }
    ]
  },
  "fields": {
    "description": "..."
  }
}

If you check the documentation for the PUT /issue REST resource, you'll see that it mentions this:

Specifying a "field_id": field_value in the "fields" is a shorthand for a "set" operation in the "update" section. Field should appear either in "fields" or "update", not in both.

Also, you've mentioned that your response status code was 400, which means it's a bad request. The response body will probably contain more detail about what is wrong, so it's best to log that as well.

Regarding this error:

Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value\n at [Source: org.apache.catalina.connector.CoyoteInputStream@20e841d2; line: 1, column: 187]

Your description value contains newlines, but it's not allowed to use those in a json string directly. You'll have to escape those. See this post for an example.

Upvotes: 3

Related Questions