【问题标题】:How to update description in Jira through rest api with json如何通过带有json的rest api更新Jira中的描述
【发布时间】:2016-06-22 12:29:02
【问题描述】:

以下是包含富文本/wiki 文本的 JSON 数据。我想通过 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*"}}}

我存储在 jiraUpdateFromBuilder 中的 JSON 之上。

我正在调用 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 调用 put() 方法后,我得到的状态码为 400。

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


【解决方案1】:

你的 json 看起来像这样:

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

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

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

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

指定“field_id”:“fields”中的 field_value 是“update”部分中“set”操作的简写。 字段应出现在“字段”或“更新”中,而不是两者中。

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

关于这个错误:

非法不带引号的字符((CTRL-CHAR,代码 10)):必须使用反斜杠转义才能包含在 [Source: org.apache.catalina.connector.CoyoteInputStream@20e841d2;行:1,列:187]

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

【讨论】:

  • 这是真的,在我使用没有“设置”之前,它没有工作。这是没有“set”的完整日志和json。
  • {"update":{"summary": [{"set": "CRF-397 – 对于 Virgin Mobile,遇到层级不匹配异常时必须通过电子邮件发送警报。"}]} ,"fields":{"description":"受影响的客户 * Virgin Mobile *背景信息*
    所有英国计划都在不同级别遇到以下问题: * 客户被收取错误的保费"}} 这是响应消息。 PUT ejira.int.syne.com/rest/api/2/issue/ISD-17223 返回响应状态 400
  • 400 是响应状态码。响应正文是什么?
  • {"errorMessages":["非法不带引号的字符 ((CTRL-CHAR, code 10)): 必须使用反斜杠转义才能包含在字符串值中\n at [Source: org.apache .catalina.connector.CoyoteInputStream@20e841d2; line: 1, column: 187]"]} 我认为这是因为 json 字符串中的富文本字符,如“*”、|、~ 等,但如果是这种情况,那么如何通过 json 发送富文本/wiki 文本?在每个富文本中使用反斜杠是一个乏味的过程?有什么建议或替代方案吗??
  • 我已经更新了我的答案。您的描述包含必须在 json 字符串中转义的换行符。
【解决方案2】:

Jira 文档和这些答案似乎已经过时。使用 Jira 的 API 版本 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)"
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2022-10-31
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多