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

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

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?

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

Cheers,
Shaun

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.

*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
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

77 views 0 3
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