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 Sign up to answer
Community showcase
Posted Mar 28, 2018 in Jira Software

Can a company’s culture make or break agile adoption?

Can a new-to-agile team survive and thrive in a non-agile culture? If so, what advice would you give to those trying to be agile in a non-agile culture? What's the key(s) to success? Share your thoug...

17,654 views 18 20
Join discussion

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