Script Listener - Transition Workflow on Comment

Hello, 

 

Im currently working on a project to transition an issue on comment using script listener "fire generic even" but i cant seem to get the following code working.

 

I've selected the events "Issue Commented" and placed the following code:

 

package com.netic.listener
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.util.JiraUtils;
import com.opensymphony.workflow.WorkflowContext;
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
 
class ExampleListener extends AbstractIssueEventListener {
    ComponentManager componentManager = ComponentManager.getInstance();
    def issueLinkManager = ComponentAccessor.getIssueLinkManager();
    def authContext = componentManager.getJiraAuthenticationContext()
 
    WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
    @Override
    void workflowEvent(IssueEvent event) {
       String issueid     = event.issue.id
       String issuekey    = event.issue.key
       String status      = event.issue.getStatusObject().getName()
       String eventid     = event.getEventTypeId()
       String issuetype   = event.issue.issueTypeObject.name
       String currentuser = event.getUser()
       currentuser = currentuser.split(":")[0]
       Integer transition = 0
       System.out.println("script=Listener.groovy IssueKey=" + issuekey + " action=StartScript")
       System.out.println("script=Listener.groovy IssueKey=" + issuekey + " currentUser=" + currentuser)
       System.out.println("script=Listener.groovy IssueKey=" + issuekey + " eventid=" + eventid)
       System.out.println("script=Listener.groovy IssueKey=" + issuekey + " issuetype=" + issuetype)
       System.out.println("script=Listener.groovy IssueKey=" + issuekey + " status=" + status)
      if (issuetype == "Escalation")
      {
        if (status == "Response") // Email Sent
      {
            transition == "Resolve" // Resolve
          }
      }
      if (transition != 0 )
      {
        // Transist Issue
        workflowTransitionUtil.setIssue(event.issue);
        workflowTransitionUtil.setUsername(currentuser);
        workflowTransitionUtil.setAction (transition)
        // Validate and transition issue
        workflowTransitionUtil.validate();
        workflowTransitionUtil.progress();
        status = event.issue.getStatusObject().getName()
        System.out.println("script=Listener.groovy IssueKey=" + issuekey + " Issue " + issueid + " transited to new status: " + status)
      }
      System.out.println("script=Listener.groovy IssueKey=" + issuekey + " action=EndScript")
   }
}

and place event as "Generic Event", but it doesnt seem to work, when attempting to comment, the comment section just load. 

 

Hopefully someone can debug this would be a massive help. 

 

Many thanks, 

Pon

 

1 answer

Hi Pon!

I deleted unused variables and optimised code. See my comments:

package com.netic.listener
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;

class ExampleListener extends AbstractIssueEventListener {

    WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
    @Override
    void workflowEvent(IssueEvent event) {

        Issue issue = event.issue;        
        //Zverev: it is better to place check at start
        if( (issue.getIssueTypeObject().getName() != "Escalation")&&(issue.getStatusObject().getName()!= "Response"))
            return      
        
        String issueid     = event.issue.id
        String issuekey    = event.issue.key
        String status      = event.issue.getStatusObject().getName()
        String eventid     = event.getEventTypeId()
        String issuetype   = event.issue.issueTypeObject.name
        String currentuser = ((String) event.getUser()).split(":")[0]
        Integer transition = 0
        System.out.println("script=Listener.groovy IssueKey=" + issuekey + " action=StartScript")
        System.out.println("script=Listener.groovy IssueKey=" + issuekey + " currentUser=" + currentuser)
        System.out.println("script=Listener.groovy IssueKey=" + issuekey + " eventid=" + eventid)
        System.out.println("script=Listener.groovy IssueKey=" + issuekey + " issuetype=" + issuetype)
        System.out.println("script=Listener.groovy IssueKey=" + issuekey + " status=" + status)
        if (issuetype == "Escalation")
        {
            if (status == "Response") // Email Sent
            {
                transition == "Resolve" // Resolve Zverev: transition in Integer ype
            }
        }
        if (transition != 0 )
        {
            // Transist Issue
            workflowTransitionUtil.setIssue(event.issue);
            workflowTransitionUtil.setUsername(currentuser);
            workflowTransitionUtil.setAction (transition) //Int is requred, but Integer is set
            // Validate and transition issue
            workflowTransitionUtil.validate();
            workflowTransitionUtil.progress();
            status = event.issue.getStatusObject().getName()
            System.out.println("script=Listener.groovy IssueKey=" + issuekey + " Issue " + issueid + " transited to new status: " + status)
        }
        System.out.println("script=Listener.groovy IssueKey=" + issuekey + " action=EndScript")
    }
}

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,847 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot