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

0 vote
Vasiliy Zverev Community Champion Nov 23, 2016

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 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 Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

81 views 0 5
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