It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Pushpkant Garg May 03, 2017

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

0 votes
Jonny Carter May 15, 2017

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

976 views 12 15
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you