Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Ranking code for tickets

Sam May 28, 2020

The below code is not satisfactory as the result does not shown as required. Could you please help me in that. Providing exact requirement and the code which I am using.
 

The below is the result for the code which is attached below.

Suppose if we have 3 tickets with rank 1 to 3 

Ticket      Rank   

1             1    

2             2 

3             3   

4             4...So on

Here the ticket 4 is changed with rank 3 and the result is as below. where rank 4 is missing

Ticket     Rank   

1            1   

2            2   

4            3   

3            5....So on

Here is the requirement for the code.
Suppose if we have 3 tickets with rank 1 to 3 

Ticket      Rank   

1             1    

2             2   

3             3 ....So on    

Then if we add another ticket 4 and the rank is given as 1 then the every ticket rank should be changed as given below.

Ticket     Rank   

4             1   

1             2   

2             3   

3             4....So on
The change of rank should be done from where the rank is changed if the rank 3 is given for 4th ticket

Ticket     Rank    1            1   

2            2   

4            3   

3            4....So on

Same way the process should be carried out through out the Jira project.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.search.SearchProvider

import com.atlassian.jira.jql.parser.JqlQueryParser

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.search.SearchQuery

import com.atlassian.jira.event.issue.IssueEvent

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.util.IssueChangeHolder

import com.atlassian.jira.issue.DocumentIssueImpl;

import com.atlassian.jira.issue.MutableIssue

import org.apache.log4j.Level

import org.apache.log4j.Logger

 

def myLog = Logger.getLogger("com.onresolve.jira.groovy")

myLog.setLevel(Level.DEBUG)

 

 

def issue = event.issue as Issue

def changeHolder = new DefaultIssueChangeHolder()

MutableIssue curIssue = (MutableIssue) issue

myLog.debug("Updated:Issue Key::--->" + issue.getKey());

 

def sourceField = "customfield_10201"

def target_1 = "customfield_10201"

 

def cfManager = ComponentAccessor.getCustomFieldManager()

def cfCond = cfManager.getCustomFieldObject(sourceField)

def tfCond = cfManager.getCustomFieldObject(target_1)

 

CustomField targetField_1 = cfManager.getCustomFieldObject(target_1)

def rank = issue.getCustomFieldValue(cfCond)

myLog.debug("rank:--->" + (int)rank)

 

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);

def searchProvider = ComponentAccessor.getComponent(SearchProvider);

def query = jqlQueryParser.parseQuery("project = PROJ and \"rank\">="+(int)rank); // sending the parameter to JQL Query. 

 

 

def searchQuery = SearchQuery.create(query, user).overrideSecurity(true);

def results = searchProvider.search(searchQuery, PagerFilter.getUnlimitedFilter())

 

com.atlassian.jira.issue.Issue[] returnVal = []

returnVal = results.getResults()*.document.collect{ComponentAccessor.issueManager.getIssueObject(it.getField('key').stringValue())}

// iterate returnValues (issues) & update Rank

myLog.debug("returnValue:Lenght:--->" + returnVal.length)

 

for(Issue issueObj : returnVal)

{

    if(!issueObj.getKey().equalsIgnoreCase( issue.getKey()))

    {

      int incrementNumber = (int)issueObj.getCustomFieldValue(cfCond)+1;

      

      myLog.debug("To Increment:--->"+ issueObj.getKey()  +":--->" + incrementNumber);

      tfCond.updateValue(null, issueObj, new ModifiedValue(issue.getCustomFieldValue(targetField_1), (java.lang.Double)incrementNumber),changeHolder)

      issueObj.store()

    }

   

}

 

 

 

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 3, 2020

Hi shashi,

If I understand your request here, you're using Jira Server and trying to implement a groovyscript (perhaps with scriptrunner) in order to maintain your own ranking system of issues in Jira. 

While it might be possible to accomplish this with a plugin like scriptrunner, I have to ask, have you considered instead trying to implement a custom Rank field as provided by Jira Software?  Jira Software natively has this field available to all instances.  It only creates a single Rank field out of the box, but in Jira Server, it is possible to create a second custom Rank field.  There is some details on how you could do this over in Changing a Rapid Board Rank field.

There are some details about how Jira Software's lexoranking works in Managing LexoRank. And also a youtube video on the details of how lexoranking works in https://www.youtube.com/watch?v=OjQv9xMoFbg

Instead of the issues have a numeric value, the use of a lexographic representation provides a much more comprehensive means to provide ranking space between values.  This helps a lot in the long term as it avoids Jira needing to adjust the values of all the issues every time a new issue is ranked above the rest.  Granted it can be more difficult to understand the lexo value when compared to a numeric one, but Jira Software doesn't typically even need to expose that data value to the end users.  Instead it just uses that value to determine where issues appear on a board or in a backlog, with higher ranked issues appearing above the others.

Even if you don't adopt a lexorank field, I think you will find that using sufficient spacing between ranks values will help tremendously when it comes to the overhead of tracking the values between issues over time. 

My apologies if you do not find this response helpful.  I understand that I don't have a clear solution for your ask here, but I would be interested to learn more about your ask here.  For example, how important is it to use a numeric value here?  What use case are you trying to solve for here with this proposed ranking system?

I hope this helps.

Andy

Suggest an answer

Log in or Sign up to answer