Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Jira cloud REST create issue and issuelink

Edited
imran I'm New Here Nov 27, 2021

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/Olansi Looks like I'm missing library

1 answer

0 votes
Hana Kučerová Community Leader Nov 28, 2021

Hi @imran ,

welcome to the Atlassian community!

I believe the link creation needs to be the second step - first of all you create a new issue and then you send another request for linking the issues together.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you