Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to display error message from progressErrorCollection.getErrors().toString() in Jira screen

Mouna Hammoudi December 23, 2022

My code is trying to perform 3 transitions back to back: from Open to Fixed, then from Fixed to Tested and then from Tested to Completed. I have a validator as well as a postfunction. The validator throws exceptions as you can see below in my code for the validator: 

package CombineTransitions

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

import com.atlassian.jira.issue.Issue;

import org.apache.log4j.Logger

import com.atlassian.jira.bc.issue.IssueService

import com.atlassian.jira.issue.Issue;

import CombineTransitions.Configuration_CombineTransitions

import com.atlassian.jira.bc.issue.IssueService.IssueResult

import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult

import com.atlassian.jira.event.type.EventDispatchOption

import  java.util.concurrent.TimeUnit

import  java.util.concurrent.ScheduledExecutorService

import java.util.concurrent.Executors

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.workflow.WorkflowTransitionUtil

import com.opensymphony.workflow.WorkflowContext;

import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;

import com.atlassian.jira.util.JiraUtils;

import com.atlassian.jira.issue.MutableIssue;

import com.atlassian.jira.util.ErrorCollection

import com.atlassian.jira.workflow.WorkflowException

import com.atlassian.jira.issue.fields.CustomField

import com.onresolve.jira.groovy.user.FormField

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.component.ComponentAccessor

import com.opensymphony.workflow.InvalidInputException

def log1 = Logger.getLogger("atlassian-jira.log")

def combineTransitionsHashMap= Configuration_CombineTransitions.getCombineTransitionsHashMap()

def issueStatusTypeId = combineTransitionsHashMap["issueStatusTypeId"] as String;

def transitionFromOpentoFixed = combineTransitionsHashMap["transitionFromOpentoFixed"] as Integer;

def transitionFromFixedToTested = combineTransitionsHashMap["transitionFromFixedToTested"] as Integer;

def transitionFromTestedToCompleted = combineTransitionsHashMap["transitionFromTestedToCompleted"] as Integer;

def fixedStatusName = combineTransitionsHashMap["fixedStatusName"] as String;

def testedStatusName = combineTransitionsHashMap["testedStatusName"] as String;

def completedStatusName = combineTransitionsHashMap["completedStatusName"] as String;

def fixedStatusId = combineTransitionsHashMap["fixedStatusId"] as String;

def testedStatusId = combineTransitionsHashMap["testedStatusId"] as String;

def completedStatusId = combineTransitionsHashMap["completedStatusId"] as String;

def errorMessage = combineTransitionsHashMap["errorMessage"] as String;

if (issue.getStatus().getSimpleStatus().getId().equals(issueStatusTypeId)) {

        def ok = false

         ok=performTransition(transitionFromOpentoFixed, fixedStatusName, fixedStatusId, errorMessage);

         log.warn("MOUNA first boolean "+ok +" "+fixedStatusName)

   if(ok){

         ok= performTransition(transitionFromFixedToTested, testedStatusName, testedStatusId, errorMessage);

         log.warn("MOUNA second boolean "+ok +" "+testedStatusName)

         if(ok){

          ok= performTransition(transitionFromTestedToCompleted, completedStatusName, completedStatusId, errorMessage);

          log.warn("MOUNA third boolean "+ok +" "+completedStatusName)

         }

 

  }

   

   

}

def  performTransition(int transitionToBeDone, String destinationStatus, String statusId , String errorMessage) {

  log.warn("MOUNA  ORIGINAL STATUS "+ issue.getStatus()  + "DEST STATUS "+ destinationStatus)

issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id)

  def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

  String issueKey = issue.getKey()

  IssueService issueService = ComponentAccessor.getIssueService()

  def issueInputParameters = issueService.newIssueInputParameters()

  issueInputParameters.setComment("Transitioning issue from status "+ issue.getStatus().getName()+" to status "+ destinationStatus );

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

workflowTransitionUtil.setIssue(issue)

workflowTransitionUtil.setAction(transitionToBeDone)

ErrorCollection errorCollection=workflowTransitionUtil.validate()

if (errorCollection.hasAnyErrors()) {

    log.warn("MOUNA here 1 "+errorCollection.getErrorMessages().toString())

    throw new InvalidInputException(errorCollection.getErrors().toString())

return false;

}else{

 

    ErrorCollection progressErrorCollection=workflowTransitionUtil.progress()

    if(progressErrorCollection.hasAnyErrors()){

        log.warn("MOUNA here 2 "+progressErrorCollection.getErrors().toString())

       throw new InvalidInputException(progressErrorCollection.getErrors().toString())

   

   

          return false;

    }else{

      return true;

    }

}

}

// String getError(String error){

//   return error;

// }

Here is the code for the postfunction which is supposed to transition my issue from one state to the next.

package CombineTransitions

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

import com.atlassian.jira.issue.Issue;

import org.apache.log4j.Logger

import com.atlassian.jira.bc.issue.IssueService

import com.atlassian.jira.issue.Issue;

import CombineTransitions.Configuration_CombineTransitions

import com.atlassian.jira.bc.issue.IssueService.IssueResult

import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult

import com.atlassian.jira.event.type.EventDispatchOption

import  java.util.concurrent.TimeUnit

import  java.util.concurrent.ScheduledExecutorService

import java.util.concurrent.Executors

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.workflow.WorkflowTransitionUtil

import com.opensymphony.workflow.WorkflowContext;

import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;

import com.atlassian.jira.util.JiraUtils;

import com.atlassian.jira.issue.MutableIssue;

import com.atlassian.jira.util.ErrorCollection

import com.atlassian.jira.workflow.WorkflowException

import com.atlassian.jira.issue.fields.CustomField

import com.onresolve.jira.groovy.user.FormField

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.component.ComponentAccessor

import com.opensymphony.workflow.InvalidInputException

def log1 = Logger.getLogger("atlassian-jira.log")

def combineTransitionsHashMap= Configuration_CombineTransitions.getCombineTransitionsHashMap()

def issueStatusTypeId = combineTransitionsHashMap["issueStatusTypeId"] as String;

def transitionFromOpentoFixed = combineTransitionsHashMap["transitionFromOpentoFixed"] as Integer;

def transitionFromFixedToTested = combineTransitionsHashMap["transitionFromFixedToTested"] as Integer;

def transitionFromTestedToCompleted = combineTransitionsHashMap["transitionFromTestedToCompleted"] as Integer;

def fixedStatusName = combineTransitionsHashMap["fixedStatusName"] as String;

def testedStatusName = combineTransitionsHashMap["testedStatusName"] as String;

def completedStatusName = combineTransitionsHashMap["completedStatusName"] as String;

def fixedStatusId = combineTransitionsHashMap["fixedStatusId"] as String;

def testedStatusId = combineTransitionsHashMap["testedStatusId"] as String;

def completedStatusId = combineTransitionsHashMap["completedStatusId"] as String;

def errorMessage = combineTransitionsHashMap["errorMessage"] as String;

//try{

if (issue.getStatus().getSimpleStatus().getId().equals(issueStatusTypeId)) {

        def ok = false

         ok=performTransition(transitionFromOpentoFixed, fixedStatusName, fixedStatusId, errorMessage);

         log.warn("MOUNA first boolean "+ok +" "+fixedStatusName)

   if(ok){

         ok= performTransition(transitionFromFixedToTested, testedStatusName, testedStatusId, errorMessage);

         log.warn("MOUNA second boolean "+ok +" "+testedStatusName)

         if(ok){

          ok= performTransition(transitionFromTestedToCompleted, completedStatusName, completedStatusId, errorMessage);

          log.warn("MOUNA third boolean "+ok +" "+completedStatusName)

         }

 

  }

   

   

}

def  performTransition(int transitionToBeDone, String destinationStatus, String statusId , String errorMessage) {

  log.warn("MOUNA  ORIGINAL STATUS "+ issue.getStatus()  + "DEST STATUS "+ destinationStatus)

issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id)

  def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

  String issueKey = issue.getKey()

  IssueService issueService = ComponentAccessor.getIssueService()

  def issueInputParameters = issueService.newIssueInputParameters()

  issueInputParameters.setComment("Transitioning issue from status "+ issue.getStatus().getName()+" to status "+ destinationStatus );

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

workflowTransitionUtil.setIssue(issue)

workflowTransitionUtil.setAction(transitionToBeDone)

ErrorCollection errorCollection=workflowTransitionUtil.validate()

if (errorCollection.hasAnyErrors()) {

    log.warn("MOUNA here 1 "+errorCollection.getErrorMessages().toString())

return false;

}else{

 

    ErrorCollection progressErrorCollection=workflowTransitionUtil.progress()

    if(progressErrorCollection.hasAnyErrors()){

        log.warn("MOUNA here 2 "+progressErrorCollection.getErrors().toString())

     

          return false;

    }else{

      return true;

    }

}

}

// String getError(String error){

//   return error;

// }

 

The problem that I have is that I keep receiving errors even though I am fixing what the exception is throwing. For example, in the screenshot below, you notice that I have input a comment but I am still receiving an error message saying that I should add a comment. 

 

Anyone knows how to fix this problem?

 

 

Capture.PNG

 

2 answers

0 votes
Will C
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2022

@Nic Not that I am doubting you but why can't you perform a transition within a transition? scriptrunner comes with a built in fast track transition post function which I guess is just doing a transition within a transition, unless its doing something funky behind the scenes?

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2022

It is indeed doing something funky behind the scenes...

Will C
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2022

Would Adaptavist like to share? I've had issues doing transitions within a post function before and always thought, well if Adaptavist are doing it so can I :)

Mouna Hammoudi December 23, 2022

@Will C 

I have separated the code into 2 parts: validator + postfunction. The problem now is that I receive an error message saying that I should add a comment even though I did input one. I have updated my code in the post. Here is a screenshot of the new problem 

Capture.PNG

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2022

Welcome to the Atlassian Community!

There are several problems with your code here, but we should get the first two main ones fixed before going into detail on the others.

  • A post function is the wrong place to try to do validation, or reporting, you should be doing validation in the transitions validators.
  • You can not perform a transition within a transition, you'll need to remove the whole section that tries to do that

Start by fixing those two, and most of the other problems will go away.

Mouna Hammoudi December 23, 2022

the goal of my postfunction is to perform 3 transitions back to back, I want to go from open to fixed, then from fixed to tested, then from tested to completed. That's why I have transitions within others. Is it still possible to do validation within my postfunction? 

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2022

You simply can't do this.  Before you start a transition, the previous one has to be complete.  If you do do it, then the best case is that you'll end up with an issue that is inconsistent state and can't be transitioned, worst case is a simple crash with a load of incorrect data written on to the issue.

Use script-runner's "fast track transition" functions instead, that does not have the problems (and it's a very long pile of code, I'm not sure you could replicate it in your own scripts)

As I already said, validation can not be done in a post-function.  You need to do it in a validator.

Mouna Hammoudi December 23, 2022

@Nic Brough -Adaptavist- I have separated the code into 2 parts: validator + postfunction. The problem now is that I receive an error message saying that I should add a comment even though I did input one. I have updated my code in the post. Here is a screenshot of the new problem 

 

Capture.PNG

Suggest an answer

Log in or Sign up to answer