1

I am trying to create jira ticket using groovy script (on Marid server from Opsgenie. However, I am facing issue when trying to setup component field.

    import com.ifountain.opsgenie.client.http.OpsGenieHttpClient
import com.ifountain.opsgenie.client.util.ClientConfiguration
import com.ifountain.opsgenie.client.util.JsonUtils
import org.apache.http.HttpHeaders

LOG_PREFIX = "[${mappedAction}]:";
logger.info("${LOG_PREFIX} Will execute [${mappedAction}] for alertId ${params.alertId}");

CONF_PREFIX = "jira.";
HTTP_CLIENT = createHttpClient();
try {
    String url = params.url
    if (url == null || "".equals(url)) {
        url = _conf("url", true)
    }
    String issueKey = params.key
    String projectKey = params.projectKey
    if (projectKey == null || "".equals(projectKey)) {
        projectKey = _conf("projectKey", true)
    }
    String issueTypeName = params.issueTypeName
    if (issueTypeName == null || "".equals(issueTypeName)) {
        issueTypeName = _conf("issueType", true)
    }


    String username = params.username
    String password = params.password

    if (username == null || "".equals(username)) {
        username = _conf("username", true)
    }
    if (password == null || "".equals(password)) {
        password = _conf("password", true)
    }

    Map contentTypeHeader = [:]
    contentTypeHeader[HttpHeaders.CONTENT_TYPE] = "application/json"
    def authString = (username + ":" + password).getBytes().encodeBase64().toString()
    contentTypeHeader[HttpHeaders.AUTHORIZATION] = "Basic ${authString}".toString()
    contentTypeHeader[HttpHeaders.ACCEPT_LANGUAGE] = "application/json"

    def contentParams = [:]
    def fields = [:]
    def project = [:]
    def issuetype = [:]
    def transitions = [:]
    def resolution = [:]
    def customfield = [:]


    String resultUrl = url + "/rest/api/2/issue"
    if (mappedAction == "addCommentToIssue") {
        contentParams.put("body", params.body)
        resultUrl += "/" + issueKey + "/comment"
    } else if (mappedAction == "createIssue") {
        issuetype.put("name", issueTypeName)
        project.put("key", projectKey)
        fields.put("project", project)
        fields.put("issuetype", issuetype)
        fields.put("summary", params.summary)
        fields.put("description", params.description)
        String toLabel = "ogAlias:" + params.alias
        //fields.put("labels", Collections.singletonList(toLabel.replaceAll("\\s", "")))

        customfield.put("value","Test")
        fields.put("customfield_10714",customfield)


        def components = [:]
        components.put("name","Monitoring \\ Reports Async")
        logger.debug("components ${components}")
        def set = ["set":components]
        contentParams.put("components", set)





        contentParams.put("fields", fields) 

The error I am facing is: ERROR: [createIssue]: Could not execute at Jira; response: 400 {"errorMessages":[],"errors":{"components":"Component/s is required."}}

Would appreciate if someone can assist how to set component field on creation

4

1 回答 1

2

根据 JIRA 的REST API 文档,您应该将组件列表放入字段映射中。此外,根据文档,JIRA REST API 仅支持有效负载中的组件 ID。您应该先检索组件 ID/s,然后您可以像这样使用它

def components = [
    ["id": "my_component_id_1"]
    ["id": "my_component_id_2"]
]

fields.put("components", components)

如果您还有其他问题,请告诉我。

于 2018-04-17T10:56:33.060 回答