Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×

Forums

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

Workflow transition script condition always true

Andrew Thorne
Contributor
May 31, 2018

Hello,

I am new to Groovy and I am trying to write a script that will not allow a transition to the closed state of the workflow if all of these conditions are true:

  • the issue type is not Epic and it is not a sub-task
  • the resolution is Done or Fixed on Dev
  • the Release Comments are empty

I am not having much success. My script always passes and I can not see why. Here is the code I have:

// Give error message if (all 3 need to be true):
// 1) Issue type is NOT epic nor sub-task
// 2) Resolution is Done or Fixed on Dev
// 3) Release Comment is empty

import com.atlassian.jira.component.ComponentAccessor
// Message
import com.onresolve.scriptrunner.runner.util.UserMessageUtil

// Custome fields: release comments
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def releaseComments = customFieldManager.getCustomFieldObjectByName("release comments")

// Valid type?
if (issue.getIssueType() != "Epic" && ! issue.getParentId()) {
// Resolution is Done or Fixed on Dev?
if (issue.getResolution() == "Done" || issue.getResolution() == "Fixed on Dev") {
// Release comments are blank
if (issue.getCustomFieldValue(releaseComments) == "") {
UserMessageUtil.error("Release Comments required when resolution is Done or Fixed on Dev.")
return false
}
}
}

return true

To be honest, I am not sure I am doing this correctly so any hints or tips for my code are welcome.

FYI: we have Jira 7.3.6 and ScriptRunner 5.4.1

 

Andrew

2 answers

1 vote
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.
May 31, 2018

My guess is that your logic is broadly right, but your equivalence checks (==) are not.

For example, getIssueType() will return an issueType object, not a string, so your check for "is not epic" is always going to return true.  Similarly for resolution

A little more formally (I'm happier in Java than groovy for this), I would try:

if ("Epic".equals(issue.getIssueType().getName()) && ! issue.getParentId()) {
// Resolution is Done or Fixed on Dev?
if ("Done".equals(issue.getResolution().getName()) || "Fixed on Dev".equals(issue.getResolution().getName())

The comments one might be ok as it is.  I'm not sure of issue.getParentId, I think that's ok too, but might be wrong.

0 votes
Andrew Thorne
Contributor
June 1, 2018

Hi, Nic,

Thanks for your reply. I tried your suggestions and I think I might be getting somewhere. I added these lines in as a debugging aid:

if (! "Epic".equals(issue.getIssueType().getName())) { log.error("Issue type is NOT Epic (good)") } else { log.error("Issue type is Epic (no release comments needed)")}
if (! issue.getParentId()) { log.error("Parent Id is null (good)") } else { log.error("Parent Id is not null (subtask)")}
if ("Done".equals(issue.getResolution().getName())) { log.error("Resolution is Done (good)") } else { log.error("Resolution is NOT Done (but is it Fixed on Dev?)")}
if ("Fixed on Dev".equals(issue.getResolution().getName())) { log.error("Resolution is Fixed on Dev (good)") } else { log.error("Resolution is NOT Fixed on Dev")}
if (issue.getCustomFieldValue(releaseComments) == "") { log.error("Release comments are empty") } else { log.error("Release Comments are NOT empty")}

However, it does not like this bit:

"Done".equals(issue.getResolution().getName())

as it gives the error:

java.lang.NullPointerException: Cannot invoke method getName() on null object
 at Script133.run(Script133.groovy:17)

Now, Resolution has a default value of Done on the Resolve Issue screen, which is displayed before the script is executed. So, for me (ex-mainframe programmer), issue.getResolution() should not be null. But what do I know?

Andrew

Suggest an answer

Log in or Sign up to answer