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

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

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)

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

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
}

Suggest an answer

Log in or Join to answer
Community showcase
Emilee Spencer
Published yesterday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

50 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot