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.
Hi,
For anyone falling in this same problem, I've found that the comment can be added if you make the change in a new thread.
Just adding:
Thread thread = Thread.start {
try {
def commentManager = ComponentAccessor.getCommentManager()
commentManager.create(issue, user, txt, true)
} catch (Exception e) {
Thread.currentThread().interrupt()
log.error("Thread exception " + e.message + "\n" + e)
return
}
return
}
It works regardless of validator returning true or false, but it could be better launching the thread only when returning false.
Regards.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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!!!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think your assumption is correct that you won't be able to create the comment if validator returns false and fails to pass.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.