简体   繁体   English

使用groovy脚本设置Jira组件字段

[英]Set Jira component field using groovy script

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. 我正在尝试使用groovy脚本(在Opsgenie的Marid服务器上创建jira票证。但是,在尝试设置组件字段时遇到了问题。

    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; 我面临的错误是:错误:[createIssue]:无法在Jira执行; response: 400 {"errorMessages":[],"errors":{"components":"Component/s is required."}} 响应:400 {“ errorMessages”:[],“ errors”:{“ components”:“组件是必需的。”}}

Would appreciate if someone can assist how to set component field on creation 如果有人可以协助您在创建时设置组件字段,将不胜感激

According to the JIRA's REST API documentation , you should put the components list into the fields map. 根据JIRA的REST API文档 ,您应该将组件列表放入字段映射中。 Also, JIRA REST API supports only component IDs in the payload according to the doc. 此外,根据文档,JIRA REST API仅支持有效负载中的组件ID。 You should retrieve the component ID/s first, and then you can use it like this 您应该首先检索组件ID,然后才能像这样使用它

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

fields.put("components", components)

Please let me know if you have further questions. 如果您还有其他问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM