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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.