Forums

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

I have this script but I want to customize it as per the requirement below-

Priyanka khare
Contributor
April 14, 2021

Hello,

 

I have this script but I want to customize it as per the requirement below-

 

 

I want that if my issue (for example ABC-103 is having the issue rank- 5) is moved/transitioned to production then the issue (whichever it is with issue rank -6 ) should change its value from 6 to 5.

similarly, if an issue ABC-110 (any random issue) moves to production with an issue rank say -10 the issue with issue rank -11 must change its issue rank to 10.

 

 

 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue;

String String = "Select List";
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomFieldManager CustomFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = CustomFieldManager.getCustomFieldObjectByName(String);

def availableOptions = ComponentAccessor.optionsManager.getOptions(cf.getRelevantConfig(issue))

// find the custom field option valuedef currValue = availableOptions.find { it == issue.getCustomFieldValue(cf) }

// convert the option value to a long data typedef optionValue = Long.valueOf(currValue.toString())

// decrement the valueLong newValue = optionValue-1;

// find the option value that match the decrement valuedef optionToSet = availableOptions.find { it.toString() == newValue.toString() }

// update the new option if foundif(optionToSet)
{
  cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf),optionToSet), changeHolder);
}

 

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
April 14, 2021

Hi @Priyanka khare just trying to get more information. So the use case is, you have list of tasks (sorted by issue rank)

  • ABC-2 (rank = 1)
  • ABC-4 (rank = 2)
  • ABC-3 (rank = 3)
  • ABC-1 (rank = 4)
  • ABC-5 (rank = 5)
  • ABC-6 (rank = 6)
  • ABC-7 (rank = null)

When ABC-1 goes to production, list is changed to

  • ABC-2 (rank = 1)
  • ABC-4 (rank = 2)
  • ABC-3 (rank = 3)
  • ABC-5 (rank = 4)
  • ABC-6 (rank = 5)
  • ABC-7 (rank = null)
  • ABC-1 (rank = null)

Is it correct?

Priyanka khare
Contributor
April 14, 2021

Hello, @Martin Bayer [MoroSystems, s.r.o.] , yes I am trying to achieve exactly that.

Let me know if you have any solution.

 

Thanks in advance.

 

Regards,

Priyanka

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
April 15, 2021

Hi @Priyanka khare , are you working with Jira API? I do not have solution for this exact problem. It is not so complicated so I will just try to describe what is the necessary to do (with short code snippets) and you can ask for more help if you can't make it, ok? :)

  1. you need to find all the issues which has any value in field Select list
  2. you need to sort the issues based on the Select list value
  3. forEach the collection of sorted issues
  4. if issue which is current for forEach is equals to current issue (which executes postfunction)  you need to clean the value of the Select list field (set to null)
  5. for other issues you need to decrease the value of Select list field
Priyanka khare
Contributor
April 16, 2021

hello @Martin Bayer [MoroSystems, s.r.o.] ,

 

Thank you so much for the code snippet,

 

Do u think the script below is going in the right direction in anyway-

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.ModifiedValue;


SearchService.ParseResult parseQuery(ApplicationUser searcher,
String query)

jqlSearch = '" Project Ranking" is not EMPTY'
parseResult = searchService.parseQuery(user, jqlSearch)


if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
} else {
log.elog("Invalid JQL: " + jqlSearch)
return (1)
}

 

I am new to scriptrunner and groovy and would like to seek your help in-

  1. you need to sort the issues based on the Select list value
  2. forEach the collection of sorted issues
  3. if issue which is current for forEach is equals to current issue (which executes postfunction)  you need to clean the value of the Select list field (set to null)
  4. for other issues you need to decrease the value of Select list field

can u provide me any referenced document for the points you have suggested here? I tried to find the code snippets for the same but did not find any,

 

any help would be highly appreciated.

 

Regards,

Priyanka

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
April 18, 2021

Hi @Priyanka khare , I'm really sorry but unfortunatelly I do not have enough time to write the whole script.

You can get a lot of resources on following web pages:

I hope, you will find out this resources usefull. Get back to this thread when you have something at least partially working. We can fix small mistakes/bugs together.

Suggest an answer

Log in or Sign up to answer