Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,364,036
Community Members
 
Community Events
168
Community Groups

How do I remove a component value from a jira issue using Scriptrunner listener?

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!

1 answer

1 accepted

0 votes
Answer accepted

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

After this, I wanted to remove the Clone_Epic label from the cloned epics to avoid duplications when updates are made on the newly created Epics. So, I added another listener at the Issue_Created Event and I got my desired end result. Here is the code here:
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)

Suggest an answer

Log in or Sign up to answer