I am wondering if anyone has any code or gotten code to work through a scriptrunner listener to remove a particular component value on an Issue Created event.
I created an Issue Updated event listener to clone an epic with its stories. The trigger will be to add a Component of Clone Epic. Well I want to create another listener based on Issue Created event to remove the Clone Epic component from the newly created Epics.
Any advice is welcome.
Thank you in advanced!
I was able to accomplish this by using labels and creating two behaviors. First Behavior looks at the Issue_Updated Event and adding the label Clone_Epic. Upon adding this label, it clones the epic with the stories and subtasks.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.*
import com.atlassian.jira.event.*
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.label.*
import org.apache.log4j.Logger
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueFactory = ComponentAccessor.issueFactory
def subtaskManager = ComponentAccessor.subTaskManager
//def issue = issueManager.getIssueByCurrentKey('CPSPLAT-1698')
Issue issue = event.issue
def changeHolder = new DefaultIssueChangeHolder()
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager);
def labels = labelManager.getLabels(issue.id) as String[]
def value = labels.find {it == /Clone_Epic/}
//Create a clone of an issue
static Issue clonedIssue(Issue issue, ApplicationUser loggedInUser, IssueFactory issueFactory, IssueManager issueManager) {
def cloneIssueFactory = issueFactory.cloneIssue(issue)
cloneIssueFactory.setCreated( new Date(System.currentTimeMillis()).toTimestamp() )
def newIssueParams = ['issue': cloneIssueFactory] as Map<String, Object>
issueManager.createIssueObject(loggedInUser, newIssueParams)
}
def epic
if (issue.issueType.name == 'Epic' && value) {
//Cloning of the Epic
def epicClone = clonedIssue(issue, loggedInUser, issueFactory, issueManager)
epic = issueManager.getIssueByCurrentKey(epicClone.key)
def epicName = customFieldManager.getCustomFieldObjectsByName('Epic Name').first()
def originalEpicName = issue.getCustomFieldValue(epicName)
epicName.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(epicName), "${originalEpicName} Clone".toString()), changeHolder)
issueManager.updateIssue(loggedInUser, epic, EventDispatchOption.ISSUE_UPDATED, false)
//Extraction of Original Issue(s) from Original Epic
def links = issueLinkManager.getOutwardLinks(issue.id)
def originalIssuesInEpic= links.findAll { it.issueLinkType.name == 'Epic-Story Link' }*.destinationObject
//Cloning of Original Issue(s) from the Original Epic and Adding it to the Cloned Epic
originalIssuesInEpic.each {
def clonedIssue = clonedIssue(it, loggedInUser, issueFactory, issueManager)
def subtasks = it.subTaskObjects
if (subtasks) {
subtasks.each {subtask ->
def mutableSubtask = subtask as MutableIssue
def toClone = issueFactory.cloneIssue(mutableSubtask)
mutableSubtask.setCreated(new Date(System.currentTimeMillis()).toTimestamp())
def cloned = issueManager.createIssueObject(clonedIssue.reporter, toClone)
subtaskManager.createSubTaskIssueLink(clonedIssue, cloned, clonedIssue.reporter)
log.warn(subtask.key)
}
}
def targetField = customFieldManager.getCustomFieldObjects(clonedIssue).findByName('Epic Link')
targetField.updateValue(null, clonedIssue, new ModifiedValue(clonedIssue.getCustomFieldValue(targetField), epic), changeHolder)
}
}
return epic
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*
import com.atlassian.jira.event.*
import com.atlassian.jira.issue.label.LabelManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
Issue issue = event.issue
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
labels -= 'Clone_Epic'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.