how to look at issue type in Condition section of Script Post-Function function for a transition

Tony September 13, 2011

Trying to define a Condition in a Post-Function where I perform an Action based on a Condition.

The Condition is to look at the issue type and see if it equals a certain string value. Example below:

issue.issueType.name == "New Feature"

Question:

Is the syntax above correct? Does issue.issueType.name store the issue type?

In general, where does one go to fine out the class name and class state (variables) so one can write accurate Conditions in a Post-Function?

Cheers!

1 answer

1 accepted

11 votes
Answer accepted
JamieA
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.
September 13, 2011

Hi,

Yes it is correct (because getIssueType returns a GenericValue which implements Map, and you can get values by calling the key as a property in groovy), although it might be better to say:

issue.issueTypeObject.name == "New Feature"

> In general, where does one go to fine out the class name and class state (variables) so one can write accurate Conditions in a Post-Function

So there are two things you need to do this, 1) to know what variables you can use (ie are in the script binding), and 2) what types they are so you can work out what methods to call on them.

You can get 1) , and 2) actually, from here: https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-TechnicalStuffforAdvancedUsers , or looking at the source code.

So for instance you know now that *issue* is a MutableIssue, so you look at the javadoc at http://docs.atlassian.com/jira/latest/com/atlassian/jira/issue/MutableIssue.html.

Now you find you can call getIssueTypeObject() on it, and in groovy thing.issueTypeObject is just shorthand for the same.

And finally if you go to Admin -> Script Runner -> Built-in Scripts -> Condition Tester you can test the condition on various different issues, which is much easier than publishing a workflow. In your case above you would have got a MissingMethodException (or similar).

Really finally, if you set up the source in a good ide like intellij idea, you never need bother with the javadoc: https://studio.plugins.atlassian.com/wiki/display/GRV/Script+Runner#ScriptRunner-DebuggingandCodingwithIDEA

HTH, jamie

Tony September 13, 2011

Thank you for your help. This is very useful. I was wondering how to setup Intellij IDEA on my (local) Windows PC to debug (step through) the source code of a JIRA instance running on another (Linux) server. Any ideas?

JamieA
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.
September 14, 2011

Well - did you follow the instructions in the link above?

Tony September 14, 2011

I read thru the instructions in the link and wondered 'how does this allow me to debug remotely?'. Maybe I am missing something but does it not say to download and install the source code locally (ok, no problem)? I have not used Intellij IDEA to do remote debugging. If it's just a matter of following the instructions on that link, I'll give it a shot. The step 'Assure you can run JIRA properly before continuing' also is confusing to me. How do I do that on my PC with 4Gb of memory (is that enough memory)?

I appreciate all your feedback btw -- very kind of you to assist.

Cheers!

JamieA
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.
September 14, 2011

4gb is plenty. You don't need the source code to use those instructions, it's mainly to give people intellisense. You can attach the javadoc as well then you don't need to browse it on the web. Having the source code is helpful if you want to step in to the jira classes but it's not necessary for writing a plugin.

Debugging remotely is the same as for any java app... just specify the name of the remote host.

Radek Kantor
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.
September 14, 2011

Hi, for developing in JIRA it is better edit startup_bundled_jre.bat to run JIRA in debug mode.

Just add parameters

-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9095

to set JAVA_OPTS=-server %JAVA_OPTS%

and connect to remote host on port 9095

Tony September 14, 2011

I am not trying to develop a plugin, btw, but thanks for that input (I may do so in the future and its good to know).

I have installed Groovy and GroovyRunner and a post function (to Fast-track transition an issue) does not appear to be working. So I wanted to step through the code to see 'why' this problem exists.

JamieA
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.
September 29, 2011

It's the same advice as above for stepping through groovy.

t April 23, 2015

Is this the same case for a custom issue type, e.g called specials i would use "Specials" or do i need to refer to it by the ID number?

Darren Shinkins December 13, 2016

Hi Jamie, 

Should this syntax also work for condition statements on post functions?

I've used the exact syntax above to apply a condition to a Transition Parent Issue post-function, but it doesn't fire. The post function works when I set no condition; it also works if I set a basic condition that will always return true ( like 1 == 1 ).

Do I need to be doing something like calling a library or something (I figured that one line would be enough in the context of "this" issue). 

 

N.B. this is on JIRA 6.4.12 and ScriptRunner 4.1.3.7

JamieA
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 13, 2016

Even though the question is over five years old wink yes it should still work. 

If not try adding an assertion in the previewer: https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_power_assertions

What is your actual condition?

Darren Shinkins December 13, 2016

Thanks for getting back to me Jamie,

I know it's old, but it looks like exactly what I need, and I took a punt that you'd still reply pretty quickly! laugh

I'll take a look at it with a power assertion. In the meantime, the thing I'm trying to do is this:

I'm applying a transition parent issue post function to a subtask's workflow. I only want the parent to transition when the subtask in question has a particular issue type.

So, in the post function (on the subtask's workflow) I've entered a condition line to test that the subtask's issuetype is a "Design Task" so:

issue.issueTypeObject.name == "Design Task"

JamieA
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 13, 2016

Looks fine. If it always returns false add the assert (just temporarily while previewing). You might have a trailing space or something or case issue or something.

Darren Shinkins December 13, 2016

Ah, I think I've figured something out! It looks to me like the post function for Transition Parent issue isn't one supplied by Scriptrunner. I disabled Scriptrunner in our non-production platform and the post function is still available to me!

I'd just assumed that it was because it was asking for a Groovy expression in the condition field.

The code you gave me works perfectly in an actual Scriptrunner post function (tested it in the Custom email one) but the one I'm having trouble with returns a NoSuchElementException in the logs.

I'm guessing I'll probably need to add in some import statement or other to get the right component accessor. Clearly this post function isn't as well written as your ones! laugh

Thanks for your time!

Darren Shinkins December 13, 2016

OK, I've managed to sort it now, in case you're interested.

The post function is supplied by JIRA Misc Workflow Extensions, and it looks like it's set up to access a limited set of functions: issue, issueObject, or parentIssue.

After digging around I ended up with:

issue.get("issuetype").name == "Design Task"

 

Thanks again for your input Jamie smile

Adam Pagac February 10, 2017

Darren you made my day smile

Darren Shinkins February 10, 2017

Glad to be of service laugh

Adam Pagac February 24, 2017

Any chance you have a solution for how to get actual issue status?
I have tried a lot of options and non was working for me:
issue.get("status").name == "Open" 

issue.getSimpleStatus().getName() == "Open"

issue.get("status").getName() == "Open"

underlyingIssue.getStatus().getName() == "Open"

issue.getStatus().getName() == "Open"

issue.getStatusObject().getName() == "Open"

Darren Shinkins February 24, 2017

Hi Adam,

I just gave this a try (after finding the answer on another forum smile) and it works for me:

issue.get("status").getName()=="Open"

I also tested it for the use case that I personally would apply it to: checking the status of the parent issue, which would be:

parentIssue.get("status").getName()=="Open"

That works for me too. Incidentally, if you're having trouble getting the issue status for issue.get("status") in a transition post-function, maybe it's about the status of the issue at the point the post-function runs?

issue.get("status").getName() worked for me when I'd evaluated it against the status that the issue was moving from, rather than the one in moves to

Does that help at all?

Adam Pagac February 26, 2017

Hi Darren,

thank you for your input. I was following the same article you mentioned but without success. My use case is:

  • we use kanban boards and according workflows much (it means ALL transitions to statuses)
  • I want tu get rid of transition of status to itself (so actual and destination status is the same
  • i am using this validation as condition (not in post function) -> "Scripted (Groovy) Condition (JMWE add-on)"
  • unfortunatelly it allways returns false, so the particular transition is never shown

I have been googling a lot and removing transition to itself seems to be unsolved issue, i thought it could be solved with this condition but it does not work as I assumed.

Darren Shinkins February 26, 2017

Ah, I see; yeah, that wouldn't make much sense on a post function! smile

I've used "All" Transitions from time to time and I came up with the same problem. Personally I solved it by using the "Value Field" condition offered by Suite Utilities for Jira. If you've got the plugin, how the condition works is pretty much self explanatory: you just set "Status" as the field you're checking.

If you don't have Suite Utilities for Jira, definitely check it out; there's lots of useful things in it.

Hope that helps.

Adam Pagac February 28, 2017

Finally, this forum works smile

We have Suite plugin so I have tried proposed solution and it works fine. It takes more time than I would like to spent on this task (considering JIRA prices - creating field, seting it on transitions and than manually validate each transition) but finally its done.

Thanks again

 

Xavier
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.
September 18, 2018

works for me with issue.issueType.name

Suggest an answer

Log in or Sign up to answer