1

I'm trying to create an issue, issuelink and copy attachment from triggered issue at the same time using scriptrunner.
For now, the code below are able to create issue and attachment, but I can not link the issue I created, does someone ever deal with this situation?

import org.apache.http.entity.ContentType;

def issueKey = issue.key
def result = get('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .asObject(Map)

def projectkey = "PILOTLV"
if (result.body.fields.customfield_10078.id == "10124"){
projectkey = "SPCLRQ"
} 

def issuetypekey = "10018"
def ticketno = result.body.fields.customfield_10060
if (result.body.fields.issuetype.id == "10015"){
issuetypekey = "10017"
ticketno = result.body.fields.customfield_10059
}

def description = result.body.fields.description
def summary = result.body.fields.summary
def sysname = result.body.fields.customfield_10078.id

logger.info(description)
logger.info(summary)
logger.info(sysname)
logger.info(ticketno)

// create issue 
def createReq = Unirest.post("/rest/api/2/issue")
        .header("Content-Type", "application/json")
        .body([
        fields: [
        summary : summary,
        description: description,
        customfield_10078: [
        id: sysname
                  ],
        customfield_10060: ticketno,
        project : [
        key: projectkey
                  ],
        issuetype : [
        id: issuetypekey
                    ]
                ],
        update: [
        issuelinks: [
            add: [
                type:[
                    name: "Blocks",
                    inward: "is blocked by",
                    outward: "blocks"
                    ],
                outwardIssue: [
                    key: issuekey
                    ]
                ]
            ]
        ]
              ])
        .asObject(Map)

assert createReq.status >= 200 && createReq.status < 300

def clonedIssue = createReq.body

// copy attachments

if (issue.fields.attachment) {
    issue.fields.attachment.collect { attachment ->
        def url = attachment.content as String
        url = url.substring(url.indexOf("/secure"))
        def fileBody = Unirest.get("${url}").asBinary().body
        def resp = Unirest.post("/rest/api/2/issue/${clonedIssue.id}/attachments")
                .header("X-Atlassian-Token", "no-check")
                .field("file", fileBody, ContentType.create(attachment['mimeType'] as String), attachment['filename'] as String)
                .asObject(List)
        assert resp.status >=200 && resp.status < 300
    }
}

And there is a minor question, I found that the attachment name on new issue can not display Chinese character https://community.atlassian.com/t5/Jira-questions/rest-api-3-issue-issue-key-attachments-upload-file-with-a/qaq-p/1070389\ Looks like I'm missing library

Carlos
  • 11
  • 1

1 Answers1

0

Simply put, you can't create and link an issue via the REST API at the same time. You have to create the issue first, then link the issue separately.

David Bakkers
  • 453
  • 3
  • 13