How to move bugs to top of backlog after creation with scriptrunner?

flaimo October 21, 2017

Is there a way to move issues with the issue type "bug" to the top of the backlog (Kanplan planning mode) programmatically using scriptrunner, after creation?

1 answer

0 votes
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 19, 2017

Yup! By updating the Rank custom field. To do that, you need to access the Jira Software (formerly known as Agile, formerly known as Greenhopper...) API.

import com.atlassian.greenhopper.api.rank.RankService
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.pyxis.greenhopper.jira")
@JiraAgileBean RankService rankService
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def rankField = customFieldManager.getCustomFieldObjects(issue).find{it.name == "Rank"}
rankService.rankFirst(currentUser, rankField.idAsLong, issue)
flaimo November 20, 2017

Thanks for the code. I implemented it as a Script Listener after an issue has been created. It gets executed without errors, but when I switch back to the Backlog, Bugs (Defects) are still at the bottom.

 

Screen_Shot_2017-11-20_at_10_44_01.png

Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 20, 2017

It may be failing silently. Perhaps we just need to add some error handling so you get appropriate logs:


import com.atlassian.greenhopper.api.rank.RankService
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.pyxis.greenhopper.jira")
@JiraAgileBean RankService rankService
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def rankField = customFieldManager.getCustomFieldObjects(issue).find{it.name == "Rank"}
def outcome = rankService.rankFirst(currentUser, rankField.idAsLong, issue)
if (outcome.isValid()) {
log.debug "Updated issue rank"
} else {
log.warn outcome.warningCollection?.warnings
log.error outcome.errorCollection?.errorMessages
}
Jason September 17, 2020

If it helps anyone else, for me, the above error handling let me know that my underlying failure was:

"This board has recently been configured to use the Rank field. The system must be re-indexed before you can rank issues. Ask your administrator to perform a manual re-index."

From what I gather, it turns out you can't rank an issue on creation, possibly because some required rank information hasn't been filled in yet.

Putting the same script code into a thread that delays briefly before executing fixed my issue.

import com.atlassian.greenhopper.api.rank.RankService
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

@WithPlugin('com.pyxis.greenhopper.jira')

@JiraAgileBean
RankService rankService


// Apparently the ranking code might run right AFTER this transition, so we're putting it in a thread
// to hopefully achieve our desired result, even if a bit delayed.

Thread thread = new Thread(new Runnable() {
void run() {
Thread.sleep(1000)

long rankFieldId = 10009;
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName('jira.api.admin')

log.warn("Ranking new issue " + issue.getKey() + " to top of global backlog")
def outcome = rankService.rankFirst(user, rankFieldId, issue);

if (outcome.isValid()) {
log.debug "Updated issue rank"
} else {
log.warn outcome.warningCollection?.warnings
log.error outcome.errorCollection?.errorMessages
}
}
})
thread.start()

Suggest an answer

Log in or Sign up to answer