Using the GreenHopper API to modify rank

Rick Trudeau July 30, 2012

Has anyone managed to use the GreenHopper API to manipulate the global Rank value in Jira? This applies to Jira 5.0.7 and GreenHopper 5.10.5.1, but I believe Global Rank was introduced in GH 5.8

By default when you create a new issue in Jira, the new Issues's Rank is assigned the lowest rank.

That's a probelm for us, because we regularly clone an issue and want the rank of the cloned issue to be ranked immediately below the source issue. So I'm looking for a way to manipulate the global rank value via groovy scripts (or any other method) so that I can manually assign a rank value after the cloned issue is created.

Any references to scripting examples manipulating the GreenHopper global rank field would be appreciated.

3 answers

1 accepted

5 votes
Answer accepted
sclowes
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.
July 31, 2012

This is pretty easily done through the RankService. Here's some sample Groovy code (for use in the Script Runner plugin):

import com.atlassian.jira.ComponentManager
import com.atlassian.plugin.PluginAccessor
import com.atlassian.jira.bc.project.ProjectService

ComponentManager componentManager = ComponentManager.getInstance();
def jiraAuthenticationContext = componentManager.getJiraAuthenticationContext()
def issueManager = componentManager.getIssueManager()
def user = jiraAuthenticationContext.getUser()

PluginAccessor pluginAccessor = componentManager.getPluginAccessor();
Class rankServiceClass = pluginAccessor.getClassLoader().findClass("com.atlassian.greenhopper.api.rank.RankService");

def rankService = componentManager.getOSGiComponentInstanceOfType(rankServiceClass);

def rankField;
for (customField in rankService.getRankFields())
{
   println "Rank custom field " + customField.getId() + " " + customField.getName();
   if (rankField == null)
      rankField = customField;
}

def issueone = issueManager.getIssueObject("PRO-9");
def issuetwo = issueManager.getIssueObject("PRO-6");

def outcome = rankService.rankAfter(user, rankField.getIdAsLong(), issuetwo, issueone);
if (outcome.isValid())
   println "Re-ranked";
else
   println "Error: " + outcome.getErrorCollection().getErrorMessages();

Hope that helps,
Shaun

Rick Trudeau July 31, 2012

Exactly the type of example of I was looking for - thanks Shaun!

Rick Trudeau October 27, 2013

*Sigh*. We've upgraded to Jira 6.1 and are now trying GreenHopper/Agile v6.3.3 and unfortunately this code snippet no longer works.

Anyone know how to accomplish this in Jira 6.1?

0 votes
Rick Trudeau October 27, 2013

*Sigh*. We've upgraded to Jira 6.1 and are now trying GreenHopper/Agile v6.3.3 and unfortunately the code snipped below no longer works.

Anyone know how to accomplish this in Jira 6.1?

0 votes
Rick Trudeau September 5, 2012

I have a follow question that I'm hoping you also have an answer for.

I've added a postFunction to run this script example you provided so that newly cloned issues have their rank updated on issue creation. The problem I'm having is that most of my users doing the cloning operations intentionally don't have the permission to do ranking, so the rank operation fails because of this.

I need a way to execute my re-ranking script as a different user, one that does have the permission. From within the script context, is there a way to become a different user for the purpose of running the script?

sclowes
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.
September 5, 2012

Sure. As you can see in the code it gets the current user through jiraAuthenticationContext.getUser(), you can put any other user you'd like in there.

You can get another user with something like:

import com.atlassian.jira.ComponentManager

ComponentManager componentManager = ComponentManager.getInstance();
def userUtil = componentManager.getUserUtil()
def user = userUtil.getUser("admin");

Cheers,
Shaun

Rick Trudeau September 5, 2012

Thanks again Shaun, I posted another question wrt to this on answers.atlassian and got a suggestion that was very close to yours. Your help is much appreciated.

Suggest an answer

Log in or Sign up to answer