Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
titlerankchangeinfo_listener.groovy
/**
 * Populates 'Rank Last Changed', 'Rank Last Changed By' and 'Rank Prior To Last Change' custom fields, for later use by the 'Rank Change Info' script field. 
 * 
*/
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.datetime.DateTimeFormatterFactory
import com.atlassian.jira.datetime.DateTimeStyle
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption

def dateTimeFormatterFactory = ComponentAccessor.getComponentOfType(DateTimeFormatterFactory)
def formatter = dateTimeFormatterFactory.formatter().forLoggedInUser().withStyle(DateTimeStyle.DATE_PICKER)

IssueManager issueManager = ComponentAccessor.getIssueManager()
// event is of type com.atlassian.greenhopper.service.lexorank.balance.LexoRankChangeEvent. There is no JavaDoc on the web, so I had to figure out the structure by decompiling. There is a getIssueId() method, called here:
if (event && event?.issueId) {
        //log.warn "Event re-ranked: " + event.getProperties()
        def Issue issue = issueManager.getIssueObject(event.issueId as Long)
    if (issue) {
        def String dateStr = formatter.format(event.time)
        def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
        CustomField timeCf = ComponentAccessor.customFieldManager.getCustomFieldObject(16121) // Rank Last Changed
        CustomField userCf = ComponentAccessor.customFieldManager.getCustomFieldObject(16122) // Rank Last Changed By
        CustomField pastrankCf = ComponentAccessor.customFieldManager.getCustomFieldObject(16123) // Rank Prior To Last Change
        issue.setCustomFieldValue(timeCf, event.time?.toTimestamp())
        issue.setCustomFieldValue(userCf, currentUser)
        def List<String> pastranks = (issue.getCustomFieldValue(pastrankCf) as String)?.split(" ")?.toList()
        if (!pastranks) pastranks = []
        // call getNewRank()
        pastranks.add(0, event.newRank as String)
        pastranks = [pastranks[0], pastranks[1]] - null
        issue.setCustomFieldValue(pastrankCf, pastranks.join(" "));
        issue.store()
        log.info("Updated ${issue} '${timeCf}' field value to ${dateStr}, '${userCf}' field value to ${currentUser}")
        def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
        issueIndexingService.reIndex(issue)
        // Useful guide to updating issues: https://community.atlassian.com/t5/Agile-articles/Three-ways-to-update-an-issue-in-Jira-Java-Api/ba-p/736585
        issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false);
    }
}

...

Next, in Jira, type 'gg' (admin shortcut) 'Script Listeners' to bring up the ScriptRunner Script Listeners page. Add a new Custom listener, listening to event LexoRankChangeEvent, and invoking our groovy script:

Image RemovedImage Added

There will be a lot of some unavoidable static type checking errors because ScriptRunner is not able to find the LexoRankChangeEvent class, which is technically part of a plugin.

...