Add a comment while throwing InvalidInputException Edited

I am trying to validate a JIRA transition using a scriptrunner script. I would want to log some details in the jira comments section and throw a UI exception which says "Refer to comments for more details"

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager

// some condition evaluation. Lets say one that fails

CommentManager commentManager = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class)
commentManager.create(issue, currentUser,"reason for failure", true)
invalidInputException = new InvalidInputException("Refer to comments for more details")


This code does produce a pop up with

"Refer to comments for more details

It seems that you have tried to perform an illegal workflow operation.

If you think this message is wrong, please contact your JIRA administrators"

 

The comment does not show up!!!!!! If I do not throw the exception, then the transition will go through and logs a comment.

 

 

3 answers

This widget could not be displayed.

Hi Venkatesh,

Which version of ScriptRunner and JIRA are you running? It seems like you are on an older version. Can you not use the built-in workflow validator feature provided by ScriptRunner? This will allow you to easily display an error message.

Screen Shot 2017-11-27 at 2.39.51 PM.png

Atlassian JIRA

Version : 7.5.3

 

Scriptrunner (v. 5.2.2)

 

@Joshua Yamdogo [Adaptavist] I have a big validator script and at the end I am able to display all error messages both at the field level and on the screen I linked with my transition,

Question is: I want to log a JIRA comment after the transition has been marked as negative by 

InvalidInputException

Venkatesh,

There is a big problem with your current script because you are still using ComponentManager. ComponentManager does not exist in JIRA 7, so any code with it will not work. See our documentation.

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()

commentManager.create(issue,currentUser,"Reason for failure",true)

Perhaps something like this will work to create the comment.

@Joshua Yamdogo [Adaptavist] Nope! Nothing logs in comments section despite using the suggestion above.

 

My Workflow in detail

1. Click on a JIRA state change from "In Progress" to "Advance next"

2. Pop up screen appears as I associated a screen with this transition

3. In the validator script attached with this transition, Do a network API call

  a. if 200 allow transition (then it logs comments on JIRA successfully)

  b. if other like a 401 then block the transition using InvalidInputException.

       User can make a retry and end up in 3.a but if he decides to cancel the transition, he would click "CANCEL" at the end of screen. (HELP!!!!! It won't log the comment :() 

I think your assumption is correct that you won't be able to create the comment if validator returns false and fails to pass.

This widget could not be displayed.

```

ds

```

This widget could not be displayed.

Actual complete code:

import com.atlassian.jira.component.ComponentAccessor;
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.opensymphony.workflow.InvalidInputException

StringBuilder commentBuilder = new StringBuilder()
boolean validationPass = true;

// Logger util to log the workflow
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

// Add comment to JIRA
def createComment = { String value ->
def commentManager = ComponentAccessor.getCommentManager()

def authContext = ComponentAccessor.jiraAuthenticationContext
def user = authContext.getLoggedInUser()

commentManager.create(issue, user, value, true)
log.info "Comment has been created"
}

def networkConnection = {
// x x x x x x some code here
}

def performValidation = {
HttpURLConnection connection = networkConnection() as HttpURLConnection
try {
if(!connection) {
throw new Exception("blabla")
}

if(connection.responseCode == 200){
// x x x x x x x some code here
}
else {
throw new Exception("Error")
}
} catch (Exception e) {
commentBuilder.append(e.message)
validationPass = false;
} finally {
connection.disconnect()
createComment(commentBuilder.toString())
if(!validationPass)
invalidInputException = new InvalidInputException(commentBuilder.toString())
}
}

performValidation()

Do not throw exceptions like this. Nothing is handling it and your validator code is literally aborting. This is, to put it bluntly, bad. You should never throw exceptions in finally blocks and you really shouldn't let them propagate up to Jira for no reason.

You should return true or return false. So in your case, just create your comment and return false. Otherwise return true.

@Steven, It does not seem to do any good. I thought I was setting the invalidInputException in finally block if validation failed.

Anyways I found out that we have a Simple scripted validator  that expects a true or false from groovy script and stops the transition.

I am making a comment just before the return false statement.

Outcome: It blocks the transition but does not log the comment. Need help!!!!!

I do not think we can ever create a comment if you return false.

Why I say so is, return false will block the transition and you cancel it to close the transition screen acknowledging the error. So kind of rollback.

Am I correct in assuming this? @Steven Behnke

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked yesterday in Data Center

Hi! I'm Cameron Deatsch, enterprise enthusiast and Head of Server at Atlassian. AMA!

Hey team! I'm Cameron Deatsch, Head of Server at Atlassian. At our European Summit just a few weeks ago, I spoke about the latest regarding our Server and Data center products and o...

19,299 views 4 4
View question

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