Hello,
Can someone please help me in fixing this error?
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.user.UserManager
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.Response
import org.apache.log4j.Category
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.config.properties.APKeys
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import java.sql.Timestamp;
import org.apache.log4j.Logger;
@BaseScript CustomEndpointDelegate delegate
cloneEpicWithIssues(httpMethod: "GET") { MultivaluedMap queryParams,body, HttpServletRequest request ->
// Variable definition
def issueKey = queryParams.getFirst("issueKey") as String // issuekey from request parameters
def newSummary = queryParams.getFirst("newSum") as String // newSummary value from request parameters
def epic_link_field="customfield_12739";
def epic_name_field="customfield_12738";
def issueMgr = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def linkMgr = ComponentAccessor.getIssueLinkManager()
def issueFactory = ComponentAccessor.getIssueFactory()
Issue issue = issueMgr.getIssueObject(issueKey)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Timestamp Ts = new Timestamp(System.currentTimeMillis());
CustomField epicLink = customFieldManager.getCustomFieldObject(epic_link_field);
CustomField epicName = customFieldManager.getCustomFieldObject(epic_name_field);
//Get current issues in epic -> links from and to source epic
List<IssueLink> outwardLinks = linkMgr.getOutwardLinks(issue.getId())
List<IssueLink> inwardLinks = linkMgr.getInwardLinks(issue.getId())
List<Issue> outwardLinkedIssues = [] as List<Issue>;
List<Issue> inwardLinkedIssues = [] as List<Issue>;
outwardLinks.each{
outwardLinkedIssues.add(it.getDestinationObject())
}
inwardLinks.each{
inwardLinkedIssues.add(it.getSourceObject())
}
//sorting of epic links - not neccessary, was requirement from users
outwardLinkedIssues.sort{a,b -> a.getId()<=>b.getId()}
outwardLinkedIssues = outwardLinkedIssues.reverse()
inwardLinkedIssues.sort{a,b -> a.getId()<=>b.getId()}
inwardLinkedIssues = inwardLinkedIssues.reverse();
ModifiedValue mVal;
MutableIssue clonedIssue;
MutableIssue clonedEpic;
try {
//Clone epic with all fields
def toClone = issueFactory.cloneIssueWithAllFields(issue)
clonedEpic = issueMgr.createIssueObject(issue.reporter, toClone) as MutableIssue
//set current system time as created date, otherwise it would have created date from source epic
//issue.store() is deprecated but issueManager.updateIssue didn't work, don't know why
clonedEpic.setCreated(Ts)
clonedEpic.store();
//Set new summary and epic name (same in this case)
clonedEpic.setSummary(newSummary)
clonedEpic.store();
mVal = new ModifiedValue(clonedEpic.getCustomFieldValue(epicName), newSummary);
epicName.updateValue(null, clonedEpic, mVal, new DefaultIssueChangeHolder());
//if source epic has outward links, clone each linked issue and set epic link to new epic
//For sub-tasks, this is unfortunately not working. Therefore they are excluded
if(!outwardLinks.isEmpty()){
outwardLinkedIssues.each{
if(it.issueType.name!="Sub-task"){
toClone=issueFactory.cloneIssueWithAllFields(it)
clonedIssue = issueMgr.createIssueObject(issue.reporter, toClone) as MutableIssue
clonedIssue.setCreated(Ts)
clonedIssue.store();
mVal = new ModifiedValue(clonedIssue.getCustomFieldValue(epicLink), clonedEpic);
epicLink.updateValue(null, clonedIssue, mVal, new DefaultIssueChangeHolder());
}
}
}
if(!inwardLinks.isEmpty()){
inwardLinkedIssues.each{
if(it.issueType.name!="Sub-task"){
toClone=issueFactory.cloneIssueWithAllFields(it)
clonedIssue = issueMgr.createIssueObject(issue.reporter, toClone) as MutableIssue
clonedIssue.setCreated(Ts)
clonedIssue.store();
mVal = new ModifiedValue(clonedIssue.getCustomFieldValue(epicLink), clonedEpic);
epicLink.updateValue(null, clonedIssue, mVal, new DefaultIssueChangeHolder());
}
}
}
}catch(e){
return Response.serverError().entity([error: e.message]).build();
}
String BASE_URL = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL);
//show success message and load new epic - doesn't work in some cases, don't know why
String clonedMessage = """
Navigating to created issue <a href="${BASE_URL}/browse/${clonedEpic.getKey()}">${clonedEpic.getKey()}</a>...
<script>
function loadUrl(newLocation){
window.location.href = newLocation;
};
loadUrl('${BASE_URL}/browse/${clonedEpic.getKey()}');
</script>
"""
UserMessageUtil.success(clonedMessage)
}
Error message :
2019-01-25 10:36:38,866 ERROR [common.UserCustomScriptEndpoint]: ************************************************************************************* 2019-01-25 10:36:38,873 ERROR [common.UserCustomScriptEndpoint]: Script endpoint failed on method: GET cloneEpicWithIssues java.lang.NullPointerException: Cannot invoke method getId() on null object at Script5$_run_closure1.doCall(Script5.groovy:48) at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:375) at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.getUserEndpoint(UserCustomScriptEndpoint.groovy:256)
Look at line 48 (the error message gives you the line on which it failed). That line has a getId() call on the object "issue", but that object does not exist (that's what the "null object" part of the error means).
So look at where that should be being created, because that line is not finding the issue.
Hello,
Thanks for your reply.
I'm new to scriptrunner. Could you please let me know how to create object?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
By getting it from somewhere - look to where "issue" is defined in your code - that is going wrong.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.