Check if a transition is available from groovy script

I have this script to automaticly promote sub-tasks

Map doScript(Map params) {

        MutableIssue issue = params['issue'] as MutableIssue

        User currentUser = componentManager.getJiraAuthenticationContext().getUser()

        WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );

         

        SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();

        Collection<MutableIssue> subTasks = issue.getSubTaskObjects()

        if (subTaskManager.subTasksEnabled && !subTasks.empty) {

            subTasks.each {

                it ->

                log.debug ("issue.statusObject.name: " + issue.statusObject.name)

                workflowTransitionUtil.setIssue(it);

                workflowTransitionUtil.setUsername(currentUser.name);

                workflowTransitionUtil.setAction (761) 

         

                // validate and transition issue

                if (workflowTransitionUtil.validate()) {

                    log.debug("Ready To Start subtask " + it.key)

                    workflowTransitionUtil.progress();

                }

            }

        }

        return params

    }

How can I check if the transition 761 exists before using workflowTransitionUtil.setAction (761) ?

1 answer

1 accepted

Use:

def actionExists = com.atlassian.jira.component.ComponentAccessor.getWorkflowManager().getWorkflow(it).getDescriptor().getAction(761)  == null

You can also use com.atlassian.jira.workflow.IssueWorkflowManager.isValidAction(Issue, int)

Thanks Boris, I changes the code as you suggested

def actionId = 4 
IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
        MutableIssue issue = issueManager.getIssueObject("ICTST-410");
        User currentUser = componentManager.getJiraAuthenticationContext().getUser()
        WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
        SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
        Collection<MutableIssue> subTasks = issue.getSubTaskObjects()
        if (subTaskManager.subTasksEnabled && !subTasks.empty) {
            subTasks.each {
                it ->
                log.debug ("issue.statusObject.name: " + issue.statusObject.name)
                if (com.atlassian.jira.workflow.IssueWorkflowManager.isValidAction(it, actionId)){
                     workflowTransitionUtil.setIssue(it);
                         workflowTransitionUtil.setUsername(currentUser.name);
                         workflowTransitionUtil.setAction (761) 
                         if (workflowTransitionUtil.validate()) {
                          workflowTransitionUtil.progress();

I tried using the code above but I'm getting the following error :

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.workflow.IssueWorkflowManager.isValidAction() is applicable for argument types: (com.atlassian.jira.issue.IssueImpl, java.lang.Integer) values: [ICTST-411, 4]

I guess that the problem is that "it" is not IssueImpl .. how can I fix it?

you've got a mistake - You should use

com.atlassian.jira.component.ComponentAccessor.getComponent(com.atlassian.jira.workflow.IssueWorkflowManager.class).isValidAction(it,actionId)

Thanks Boris ... Thant did the trick.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

176 views 0 4
Read article

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