We came across the plug-in for JIRA called “Script Runner”. It was a nice plug-in, except that there was not too many examples or documentation for all of its capabilities. So, I turned to my best friend “Google”. But alas, help was not available easily. I had to dig in deep, ransack my brain to come up with a search term which might get me results. Why not save the pain for some other people. So, here is my $0.02.
Script Runner plug-in allows you to write and execute Groovy scripts, for that matter, any scripts provided you have the JAR files for that language. Once, you install this plug-in, you can insert your groovy script into any of the operations that you perform.
For e.g., you can assign a set a value to a custom field during a state transition or you could write your own assignment logic during state transition.
So, in this post, I thought I would share some of the simple Groovy code snippets that I had created and used.
Updating a Custom Field:
As I was mentioning earlier, updating a custom field is simple and yet could be done in multiple ways, some easier and some not so easier :). The first approach that I came up with looked something like a Visu movie.
import com.atlassian.jira.ComponentManager
import java.text.*;
import java.util.*;
import com.atlassian.jira.issue.fields.CustomField;
import java.sql.*;
// Get the component manager & custom field manager instance for the script
def componentManager = ComponentManager.getInstance();
def customFieldManager = componentManager.getCustomFieldManager();
def cf = customFieldManager.getCustomFieldObjectByName("CreatedOn");
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
Format formatter = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
String timeStamp = formatter.format(new Date());
cf.updateValue(null, issue, new ModifiedValue([], timeStamp ),changeHolder);
cf.store();
Don’t even think. I am not going to explain this horror. Unable to store a Date field, I had stored the value as a string. Then, as a mature programmer would do, same functionality was re-factored to a simple this.
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.*;
import java.util.*;
// Get the component manager & custom field manager instance for the script
def componentManager = ComponentManager.getInstance();
def customFieldManager = componentManager.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObjectByName("CreatedOn");
cf.createValue(issue, new java.sql.Timestamp(new Date().getTime()));
Well, all we do here is get the Component Manager, which is the source for providing manjority of the JIRA objects required like Custom Field Manager, Project Manager or Authentication Manager. It is almost like a Manager Factory :). Using this we can then get the required Custom Field object by passing in the name of the custom field, “CreatedOn” in this case. Once, you get the object it’s a piece of cake. The JIRA takes only SQL TimeStamp value, even if you declare the field as a Date Field and not a DateTime field. I know, easy isn’t it, that is if you already know that.
You can get the list of JIRA objects available and their documentation here.
In the next coming posts, we will discuss on the JQl (JIRA Query Language) and how to use it in the Script Runner scripts and also other client APIS to access JIRA from outside JIRA.
Until then
Desire!!! Indulge!!! Inspire!!!