We are trying to create a Custom Clone REST Endpoint Error - "Error occurred while creating issue."

Krishnanand Nayak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2019

We are trying to create a custom clone functionality using the SR4J Web Item and REST Endpoint. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import groovy.transform.BaseScript
import groovy.transform.Field
import com.atlassian.jira.issue.fields.CustomField
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import static com.onresolve.scriptrunner.canned.util.OutputFormatter.markupBuilder

@BaseScript CustomEndpointDelegate delegate
@Field String payload = "<script>location.reload();</script>"

cloneTest(httpMethod: "GET") { MultivaluedMap queryParams, String body ->
def issueId = queryParams.getFirst("issueId") as String
try {
if (!issueId || issueId == "" || issueId == null){
throw new Exception("Field issuekey Required")
}
def issueManager = ComponentAccessor.issueManager
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory
def constantsManager = ComponentAccessor.constantsManager

def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfModule = customFieldManager.getCustomFieldObjectByName("Module(s)")
CustomField cfTestType = customFieldManager.getCustomFieldObjectByName("Test Type")
CustomField cfAutoType = customFieldManager.getCustomFieldObjectByName("Automation Type")
CustomField cfSteps = customFieldManager.getCustomFieldObjectByName("Manual Test Steps")
CustomField cfGeneric = customFieldManager.getCustomFieldObjectByName("Generic Test Definition")

//Get the logged in user, this is the user that will be creating the new subtask.
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

//Get test to be cloned
def issue = issueManager.getIssueObject(issueId)

//Poject where test will be cloned
def targetProject = projectManager.getProjectByCurrentKey("TESTS")

//Summary of cloned test case
def issueSummary = "Cloned - ${issue.summary}"

//This creates an empty test
def clonedtest = issueFactory.issue

//Set the summary and the target project
clonedtest.setSummary(issueSummary)
clonedtest.setProjectObject(targetProject)
clonedtest.setDescription(issue.getDescription())
clonedtest.setComponent(issue.components)
clonedtest.setPriority(issue.priority)

def moduleValue = issue.getCustomFieldValue(cfModule)
clonedtest.setCustomFieldValue(cfModule,moduleValue)

def testTypeValue = issue.getCustomFieldValue(cfTestType) as String
clonedtest.setCustomFieldValue(cfTestType,testTypeValue)
log.warn("Test Type : ${testTypeValue}")
log.warn("Step 7b")

def autoTypeValue = issue.getCustomFieldValue(cfAutoType)
clonedtest.setCustomFieldValue(cfAutoType,autoTypeValue)
log.warn("Automation Type : ${autoTypeValue}")

def stepsValue =""

if(testTypeValue.trim() == "Generic"){
stepsValue = issue.getCustomFieldValue(cfGeneric)
log.warn("Generic Test Definition : ${stepsValue}")
}
else{
stepsValue = issue.getCustomFieldValue(cfSteps)
log.warn("Manual Test Steps : ${stepsValue}")
}

clonedtest.setCustomFieldValue(cfGeneric,stepsValue)

//You need a subtask type, and this cannot be null.
def issueType = constantsManager.allIssueTypeObjects.find{it.name in ["Test"]}?.id
if (!issueType) {
return "Your issue type does not exist, you need a test issue type to create a cloned test!"
}
clonedtest.setIssueTypeId(issueType)

//Parse clone test issue params
Map<String, Object> newIssueParams = [issue:clonedtest] as Map<String, Object>
issueManager.createIssueObject(currentUser, newIssueParams)
Response.ok(payload).build()
}
catch (Exception e) {
return Response.serverError().entity([error: e.message]).build()
}
}

 

URL : https://myjira.com/rest/scriptrunner/latest/custom/cloneTest/?issueId=TESTS-33220

This code gives the following error when executed - "

{"error":"Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB"}

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events