Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

New on groovy and script-runner

Vectalis February 6, 2014

Hi,

using script-runner plugin I would like on post action to the change the Summary on the fly, according to some other fields

ex:

  • user creates issue and input a Summary as well as a Component.
  • After creation Summary field is original_summary + "-" + original_components

I'm a newbe and any link to

  • groovy basics
  • jira library documentation

will be welcomed

thanks

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Vectalis February 12, 2014

thanks for those clarifications.

cheers

3 votes
Sameera Shaakunthala [inactive]
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.
February 6, 2014

I'm also a beginner.

Groovy is Java. No need to be pro, but you should have basic knowledge of object oriented programming.

First thing I did is looking at some basic scripts. When you create a script post function in a workflow step, there are some built-in (canned) scripts that you can use. Once you select a canned script, you will see a link to 'view source'. View the source and study.

Secondly, for API reference I referred to the following link:

https://developer.atlassian.com/static/javadoc/jira/6.0.6/reference/packages.html

Start with modifying some canned scripts to cater your requirements. Definetely you will change it to what you need, while gaining knowledge.

That's how I learned. :)

Vectalis February 6, 2014

Thanks you Sameera. Your tips are useful. And the link you provided is far better than the one I found (https://docs.atlassian.com/jira/latest/com/atlassian/jira/ComponentManager.html)

I have 2 questions, maybe you can help.

  • I tried to follow the advice here and installed IDEA on the server running our Jira instance. Nevertheless I don't know how to figure out the version of groovy embed in our Jira server. so I cannot really start to debug.
  • in the API reference, there is nothing related to issue summary but only to project summary. I guess I miss something in this philosophy

I come from "compiled world" (C#) where you have proper editor to help you programming ;) web & script world is a total nightmare for me.

thanks

Julien

Henning Tietgens
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.
February 6, 2014

If you type GroovySystem.getVersion() into the script console you get the version.

I personally have IDEA installed on my local machine as groovy script IDE and I'm "debugging" through log commands in the scripts to print useful information to the log file (catalina.out).

Sameera Shaakunthala [inactive]
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.
February 6, 2014

Is your server Linux or Windows?

Regarding your interpretation of API redference, for example, issue summary:

API Reference: https://developer.atlassian.com/static/javadoc/jira/6.0.6/reference/com/atlassian/jira/issue/Issue.html

You have to import com.atlassian.jira.issue.Issue (Interface is Issue) and call the getSummary() method. That's it.

You are coming from 'compiled world' but seems you already have OOP knowledge. Script stuff will be much more easier but it's not going to give you the 'Microsoft' feeling.

1 vote
Henning Tietgens
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.
February 6, 2014

You could take a look at my answer here: https://answers.atlassian.com/questions/257042/groovy-starter-ressources

You can access the issue summary through issue.getSummary() in a workflow postfunction. The components you get through issue.getComponentObjects(). So your workflow postfunction should be like this

issue.summary += " - " + issue.componentObjects?.join('/')

Look for "Groovy Beans" in the Groovy documentation to see why you could write issue.summary instead of issue.setSummary() or issue.getSummary(). The question mark is the "Safe Navigation Operator (?.)" which prevents Null Pointer Exceptions in case of undefined results.

Maybe you want to test first if there are components selected, but I leave that up to you.

Vectalis February 6, 2014

Many Thanks for those details Henning.

I guess the reasons for direct access to summary is

"If you access a property from within the class the property is defined in at compile time with implicit or explicit this (for example this.foo, or simply foo), Groovy will access the field directly instead of going though the getter and setter."

I looked at the source of a canned script but I think there are to much infomation at a time. My guess s that I don't need all the includes in my own exemple. My class should be very simple.

I have 4 very basics questions

  • Is the [Hira-home]\Log\atlassian-jira the right place to look at the logs ?
  • sorry if my question seems stupid, where do you access the script console ?
  • Last question Henning I looked at the source of the sendMail exemple: Is the doScript mehtod the method supposed to be called by the postAction
  • my jira environment is based on windows. So when I run a custom script I indicate a path with "\". Is that supported ? Shoud I embrass my path with "" ?

Appreciate your help but I keep looking by myself at the same time of course.

Julien

Henning Tietgens
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.
February 9, 2014

The log file you suggested is correct.

The script console is a menu point in the plug in section in JIRA. Type "gg" on any JIRA screen, than type "console" and you will see the corresponding point.

For basic scripts you don't have to create/extend any classes or write methods. Write what you want to do. That's a benefit of a scripting language like Groovy.

For the Windows environment I couldn't help you. I would try to write the path like you would write it on Windows. If it doesn't work you will see error messages in the log.

0 votes
Vectalis February 11, 2014

Hi guys.

I did it eventually !

import com.atlassian.jira.issue.Issue 
import com.atlassian.jira.issue.MutableIssue 
import com.atlassian.jira.issue.IssueManager 
import com.atlassian.jira.ComponentManager 
import com.atlassian.jira.issue.resolution.Resolution 
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField



//retrieve the custome field 'Sous-Projet'
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomField ssprjt = customFieldManager.getCustomFieldObjectByName("Sous-Projet")

//retrieve a specific issue
//IssueManager issueManager = ComponentManager.getInstance().getIssueManager()
//MutableIssue myIssue = issueManager.getIssueObject("BP_19") 
MutableIssue myIssue = issue

//amend the issue summary
myIssue.summary = "[" + String.valueOf(myIssue.getCustomFieldValue(ssprjt)) + "] - " + myIssue.summary

//return myIssue.summary

I first debug it with a real issue and then implement a post action successfully.

Nevertheless I a bit confused:

  1. MutableIssue myIssue = issue

    I spent some time to figure out on the Web how to specify the current issue. Where is that clearly written in the documentation

  2. my post action is on the first transition "create". I had to place my post action on the top position before "Creates the issue originally." and this is not clear to me. Remarks by Henning on this case helped me.

Anyway thanks again for your help

cheers

Julien

Henning Tietgens
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.
February 12, 2014

On this page there is a hint for issue: "In each case, the plugin will provide the current issue and transientVars in the binding. That means you can refer to them using these variables." Another place to look for such bindings are the examples.

While creating the issue, the issue object exists but is not saved to the database before the "Creates the issue originally" happens. If you modify the issue object before "Creates the issue originally"your changes are saved in the moment "Creates the issue originally" happens. If you modify the issue object after "Creates the issue originally", you have to take care by yourself to save the changes. In this case "issueManager.updateIssue()" should be called. Or you use the safest way (from API usage point of view) to change the issue.

0 votes
Vectalis February 10, 2014

Hi Henning & Sameera,

I achieved to test a basic script (in the script consol) to understand how things work.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.resolution.Resolution


IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
MutableIssue myIssue = issueManager.getIssueObject("BP-19")
myIssue.summary += "-" + myIssue.getResolutionObject()?.getName()
return myIssue.summary

I will close definitively my ticket soon

Many thanks to both of you for your great help.

Cheers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events