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 votes
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
Community showcase
Posted Sep 18, 2018 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...

26,183 views 2 7
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