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()
}
}