Odd Groovy Script problem with a function call from another groovy script.

Jason Galm April 23, 2013

It worked once, and now it fails oddly enough.

I have script that calculates the percentage done by taking

(sum)timeSpent / ((sum)timeSpent +(sum)estimate)

My boss wants this to be a field and wants a second field that takes this information and multplies it by StoryPoints a number field we have to get the StoryPointsCompleted for an issue.

So I didn't want to duplicate my code so I have 3 groovy files

PercentRemaining the has method with this signature

public static double getPercentDone(Issue issue)

and PercentRemaingField

double percent = PercentRemaining.getPercentDone(issue)

if(percent == -1)
{
	return "<p>ERROR in PercentRemaing.groovy<p>"
}
else if (percent > 1.0)
{	
	//Ids are just for Jira sorting to make 100.00% does not come before 10.00%
	return "<p id=\"a\">" + (percent * 100) + "%<p>"
}
else
{
	return "<p id=\"z\">" + (percent * 100) + "%<p>"
}

I have not made the 3rd file yet, but is almost the same thing it is going to access the custom field StoryPoints and multiply it by the double returned by my method.

It was working before but I was getting a rounding error (.989 became 1.0) and after that it just stopped working, even after I undid my few changes. (import java.math.RoundingMode and df.setRoundingMode(RoundingMode.DOWN); )

The files are in the same directory.

3 answers

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.
April 24, 2013

Try to clear the Groovy class cache through the built-in scripts of Script Runner, maybe this helps.

Jason Galm April 26, 2013

Thaks, I will try that when I am in work next week. I just thought it was odd because it worked, and then all I did was added something to the function, tested it and it broke in the class that was calling it while working itself. (I had a sperate scripted field just for testing the function)

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.
April 26, 2013

> The files are in the same directory

I'm not sure how you're calling the other scripts - using getFieldValue(firstCalcdField) should work, using other ways like calling static methods (which I see you're doing) seem a bit iffy to me.

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.
April 26, 2013

Unfortunately the CustomField object itself doesn't seem to be in the script binding - otherwise I'd recommend just using the same file for all 3, and setting different output depending on the field name or ID.

Jason Galm April 30, 2013

What is the getFieldValue in? I tried this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import java.lang.Double

def customFieldMag = ComponentAccessor.getCustomFieldManager()

double percent = (customFieldMag.getCustomFieldObjectByName( "%done" )).getValue(issue)

return percent


and I am getting nothing.

0 votes
AKASHB June 13, 2013

sorry, from next time will use the source code section. i was totally unaware of it.

0 votes
AKASHB June 13, 2013

I am stuck with this script: My requirement is

1) I need to connect to jira using groovy script calling from my terminal or from IntelliJIdea IDE .

2) Create issue and set summary and Description.

My Code is:



// 1) Creating Issues through IntelliJIdea

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Category



// Defninition of the component manager classes
def componentManager
def customFieldManager
def issueFactory
def jiraAuthenticationContext
def issueManager
def subtaskManager



def public CreateIssue(com.atlassian.jira.issue.MutableIssue issue)
{
componentManager = ComponentManager.getInstance()
customFieldManager = componentManager.getCustomFieldManager()
issueFactory = componentManager.getIssueFactory()
jiraAuthenticationContext = componentManager.getJiraAuthenticationContext()
issueManager = componentManager.getIssueManager()
subTaskManager =componentManager.getSubTaskManager()

def Category log = Category.getInstance("com.onresolve.jira.groovy.CM861");

//Creating a new Issue as subtask
MutableIssue createIssue = issueFactory.getIssue();


// 2) Setting Summary for the Issue Ticket on Creation by Default.




createIssue.setSummary("Issue Tickets From Groovy");
issue.setSummary("This is an issue ticket from Groovy")
println("I am in set Summary Method")
log.warn(createIssue.summary);



// 3) Setting Description for the Issue Ticket on creation by default



createIssue.setDescription("This is an issue ticket from Groovy");
println("I am in Set Description method")
log.warn(createIssue.description);


indexManager.reIndex(newSubtaskGV);
log.warn("***");
}


// call to the Function


CreateIssue(Issue)

AKASHB June 13, 2013

Henning can you please help me out with this.

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.
June 13, 2013

Ask a new question - this is unrelated to the OP's question.

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.
June 13, 2013

And please use a code section for your code... it's not fun to read the code as normal text.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events