Forums

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

Edit script for count the single transition

siva
Contributor
February 10, 2022

Hello,

We are using  script runner scripted custom field that counts the all used  transitions  from open to resolved status.

For example if we had transitions A,B and C from open to resolved status the present script counts the all 3 used transitions but i need only "A" transition count only can any  one please  help with the script to count only "A" transition.

Here the script below

import com.atlassian.jira.component.ComponentAccessor

//Fetching the Issue History objects and filtering it to 'status' field, which reflects the Status changes
def changedStatus = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)?.findAll {it.field == "status"}
def result = changedStatus.findAll{
(it.froms["1"] && it.tos["5"])
//For each fetched entries, check if the 'from' (source) and 'to' (destination) statuses match the configured IDs
}
result.size() //Returns the count of the result of the matched IDs

 

Thanks in advance,

Siva.

2 answers

2 accepted

1 vote
Answer accepted
Fabio Racobaldo _Catworkx_
Community Champion
February 11, 2022

Hi @siva ,

please try this code :

import java.util.Arrays;
import java.util.List;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.changehistory.ChangeHistory;
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager;
import com.atlassian.jira.issue.history.ChangeItemBean;

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
String[] statuses = {"1","2","3","4","5"};

List<ChangeItemBean> list = changeHistoryManager.getChangeItemsForField(issue, "status");
int count = 0;
for(ChangeItemBean ci : list){
if(Arrays.asList(statuses).contains(ci.getFrom()) && Arrays.asList(statuses).contains(ci.getTo())){
count++;
}
}
return count;

Hope this helps,

Fabio

siva
Contributor
February 11, 2022

@Fabio Racobaldo _Catworkx_ 

Thanks for the reply, Just want to give small clarification here the 1 and 5 are status ID'S 

1=open

2=resolved

So, here when status moved from open to resolved status. And we had multiple transitions  A, B and C between open and closed status  present the existing script counts all used A,D,C transition  and i need only A transition count only 

A.png

As you can see above picture from among A,B and C transitions i need only A transition count only 

And i placed your script and it's showing the error here the error logs

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script224.groovy: 11: unexpected token: 1 @ line 11, column 22.
   String[] statuses = {"1","5"};
                        ^

1 error

.Please help me with this.

Thanks,

Siva.

Fabio Racobaldo _Catworkx_
Community Champion
February 11, 2022

Hi @siva ,

use the following fix :

String[] statuses = ["1","5"];

ChangeItem contains just information about from and to status without info about transition. Therefore, you can't perform desired check.

Fabio

0 votes
Answer accepted
Tuncay Senturk _Snapbytes_
Community Champion
February 15, 2022

Hi @siva 

As far as I understand you want to count the specific transitions.

Assuming there are three transitions from Open status to Resolved status:

  1. T1: Open -> Resolved
  2. T2: Open -> Resolved
  3. T3: Open -> Resolved

you want to count only T1.

If that's the case, I'm afraid Jira does not track the transition. It keeps track at the database level, but I don't think there is a way to get it at the API level. So, you can count all transitions from A status to B status as the way @Fabio Racobaldo _Catworkx_ suggests or you can keep a counter field and increase it within the T1 transition with a post function. But in the latter way, you can't do that retrospectively. I mean, you won't get the count for the issues which already have those transitions in issue history.

I hope I was clear.

Tuncay

Suggest an answer

Log in or Sign up to answer