如何通过休息 api 和 json 更新 Jira 中的描述

How to update description in Jira through rest api with json

下面是 JSON 数据,其中包含丰富的 text/wiki 文本。我想通过 REST API 将此数据传递给 Jira 中的一个问题。这里Java是我正在使用的技术。

{"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*"}}}

以上 JSON 我在 jiraUpdateFromBuilder.

存储

我正在调用 PUT 方法来更新 Jira 中的描述,如下所示。

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

这样做,我无法通过任何 REST API 更新 Jira 中问题的描述,因为这里得到的状态不是 201。同样的问题是 JIRA 中问题的所有字段其中包含富文本。如果我需要更改 JSON 或任何其他方法,请告诉我 JRJC 是否可以提供其他帮助。

您的 json 看起来像这样:

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

但是"fields"部分不需要使用'set'关键字,所以应该是这样的:

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

如果您查看 PUT /issue REST resource 的文档,您会看到它提到了这一点:

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.

此外,您提到您的响应状态代码是 400,这意味着这是一个错误的请求。响应正文可能会包含有关问题的更多详细信息,因此最好也将其记录下来。

关于这个错误:

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]

您的描述值包含换行符,但不允许直接在 json 字符串中使用它们。你将不得不逃避那些。参见 this post for an example

Jira 文档和这些答案似乎已过时。使用 API Jira 版本 3。 (API docs to edit issue)

正在更新 Jira 中问题的描述:

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

如果您使用不同的请求正文,您将收到以下错误:

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