Scott C Wilson
Scott C Wilson

Reputation: 20036

Update JIRA subtask Fix Version when parent issue Fix Version updated

When an issue is created, the Fix Version field is set to a particular value (say 2.0). Then subtasks are created, and they inherit this value. So far so good. But if later, the issue's Fix Version value is modified (to say 1.0), the subtasks still keep the 2.0 fix version value.

Is there a plugin or technique I can use to keep these fields in sync?

Note: This has been requested as a JIRA feature, but Atlassian doesn't seem to want to do it. https://jira.atlassian.com/browse/JRA-9016

Upvotes: 2

Views: 6921

Answers (3)

kivagant
kivagant

Reputation: 1967

For Jira 6.4

Your subtasks fixversion fields will be update automatically.

  1. Install ScriptRunner plugin.
  2. OPEN your-jira.host/secure/admin/ViewCustomFields.jspa
  3. ADD the new custom field with (Advanced>Scripted field)
  4. OPEN /plugins/servlet/scriptrunner/builtin?section=script_fields and find new field there.
  5. ADD this script, test it and save.

    import com.atlassian.jira.component.ComponentAccessor;
    import com.atlassian.jira.config.SubTaskManager;
    import com.atlassian.jira.issue.IssueManager;
    import com.atlassian.jira.event.type.EventDispatchOption;
    
    def result = "";
    def subTasks = issue.getSubTaskObjects()
    Collection fixVersions = issue.getFixVersions();
    if (!fixVersions.empty) {
        SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();
        if(subTaskManager.subTasksEnabled && !subTasks.empty) {
        def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
            subTasks.each {
                if (it.statusObject.name != "Closed") {            
                    it.setFixVersions(fixVersions);
                    IssueManager issueManager = ComponentAccessor.getIssueManager()
                    result += it.getKey() + " ";
                    issueManager.updateIssue(user, it, EventDispatchOption.ISSUE_UPDATED, false);
                }
            }
            if (count > 0) {
                result += " versions updated";
            }
        }
    }
    return result;
    

Upvotes: 1

Kixer
Kixer

Reputation: 41

I know its rather an old question. But here is my code I recently deployed. This is an event listener for issueUpdated event deployed on Script Listener from ScriptRunner plugin. A lot of the code is from Jamie Echlin's examples. It still needs to be tweaked for when a Fix Version field on the Parent is made "empty", it's sub tasks also need to be empty.

package com.custom.listeners

import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.version.Version
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import java.util.ArrayList
import java.util.Collection

class FixVersionPBI_To_SBI_1 extends AbstractIssueEventListener 
{

  Category log = Category.getInstance(FixVersionPBI_To_SBI_1.class)

  @Override
  void issueUpdated(IssueEvent event) 
  {

   try
   {
    Issue fix_Issue = event.getIssue()
    if (fix_Issue.issueTypeObject.name == "Parent issue type" )
    {
      List changeItems = event.getChangeLog().getRelated("ChildChangeItem")     
  if( fix_Issue.fixVersions?.name  && changeItems.any {it.get('field')=='Fix Version'} )
      {
        Collection<Version> fixVersions = new ArrayList<Version>();
    fixVersions = fix_Issue.getFixVersions()
    Collection subTasks = fix_Issue.getSubTasks();
    SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();

    if (subTaskManager.subTasksEnabled && !subTasks.empty) 
        {
          IssueManager issueManager = ComponentManager.getInstance().getIssueManager()
          Collection _subTasks = fix_Issue.getSubTaskObjects()
          _subTasks.each 
          {
            it.setFixVersions(fixVersions)
            issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
          }                     
        }
      }
    }
  }
  catch (ex)
  {
   log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by  FixVersionPBI_To_SBI_1"
   log.debug (ex.getMessage())
  }
 }
}

Upvotes: 4

mdoar
mdoar

Reputation: 6891

To do it manually, you could do query such as "parent=TEST-123" and then a bulk edit. To do it automatically you would need to have a custom listener (e.g. Script Runner) to detect the Issue Updated event and only do something if the change was an issue update. Updating the subtask Fix Versions will also require that the subtasks are reindexed or your searches will not work as expected.

I don't know of a plugin to do this.

Upvotes: 0

Related Questions