Script Runner Transition parent issue based on subtask status

Since the original poster of Script Runner Transition parent issue based on subtask status didn't share his script I have to ask the question again.

I would like to automatically transition a parent issue from the status "Backlog Dev" to "Implementation", if one if its subtasks is transitioned to "Implementation". I started of with the script posted at www.petervandevoorde.com/2014/09/18/automatically-transition-a-parent-issue-from-a-subtask-in-jira/ and modified it a little bit. Since

MutableIssue parent = issue.getParentObject();

shows an error, I changed it to

MutableIssue parent = issue.getParentObject() as MutableIssue

The whole script looks like this:

import com.opensymphony.workflow.WorkflowContext;
import com.atlassian.jira.config.SubTaskManager;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
 
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)
MutableIssue parent = issue.getParentObject() as MutableIssue
 
String originalParentStatus  = parent.getStatus().getSimpleStatus().getName()
def isDevBacklogStatus = originalParentStatus in ['Next Up Dev', 'Backlog Dev', 'Selected for Development']
 
if (isDevBacklogStatus) {
    WorkflowTransitionUtil.setIssue(parent)
    workflowTransitionUtil.setUserkey(currentUser)
    workflowTransitionUtil.setAction(171)
    if (workflowTransitionUtil.validate()) {
        workflowTransitionUtil.progress()
    }        
}

Now the script doesn't show any errors in the edit field, but when triggering the script i get the following error:

2016-02-26 23:28:28,062 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-02-26 23:28:28,093 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: KDEVA-42, actionId: 171, file: <inline script="">
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.workflow.WorkflowTransitionUtil.setIssue() is applicable for argument types: (com.atlassian.jira.issue.IssueImpl) values: [KDEVA-24]
 at Script944.run(Script944.groovy:17)

I'm still new to Groovy, so I can't figure out what I need to change to gt it to work. Can someone help, please?

 

5 answers

1 accepted

This widget could not be displayed.

For me it was failing on: 

String originalParentStatus  = parent.getStatus().getSimpleStatus().getName()

Instead I use:

String originalParentStatus  = parent.status?.name

But the root of your issue is this:

WorkflowTransitionUtil.setIssue(parent)

It should be:

workflowTransitionUtil.setIssue(parent)


correct, sorry I missed this when I posted.

Oh man, "w" instead of "W". Now  feel stupid smile.

Thanks for noticing that. Now the script works.

This widget could not be displayed.

Hi Flamio,

This can be achieved by placing a custom script transition on the workflow transition to the required status which checks if the issue is a sub-task and if it is in the correct status. Please note you need to place this post function as the bottom last function in the list below the  Fire a Generic Event event that can be processed by the listeners post function.

Below I have attached some sample code for this which shows how when a sub-task is transitioned to In Progress that the parent issue gets transitioned to done.

// Required Imports
import com.opensymphony.workflow.WorkflowContext;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.MutableIssue;

// Only fire if the issue is a Sub Task and in the In Progress Status
if(issue.issueTypeObject.name == 'Sub-task' && issue.getStatusObject()?.name == 'In Progress'){
    // Get the details of the user executing the workflow transition for the issue change history
    String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
    WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);

    // Get the ID of the parent issue
    MutableIssue parent = issue.getParentObject() as MutableIssue
    // Update the issue by validating the transition can be completed first before executing it. 
    workflowTransitionUtil.setIssue(parent);
    workflowTransitionUtil.setUserkey(currentUser);
    workflowTransitionUtil.setAction(71); // 71 is the ID of the  Global Done transition on my workflow
    workflowTransitionUtil.validate();
    workflowTransitionUtil.progress();
}

I hope this helps.

Thanks

Kristian

@Kristian Walker (Adaptavist) @Jamie Echlin [Adaptavist] I would like to use this but what if our Sub-task and parent issue type have different workflows? I've tried using this solution but it doesn't seem to work or us. We need to set the status of the parent to In Progress when the Sub-task has been set to In Progress. Our transition ID is the same in both workflows to set the status to In Progress. Would they need to be different in order for this script to work correctly?

so you just need a script on the sub-task workflow. You can either hard-code the action ID or look it up by name from the WorkflowManager).

This is super useful and saves us from having to find another add-on! One question, however. In my testing, I get some warnings that indicate that there are deprecation. I'm using JIRA 7.1.4. 

It says:

since 7.0 use Issue.getStatus() instead. @ line 9, column 48
Please use Issue.getIssueType(). Deprecated since v7.0 @ line 9, column 4.
since 7.0 Use Issue.getStatus() instead. @ line 9, column 48. 

When I make the changes I think I need to make, it errors out on me. Can you please provide an updated version of the script with those warnings in mind? I'd like to get them updated so I don't have to touch them again.

 

Otherwise, works like a charm as long as this post-function is AFTER the post function that sets the status to In Progress for the sub-task.

image2016-4-12 14:12:15.png

The deprecation warnings are correct. If you change it as recommended, it should work in JIRA 8 but may not. If you don't change it, it may not work in JIRA 8 but probably will. So, meh.

Changing it as recommended should work fine though, you need to tell us how it errors out.

So, what I'm hearing is, leave it alone until JIRA 8 is closer to a real thing in our world and revisit. I'll do that and revisit the errors later.

 

Thanks!

 

 

The script above worked well for me, as I mentioned in an earlier comment. However, I created a different version of the script to account for a different transition and status (Dev In Progress instead of In Progress) for a different workflow need. All I changed was the ID of the global transition to the one used for Dev In Progress and it's not working. It says 0 failures when it executes, but it doesn't transition the parent. The payload looks correct as well. 

 

{
    "full.module.key": "com.onresolve.jira.groovy.groovyrunnerrungroovy-function (java.lang.String)",
    "canned-script": "com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction (java.lang.String)",
    "class.name": "com.onresolve.jira.groovy.GroovyFunctionPlugin (java.lang.String)",
    "issue": "ADEMO-58 (com.atlassian.jira.issue.IssueImpl)",
    "passesCondition": "true (java.lang.Boolean)",
    "transientVars": {
        "changeGroup": "[GenericEntity:ChangeGroup][issue,1399798][author,kim][created,2016-05-06 11:05:15.241][id,5636150] (org.ofbiz.core.entity.GenericValue)",
        "issue": "ADEMO-58 (com.atlassian.jira.issue.IssueImpl)",
        "configuration": "com.opensymphony.workflow.config.DefaultConfiguration@5c681d78",
        "proj": "Project: ADEMO (com.atlassian.jira.project.ProjectImpl)",
        "project": "[GenericEntity:Project][name,Agile Demo][assigneetype,3][description,This is a sample Scrum project for JIRA Agile.\r\n<br><br>\r\nSample Sprint 1 started 21 days 10 hours 20 minutes ago ends 7 days 10 hours 20 minutes ago completed 7 days 11 hours 40 minutes ago\r\n<br><br>\r\nSample Sprint 2 started 7 days 9 hours 10 minutes ago ends 6 days 15 hours 10 minutes \r\n<br><br>][projecttype,software][id,22706][counter,59][avatar,10011][originalkey,ADEMO][url,null][lead,kim][key,ADEMO] (org.ofbiz.core.entity.GenericValue)",
        "currentSteps": "[SimpleStep@10[owner=, actionId=91, status=null]] (java.util.ArrayList)",
        "store": "com.opensymphony.workflow.spi.ofbiz.OfbizWorkflowStore@202ef008",
        "descriptor": "com.atlassian.jira.workflow.ImmutableWorkflowDescriptor@7d427c6a",
        "userKey": "kim (java.lang.String)",
        "originalAssigneeId": "null (org.codehaus.groovy.runtime.NullObject)",
        "entry": "com.opensymphony.workflow.spi.SimpleWorkflowEntry@65d31e8f",
        "oldStatus": "com.atlassian.jira.issue.status.StatusImpl@fffca195",
        "context": "com.opensymphony.workflow.basic.BasicWorkflowContext@1e40e426",
        "createdStep": "SimpleStep@10[owner=, actionId=0, status=null] (com.opensymphony.workflow.spi.SimpleStep)",
        "originalissueobject": "ADEMO-58 (com.atlassian.jira.issue.IssueImpl)",
        "actionId": "91 (java.lang.Integer)",
        "pkey": "ADEMO (java.lang.String)",
        "changeItems": "[com.atlassian.jira.issue.history.ChangeItemBean@26770eee[fieldType=custom,field=In Progress Date,from=2016-05-06T10:54:43-0500,fromString=05/06/2016 10:54 AM,to=2016-05-06T11:05:15-0500,toString=05/06/2016 11:05 AM,created=<null>], com.atlassian.jira.issue.history.ChangeItemBean@48388766[fieldType=jira,field=status,from=10048,fromString=Dev In Progress,to=10048,toString=Dev In Progress,created=<null>]] (java.util.LinkedList)"
    },
    "log": "org.apache.log4j.Logger@2c467a85"
}

Check the logs for the reasons why it didn't fail. Expected errors, eg permissions errors, won't show as failures.

On the Execution Information screen, the logs say:

Time (on server): Mon May 09 2016 10:24:44 GMT-0500 (Central Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

No logs were found for this execution.


I don't have any logging statements in my script. I'm not having the best luck getting them added. Can you tell me what I would need to add to this script to make it work to gather logs that would be helpful?

 

// Required Imports
import com.opensymphony.workflow.WorkflowContext;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.MutableIssue;

// Only fire if the issue is a Sub Task and in the In Progress Status
if((issue.issueTypeObject.name == 'Sub-task' || issue.issueTypeObject.name == 'Technical task') && issue.getStatusObject()?.name == 'In Progress'){
// Get the details of the user executing the workflow transition for the issue change history
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);

// Get the ID of the parent issue
MutableIssue parent = issue.getParentObject() as MutableIssue
// Update the issue by validating the transition can be completed first before executing it.
workflowTransitionUtil.setIssue(parent);
workflowTransitionUtil.setUserkey(currentUser);
workflowTransitionUtil.setAction(91); // 91 is the ID of the Global Dev In Progress transition
workflowTransitionUtil.validate();
workflowTransitionUtil.progress();
}

you're better off using the "fast-track" built-in script. It's tricky doing a transition within another transition. If you refresh the issue does it work? How about change it then refresh?

Refreshing doesn't change anything.

 

I looked at the Fast Track option, but it's only pertaining to the issue being transitioned, isn't it? So, if what I'm trying to do is transition the parent of a subtask to "Dev In Progress" when one of the sub-tasks moved to "Dev In Progress" this won't work, will it? It's strange that this seems to be working for the generic "In Progress" transition but not for "Dev In Progress."

oh ok I missed that. Should be fine then. Check the output from .validate() and .progress, eg:

log.warn workflowTransitionUtil.validate()
log.warn workflowTransitionUtil.progress()

then check the log output.

I tried both log.warn and log.info and there's nothing in the logs. I can see where I updated the workflow but nothing is in the logs either on the Execution Information or in atlassian-jira.log after running the operation.

Any thoughts on how to get some more logs out of this? I can open a support ticket if that's better, but I know that the support site guided me to Answers.

Hi Kim,

I would raise a request through the Adaptavist support portal in order to get the developers to investigate this further.

Thanks

Kristian

Sorry Kim, you are in the right place for support here... this is not covered in our support remit. We will get to this question.

If you use this:

log.warn "Validate: " + workflowTransitionUtil.validate()
log.warn "Progress: " + workflowTransitionUtil.progress()

do you at least see those lines?

Sorry, that was definitely my fault. This line: 

if((issue.issueTypeObject.name == 'Sub-task' || issue.issueTypeObject.name == 'Technical task') && issue.getStatusObject()?.name == 'In Progress'){

 

Was still looking for "In Progress" and not "Dev In Progress". So sorry!

Kristian, Can you help me?

I need to update the parent Date/time field with current timestamp when a child task is done.

 

Need: Aging charts , like To track how long it took for testing to being after coding task is done, etc

 

The question is posted here. you can provide answer there.

https://community.atlassian.com/t5/Questions/How-to-update-a-parent-field-when-subtsk-state-changes/...

This widget could not be displayed.

Thanks! Just set this up in our system. Still looks good...   Clean code is:  

"import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.comments.CommentManager;
import com.opensymphony.workflow.WorkflowContext;
import org.apache.log4j.Category;
import com.atlassian.jira.config.SubTaskManager;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.MutableIssue;

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
MutableIssue parent = issue.getParentObject() as MutableIssue

String originalParentStatus = parent.getStatus().getSimpleStatus().getName()
def isDevBacklogStatus = originalParentStatus in ['Open']

if (isDevBacklogStatus) {
workflowTransitionUtil.setIssue(parent)
workflowTransitionUtil.setUserkey(currentUser)
workflowTransitionUtil.setAction(11)
if (workflowTransitionUtil.validate()) {
workflowTransitionUtil.progress()
}
}

"

Luke, Can you help me?

I need to update the parent Date/time field with current timestamp when a child task is done.

 

Need: Aging charts , like To track how long it took for testing to being after coding task is done, etc

 

The question is posted here. you can provide answer there.

https://community.atlassian.com/t5/Questions/How-to-update-a-parent-field-when-subtsk-state-changes/...

This widget could not be displayed.

Try changing 

MutableIssue parent = issue.getParentObject() as MutableIssue

to

def parent = ComponentAccessor.getIssueManager().getIssueObject(issue.getParentId())

Unfortunately I still get the same error

2016-03-01 22:46:11,997 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-03-01 22:46:11,998 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: KDEVA-30, actionId: 171, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.workflow.WorkflowTransitionUtil.setIssue() is applicable for argument types: (com.atlassian.jira.issue.IssueImpl) values: [KDEVA-16]
	at Script997.run(Script997.groovy:17)

Line 17 is

WorkflowTransitionUtil.setIssue(parent)

Boris, Can you help me?

I need to update the parent Date/time field with current timestamp when a child task is done.

 

Need: Aging charts , like To track how long it took for testing to being after coding task is done, etc

 

The question is posted here. you can provide answer there.

https://community.atlassian.com/t5/Questions/How-to-update-a-parent-field-when-subtsk-state-changes/...

This widget could not be displayed.

WorkflowTransitionUtil.setIssue(parent)

should be

workflowTransitionUtil.setIssue(parent)

ie a small w.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

199 views 1 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you