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
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

412 views 0 3
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