Can someone please validate my script for updating subtask fix version ?

Aisha M
Contributor
May 15, 2018

I am looking at the option of using Script listener to update the sub task when the Fix version of the Parent is updated. Below is the script for it, my only problem with the script is, it does not copy the Fix Version of the parent when creating a sub task, but when I change the Fix version of parent later, then it works and reflects on the Sub Task.

EVENT - Issue created, Issue updated

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version

IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)

Issue updatedIssue = event.getIssue()
Collection<Version> fixVersions = new ArrayList<Version>()
fixVersions = updatedIssue.getFixVersions()
Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
subTasks.each {
if (it instanceof MutableIssue) {
((MutableIssue) it).setFixVersions(fixVersions)
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
}
}

 

Can someone please help :) Thank you

2 answers

0 votes
Tanu
Contributor
January 16, 2020

@Aisha M  did the above script worked ?

0 votes
Alexey Matveev
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.
May 15, 2018

It works correctly. In the script you look for subtasks, that is why your script works only for the parent task. Your scripts for the create subtask and update parent task must be different. For the create event you should get the parent of the issue, get the fix version and save it to the event issue.

Aisha M
Contributor
May 15, 2018

@Alexey Matveev Hello. Thanks for the comment. So, I need to create another listener to copy the Fix Version of parent when a sub task is being created ? 

Alexey Matveev
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.
May 15, 2018

You can either create another listener or use the same one. But you need to define in the listener whether the event issue is a sub-task or the parent task and then code different logic.

For parent task you would iterate over sub-tasks and set the fix version for the sub-tasks (that is what your script does right now)

For subtasks you would find the parent task, take the fix version and set the fix version for the sub-task.

Aisha M
Contributor
May 15, 2018

@Alexey Matveev Thank you. I will try adding another listener and see how it goes :) 

Aisha M
Contributor
May 23, 2018

@Alexey Matveev

I came across the below script for a similar scenario. Would this work for my requirement. (Not from a developmental background, so had trouble writing one myself)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

if(!event.issue.isSubTask() && !event.issue.getSubTaskObjects()){
    return
}

def customFieldManager = ComponentAccessor.customFieldManager

if(event.issue.isSubTask()) {

    def parent = event.issue.getParentObject()
    def status = parent.status.name

    def parentStatus = customFieldManager.getCustomFieldObjectByName("Fix Version/s")
    def changeHolder = new DefaultIssueChangeHolder()

    parentStatus.updateValue(null, event.issue, new ModifiedValue(parentStatus.getValue(event.issue), status), changeHolder)
}

if(event.issue.getSubTaskObjects()){
    def subtasks = event.issue.getSubTaskObjects()
    def status = event.issue.status.name
    subtasks.each {
        def parentStatus = customFieldManager.getCustomFieldObjectByName("Fix Version/s")
        def changeHolder = new DefaultIssueChangeHolder()

        parentStatus.updateValue(null, it, new ModifiedValue(parentStatus.getValue(it), status), changeHolder)
    }

}

Alexey Matveev
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.
May 23, 2018

Try to log info into the atlassian-jira.log and have a look if it works as you expect. You can log with log.error("variable: " + variable)

Aisha M
Contributor
May 24, 2018

@Alexey Matveev Thank you. That script did not work. 

Aisha M
Contributor
May 28, 2018

@Alexey Matveev Hello Alexey, can you please validate the below script, (I am so sorry for troubling you so much)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Fix Version")

//def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fixVersion")

def parentMyFieldValue = issue.parentObject.getCustomFieldValue(field)
def changeHolder = new DefaultIssueChangeHolder();
field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), parentMyFieldValue),changeHolder);

Suggest an answer

Log in or Sign up to answer