Scriptrunner - Getting Value of First Component

jeff poggensee November 29, 2012

I'm very new to Jira API, Groovy scripting and the Scriptrunner Plugin. Given that, I have been able to use it in a post function to raise a custom notification event based on the value of a custom field. Now a customer would like to be able notify different groups of users based on the component entered. Team A should be notified if Component A is entered, Team B notified when Component B is entered, ect. The event raised should be base on the first component entered and in this project the user should only chose one component for an issue.

My question is - Using scriptrunner plugin, is it possible to get the value of the first component entered and if so can anyone provide an example of the script?

If this is not possible, I can always remove components and implement a customfield with the required values, but would have to change some filters and dashboards as well.

Thanks in Advance

2 answers

1 accepted

3 votes
Answer accepted
Alexey Paveliev
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 12, 2012

I'm not sure if getComponentObjects() method returns components in same order as user added, but you can test with code like this

issue.getComponentObjects() { c ->
   //do whaever you want with...
   log.debug "Component ${c.getName()}"
}

or

issue.getComponentObjects().getAt(0)?.getName() //corrected per Jamie's comment. Now null safe

jeff poggensee December 12, 2012

Thanks Alexey, I'll give this a try. FYI - I was able to get around this by using by component lead assignment. I raised a custom event by checking the value of issue's assignee for the various component lead names.

Alexey Paveliev
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 12, 2012

the easiest way to quickly test groovy script is to create script field and test it on actual issue without any consequences

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 14, 2012

Should be the following, otherwise you will get NPE for issues with no components.

issue.getComponentObjects().getAt(0)?.getName()

Creating a script field for testing is a good idea, you don't need to assign it to any issues. Or personally I just use issuemanager to get an issue, and test in the admin script panel.

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 14, 2012

We all had to learn once ;-) Anyway, hopefully that sort of problem would come out in testing...

Alexey Paveliev
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 14, 2012

Thanks Jamie.

Two weeks ago I had no idea Groovy exists. Still learning

0 votes
jeff poggensee December 16, 2012

Thanks Alexey and Jamie, this will be very useful on the next project !

Suggest an answer

Log in or Sign up to answer