Using the GreenHopper API to modify rank

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, 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

This widget could not be displayed.

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";
   println "Error: " + outcome.getErrorCollection().getErrorMessages();

Hope that helps,

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

*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?

This widget could not be displayed.

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?

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");


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.

This widget could not be displayed.

*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?

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

106 views 2 0
Join discussion

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