Creating an issue via rest api with scriptrunner inheriting information from another issue

Dante Labate December 13, 2022

I need help.

I already have the pendency creation script through the rest api... my problem is that I use another pending issue as a basis for field information (I copy the fields from this parent pending issue to the new pending issue created via api)...

From what I've noticed, if the parent issue description field has a line break, the issue is not created via the api.

 

 

 

How can I make it so that during the creation of the problem by the rest api the description field (even with a line break) is accepted in the execution and the problem is created.

 

 

import  com.atlassian.jira.component.ComponentAccessor 

def customFieldManager = ComponentAccessor .getCustomFieldManager()

def projectManager = ComponentAccessor .getProjectManager()

def baseurl = com.atlassian.jira.component.ComponentAccessor .getApplicationProperties().getString( "jira.baseurl" )

def Project_Key = ""

def atom_team_value = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject( "customfield_11912" )) as List

def IssueType_Name ="Task" //issue.getIssueType().name

def channel_value = "Cross" //issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10500"))

def summary_value = issue.getSummary()

def description_value = issue.getDescription()

def priority_value = issue.getPriority().getId()

Project_Key = "ATT"

def authString = "USER:PASS" .getBytes().encodeBase64().toString()

def body_req = """{

"campos":{

"projeto ":{ "chave" : "${ Project_Key }"}

, "issuetype":{ "name" : " ${ IssueType_Name } "}

, "summary":" ${ summary_value } "

, "description": "${description_value}"

, "customfield_10500": {"value" : " ${ channel_value } "}

, "priority":{ "id" : " ${ priority_value } "}

},

"update":{

"issuelinks":[

{

"add":{

"type":{

"name ":"Orquestrador",

"inward":"Atividade",

"outward":"Orquestrador"

},

"outwardIssue":{ "key":" ${ issue.key } " }

}

}

]

}

}"""

def connection = new URL ( " ${ baseurl } /rest/api/2/issue/" ).openConnection() as HttpURLConnection

connection.setRequestMethod( "POST" )

connection.setRequestProperty( "Autorização " , "Basic ${ authString } " )

connection.doOutput = true

connection.setRequestProperty( "Content-Type" , "application/json;charset=UTF-8" )

connection.getOutputStream().write(body_req.getBytes( "UTF-8" ))

connection.connect()

def postRC = connection.getResponseCode();

println (postRC);

if (postRC.equals( 200 )) {

println (connection.getInputStream().getText());

}

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 13, 2022

Why are you using the REST api to create the issue?

You're already in the java layer, why don't you create the issue using the Java api?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkTypeManager

def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager

Issue issue

def iip = issueService.newIssueInputParameters()
iip.setApplyDefaultValuesWhenParameterNotProvided(true)
iip.setSkipScreenCheck(true)

def issueType = ComponentAccessor.constantsManager.getIssueType('Task')
iip.projectId = projectManager.getProjectObjByKey('ATT').id
iip.issueTypeId = issueType.id
iip.priorityId = issue.priority.id
iip.summary = issue.summary
iip.description = issue.description
iip.addCustomFieldValue 'customfield_10500', 'Cross'

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def validationResult = issueService.validateCreate(currentUser, iip)
if(!validationResult.valid){
log.error "Error validating issue input parameters: $validationResult.errorCollection"
return
}
def creationResult = issueService.create(currentUser, validationResult)
if(!creationResult.valid){
log.error "Error creating issue: $creationResult.errorCollection"
return
}
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def linkType =issueLinkTypeManager.getIssueLinkTypesByName('Orquestrador')[0]
issueLinkManager.createIssueLink(issue.id, creationResult.issue.id, linkType.id, 0, currentUser)
Dante Labate December 15, 2022

hi   @Peter-Dave Sheehan 

Sorry for the delay, I didn't think you would respond so quickly here!!

I hadn't thought of that...

It really could be done that way.

Would you know how I would have to insert more than one value in the customfield_10500 field (it being a checkbox)

I believe you have to use the ID instead of the value, but I don't know how it would look when you have more than one option selected

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2022

Correct, if the custom field is a checkbox, you have to find the option and add the id (as a string). 

It is possible to have multiple.

For example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.link.IssueLinkTypeManager

def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager

Issue issue

def iip = issueService.newIssueInputParameters()
iip.setApplyDefaultValuesWhenParameterNotProvided(true)
iip.setSkipScreenCheck(true)

def issueType = ComponentAccessor.constantsManager.getIssueType('Task')
def project = projectManager.getProjectObjByKey('ATT')
iip.projectId = project.id
iip.issueTypeId = issueType.id
iip.priorityId = issue.priority.id
iip.summary = issue.summary
iip.description = issue.description

def optionValueList = ['Cross', 'Banana']
def cfObject = ComponentAccessor.customFieldManager.getCustomFieldObject('customfield_10500')
def config = cfObject.getRelevantConfig(new IssueContextImpl(project,issueType) )
def options = ComponentAccessor.optionsManager.getOptions(config).findAll{it.value in optionValueList}

iip.addCustomFieldValue 'customfield_10500', options.collect{it.optionId as String} as String[]

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def validationResult = issueService.validateCreate(currentUser, iip)
if(!validationResult.valid){
log.error "Error validating issue input parameters: $validationResult.errorCollection"
return
}
def creationResult = issueService.create(currentUser, validationResult)
if(!creationResult.valid){
log.error "Error creating issue: $creationResult.errorCollection"
return
}
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def linkType =issueLinkTypeManager.getIssueLinkTypesByName('Orquestrador')[0]
issueLinkManager.createIssueLink(issue.id, creationResult.issue.id, linkType.id, 0, currentUser)

Like Dante Labate likes this
TAGS
AUG Leaders

Atlassian Community Events