Hello,
I am new to groovy and looking for help for the following:
From my JIRA administrator, I got the following id's having database related information.
Database Server Type: ID=10513
Database Server Name: ID=12906
Database Name: ID=12911
Database Username: ID=12907
Database Password: ID=12908
My requirement: I have to develop a groovy script that should fetch the corresponding values based upon the ID's above and next, login into JIRA database using those values. I found groovy has sql class that allows connection to database. For that, I require values from the above ID's.
Please help.
Thanks,
Nishant
This is the solution that worked for me
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
// Filed id : customfield_16306 --> This is field ID for Impact Score.
def cf = getFieldById("customfield_16306");
//int cFieldValue = (int)(cf.getValue())
def cFieldValue = (int)(cf.getValue() ? cf.getValue() : 0)
if (cFieldValue >= 1 && cFieldValue <= 100 )
{
cf.clearError()
}
else if (cFieldValue < 1 || cFieldValue > 100 )
{
//below Command displays Error and but allows the user to submit the change. If you are enabling below comment setError
//cf.setHelpText("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : "+ cFieldValue)
cf.setError("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : "+ cFieldValue)
}
else
{
cf.setError("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : (Blank) ")
}
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.screen.FieldScreenFactory
import com.atlassian.jira.issue.fields.screen.FieldScreenManager
import com.atlassian.jira.issue.fields.screen.FieldScreenSchemeManager
import com.atlassian.jira.web.action.admin.issuefields.screens.ViewFieldScreens
import com.atlassian.jira.workflow.WorkflowManager
def sb = new StringBuffer()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_id")
sb.append("${cField}<br/>\n")
return sb.toString()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How can we convert the below line of code
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("MFP-10");
to make it more generic in a way where it can work for any Key
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Get CustomField value:
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.fields.CustomField; def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("MFP-10"); def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10301L);//customfield id if(customField != null) { issue.getCustomFieldValue(customField); }
Note: You must have Issue to get value from, because Customfield does not save data by itself.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Aleks,
I tried your code and it worked for me, however what I am trying to do is also automatically fetch the current issue key and use the custom field value in a sql query. I am getting the custom field value if I hardcode my issue key. The sql too isnt functioning. Please point out where I am going wrong. I get null returned for this.
Following is my code:
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager
import groovy.sql.Sql
import java.sql.Driver
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TR-24");
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11495L);//customfield id
def bugid = issue.getCustomFieldValue(customField);
def driver = Class.forName('com.mysql.jdbc.Driver').newInstance() as Driver
def props = new Properties()
props.setProperty("user", "rxy")
props.setProperty("password", "rxy")
def conn = driver.connect("jdbc:mysql://localhost:3306/bug", props)
def sql = new Sql(conn)
try {
sql.eachRow("select p.realname from bugs.bugs b join bugs.profiles p on b.qa_contact=p.userid where bug_id = ${bugid}") {
log.debug(it)
}
} finally {
sql.close()
conn.close()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear All,
I am looking for the script which will get the external values in the Jira custom field.
Can someone help me on this scripting?
Your help will be much appreciable.
Thanks,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you need to do this with external tool (not JIRA) or from JIRA itself?
Groovy is user for internal scripts.
For external tool I wood recommend you to start with JIRA REST API: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Vasiliy for your guidance.
I have to execute the script from JIRA itself as a post function. Further, with the help of different posts, I have developed the below script. All I am trying is to find the field value against the field id:10513.
---------------------------------------------------------------------------------------------------------------------------------------
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
void customFieldValues (String customFieldId) {
// gets a reference to the IssueManager and CustomFieldManager manager classes
final CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
// gets a reference to the desired custom field
final CustomField customField = customFieldManager.getCustomFieldObject("customFieldId")
// retrieves the custom field value object from the issue
final Object customFieldValue = getCustomFieldValue("customField")
// prints value to console
System.out.println("Custom field value: " + customFieldValue)
}
customFieldValues("10513")
---------------------------------------------------------------------------------------------------------------------------------------
However, it is giving below error:
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (java.lang.String) values: [customField] at Script691.sumCustomFieldValues(Script691.groovy:20) at Script691.run(Script691.groovy:24)
Could you please help me here?
Thanks,
Nishant
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There is no such a function getCustomFieldValue in your code. There can be
issue.getCustomFieldValue(customFieldObject);
Also do not use double qoutes here
customFieldManager.getCustomFieldObject("customFieldId")
because you it will be transformed to String. Use it without scopes:
customFieldManager.getCustomFieldObject(customFieldId)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Aleks for all the tips. I tried your your provided script as below with currentkey and customfieldid per my JIRA set-up. However, it is giving "Null" response. Any help on this, please?
--------------------------------------------------------------------------------------------------------------------------
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("MFP-10");
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10301L);//customfield id
if(customField != null) {
issue.getCustomFieldValue(customField);
}
---------------------------------------------------------------------------------------------------------------------------
Thanks,
Nishant
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My code is just an example.
Here
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("MFP-10");
input your own Issue Key instead of my "MFP-10"
and here
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10301L);
must be uor own customfield id instead of my 10301L (e.g. 10513L for Database Server Type Custom Field)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, that I understood and did the same. In my case, the Issue Key value was KWD-1 and customfield id was 10001. It is still returning null...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please check again if there is value for customfield 10001 in issue KWD-1. THiscode was checked in sciptrunner and it works fine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Aleks,
Thanks a lot, your script worked.
Further, I have extended it as below to store the custom field values into an array for further processing. However, it is giving null values.
I have double checked and each of the field id have valid value against it.
Please help.
----------------------------------------------------------------------
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
def fieldid = new int[5] //array storing the customfield id
def fieldvalue= new String[5] //array to store field values corresponding to each custom field for a given ticket
fieldid[0] = 10513
fieldid[1] = 12906
fieldid[2] = 12911
fieldid[3] = 12907
fieldid[4] = 12908
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TIK-3039");
for (i = 0; i <5; i++) {
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);//customfield id
if(customField != null) {
final Object customFieldValue = issue.getCustomFieldValue(customField);
fieldvalue[i] = "customFieldValue";
}}
for (i = 0; i <5; i++){
System.out.println("Custom field value: " + fieldvalue[i]);
}
-------------------------------------------------------------------------
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Custom field id is long type, but not int.
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.fields.CustomField; def fieldid = new long[5] //array storing the customfield id def fieldvalue= new String[5] //array to store field values corresponding to each custom field for a given ticket fieldid[0] = 10513L fieldid[1] = 12906L fieldid[2] = 12911L fieldid[3] = 12907L fieldid[4] = 12908L def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TIK-3039"); for (i = 0; i <5; i++) { def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);//customfield id if(customField != null) { final Object customFieldValue = issue.getCustomFieldValue(customField); fieldvalue[i] = "customFieldValue"; } } for (i = 0; i <5; i++){ System.out.println("Custom field value: " + fieldvalue[i]); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Vasiliy for pointing it out. However, script is still returning null.
I tried putting println at different points to debug the script. It seems "customField" is getting null value all the time within FOR loop. with my little knowledge, I am feeling that I am still not using arrays and then FOR loop correctly. May be in (fieldid[i]), value of i is not getting substituted.
Please help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
To test a script I would recommend to use Script Console and use retun statement.
At first lets try to get a custom field object like this:
import com.atlassian.jira.component.ComponentAccessor return ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10513L).getName()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And you should to see custom field name
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Vasiliy for your continued support in helping me resolving it.
I tested the same and found that-
"def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);
//customfield id" is returning correct value and getting properly assigned to customField. So, index "i" value is coming ok from FOR loop.
"issue.getCustomFieldValue(customField);" is also calculating correct value.
So the main statements are working fine. Problem is the assignation statement, "fieldvalue[i] = "customFieldValue";". Here, null is getting stored. I tried different ways to assign, however, "null" is the result.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry, it is hard for me to see this code, I just modified it slightly.
Idea - you created an String array, by try to store an Object value. Here is my version:
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.fields.CustomField; def fieldid = new long[5] //array storing the customfield id def fieldvalue = new String[5] //array to store field values corresponding to each custom field for a given ticket fieldid[0] = 10513L fieldid[1] = 12906L fieldid[2] = 12911L fieldid[3] = 12907L fieldid[4] = 12908L def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TIK-3039"); for (i = 0; i <5; i++) { def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);//customfield id if(customField != null) { fieldvalue[i] = (String) issue.getCustomFieldValue(customField); } } return fieldvalue.toString()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.