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.RankServiceimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.scriptrunner.runner.customisers.JiraAgileBeanimport com.onresolve.scriptrunner.runner.customisers.WithPlugin@WithPlugin("com.pyxis.greenhopper.jira")@JiraAgileBean RankService rankServicedef currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUserdef customFieldManager = ComponentAccessor.customFieldManagerdef 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.
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.RankServiceimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.scriptrunner.runner.customisers.JiraAgileBeanimport com.onresolve.scriptrunner.runner.customisers.WithPlugin@WithPlugin("com.pyxis.greenhopper.jira")@JiraAgileBean RankService rankServicedef currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUserdef customFieldManager = ComponentAccessor.customFieldManagerdef 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}
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.RankServiceimport com.atlassian.jira.issue.fields.CustomFieldimport com.onresolve.scriptrunner.runner.customisers.WithPluginimport com.onresolve.scriptrunner.runner.customisers.JiraAgileBeanimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.user.ApplicationUser
@WithPlugin('com.pyxis.greenhopper.jira')
@JiraAgileBeanRankService 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?.warningslog.error outcome.errorCollection?.errorMessages}}})thread.start()
It looks like you're new here. Sign in or register to get started.