Hi,
I’m writing a script that is used in a post function, the script should clone an issue into an epic and if the issue had sub tasks clone each of them into a story.
I got it all working except for one thing, setting the link between the Epic and the stories.
I looked at many posts and now I’m at the stage that I can create such a link when I do my test on an epic and story that already exist from the script console. However this doesn’t work when I do this in the post function. If I use an existing Story instead of the one cloned in the script, it works.
I tried to use ‘doAfterCreate’ but when I try this the code inside this section doesn’t run. I also get a warning : the variable [doAfterCreate] is undeclared.
I’m using Jira version 7.2.8
Script runner version 5.1.0
Any help will be appreciated.
Thanks,
Alon
Here is the script:
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.util.ImportUtils
import com.atlassian.crowd.embedded.api.User;
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.project.Project
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.link.IssueLinkTypeManager;
import com.atlassian.jira.workflow.TransitionOptions
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
// log = Category.getInstance("com.onresolve.jira.groovy.CreateDependentIssue")
// to run in script console on specifi issue - use:
def issueManager = ComponentAccessor.getIssueManager()
Issue newReq = issueManager.getIssueObject("A-12")
// check if sub tasks exist
newReq.subTaskObjects.size() >= 1
// clone the new req to Epic + create stories from sub tasks
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
MutableIssue newEpic = issueFactory.getIssue()
newEpic.setSummary ("Epic created from new Requierement " + newReq.summary)
newEpic.setParentObject(newReq)
newEpic.setProjectObject(newReq.getProjectObject())
newEpic.setIssueTypeId("10100") //10100 == Epic
newEpic.description = newReq.description
newEpic.reporter = newReq.getReporter()
newEpic.assignee = newReq.getAssignee()
def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Map<String,Object> newIssueParams = ["issue" : newEpic] as Map<String,Object>
issueManager.createIssueObject(currentUserObj, newIssueParams)
// create link between the new Req and the Epic
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def issueLinkManager = ComponentAccessor.getIssueLinkManager();
def duplicateLinkTypeName = "Create"
def duplicateLinkType = issueLinkTypeManager.getIssueLinkTypesByName(duplicateLinkTypeName)
if (!duplicateLinkType) {
log.warn ("Issue link type with name: ${duplicateLinkTypeName} does not exist")
}
else {
issueLinkManager.createIssueLink(newReq.id, newEpic.id, duplicateLinkType[0].id, Long.valueOf(1), currentUserObj)
}
// clone the sub tasks into stories
def subtasks = newReq.getSubTaskObjects()
// if there are no subtasks to clone, do nothing
subtasks.each {subtask ->
println "sub taks: " + subtask
MutableIssue newStory = issueFactory.getIssue()
newStory.setSummary ("Story created from new Requierement sub task" + subtask.getSummary())
newStory.setIssueTypeId("10101") //10100 == Story
newStory.description = subtask.getDescription()
newStory.reporter = subtask.getReporter()
newStory.assignee = subtask.getAssignee()
newStory.setParentObject(newReq)
newStory.setProjectObject(newReq.getProjectObject())
Map<String,Object> newStoryParams = ["issue" : newStory] as Map<String,Object>
issueManager.createIssueObject(currentUserObj, newStoryParams)
// link the new stories to the new Epic
println "newStory:" + newStory
// doAfterCreate = {
def epicLinkField = ComponentAccessor.customFieldManager.getCustomFieldObjects(newStory).find{it.name == 'Epic Link'}
if(!epicLinkField) {log.debug "No Epic Link field"; return}
// newEpic.setCustomFieldValue(epicLinkField,newStory)
epicLinkField.updateValue(null, newStory, new ModifiedValue(newStory.getCustomFieldValue(epicLinkField), newEpic),new DefaultIssueChangeHolder())
println "epicLinkField:"+ epicLinkField
println "newEpic:" + newEpic
// }
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.