Post upgrade from 6.4.9 to 7.0.11 groovy script is throwing errors Edited

We have have a groovy script which stops working post JIRA upgrade.

 

Can you please help us in fixing the same?

 

Error from logs

*********************

2017-05-02 15:21:17,766 devstatus.applink:thread-3 ERROR XXXXXXXXX 847x1378x2 1l6oimu 10.20.33.66 /rest/dev-status/1.0/issue/summary [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: XYZ-12345, field: Epic Rollup
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'abcd(abcd)' with class 'com.atlassian.jira.user.DelegatingApplicationUser' to class 'com.atlassian.crowd.embedded.api.User'
at Script159.getUser(Script159.groovy:121)
at Script159.retrieveIssuesInEpic(Script159.groovy:62)
at Script159.run(Script159.groovy:33)

 

Script

***********

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;

import com.atlassian.jira.config.properties.ApplicationProperties;
import com.atlassian.jira.config.properties.APKeys;

import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.issue.DocumentIssueImpl;

import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.crowd.embedded.api.User;

import java.util.concurrent.TimeUnit;

log.setLevel(org.apache.log4j.Level.DEBUG);
def logString="";


/* -----------------------------------------------------------------------------------------------------------
* Gobal vlaues: user, issue (assuming an Epic)
* ------------------------------------------------------------------------------------------------------------
*/

/* User that will be used to search for issues.
* Must have Browse Project permission on all relevant projects.
*/
USER = ‘abcd’
children = retrieveIssuesInEpic(issue)
total = sumOfOriginalEstimates(children)
total = format(total * 1000) // Atlassian returns milisecond K
return total.toString()

private format(millis) {
ApplicationProperties applicationProperties = ComponentManager.getComponentInstanceOfType(ApplicationProperties.class);
Integer daysPerWeek = applicationProperties.getDefaultBackedString(APKeys.JIRA_TIMETRACKING_DAYS_PER_WEEK).toInteger();
Integer hoursPerDay = applicationProperties.getDefaultBackedString(APKeys.JIRA_TIMETRACKING_HOURS_PER_DAY).toInteger();

// TODO clean up messy calcualtions, maybe use mod function?
Long long_hours = TimeUnit.MILLISECONDS.toHours(millis)
Integer int_days = long_hours.toInteger()/hoursPerDay
Integer int_hours = long_hours.toInteger() - (int_days * hoursPerDay)
Integer int_weeks = int_days/daysPerWeek
int_days = int_days - (int_weeks * daysPerWeek)
Long long_minutes = TimeUnit.MILLISECONDS.toMinutes(millis)
Integer int_minutes = long_minutes.toInteger() - (int_hours*60) - (int_days * hoursPerDay * 60) - (int_weeks * daysPerWeek * hoursPerDay * 60)

formatted = String.format("%d weeks, %d days, %d hours, %d minutes",
int_weeks,
int_days,
int_hours,
int_minutes,
);
return formatted
}
private retrieveIssuesInEpic(epic) {
// Search for issues in epic
User user = getUser(USER);
jql_query = 'issueFunction in linkedIssuesOf("key = ' + epic.key + '", "is Epic of")'
if (user) {
List<DocumentIssueImpl> queryResults = runJQLSearch(user, jql_query);
if (queryResults && !queryResults.isEmpty()) {
return queryResults
}
}
return []
}

/*
* Given a list of issues, sums up the original estimate on each.
*/
private sumOfOriginalEstimates(issues) {
IssueManager issueManager = componentManager.getIssueManager();
Long sum = 0
for (Issue i in issues){
iObj = issueManager.getIssueObject(i.getId());
if (iObj) {
original_estimate = iObj.getOriginalEstimate()
if (original_estimate) {
sum += original_estimate
}
}
}
return sum
}

/*
* Executes JQL search.
*/
private List<DocumentIssueImpl> runJQLSearch(user, jqlQuery) {
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery);
List<DocumentIssueImpl> queryResults;

if (parseResult && parseResult.isValid()) {
try {
SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
queryResults = searchResult.getIssues();

log.debug("Valid JQL: " + jqlQuery);
log.debug(queryResults);
} catch (e) {
log.debug(e);
}
} else {
log.debug("Invalid JQL: " + jqlQuery);
}

return queryResults;
}

/*
* Retrieves user object
*/
private User getUser(user) {
UserUtil userUtil = ComponentAccessor.getUserUtil();
return userUtil.getUserObject(user);
}

 

Thanks

1 answer

Looks like you're running into some issues centering around how the APIs around users changed. See https://scriptrunner.adaptavist.com/latest/jira/releases/UpgradingToJira7.html#_user_replaced_with_applicationuser

I'd recommend either changing your helper methods to take/return an ApplicationUser object or just use 'def'. 

For example:

private ApplicationUser getUser(user) {
    def userManager = ComponentAccessor.userManager
    userManager.getUserByName(user)
}

You may notice that I'm using the userManager instead of userUtil.getUserObject. That's simply because the getUserObject was deprecated in 7.0, but it will still work.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

180 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you