Skip to main content
Example scripts
arrow icon
To homepage
Jira Software
Data centre icon
Data Center

Select Tempo Account Automatically at Issue Creation

Features
Listeners
Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira Software (7.7 - 8.6)
compatibility bullet
ScriptRunner For Jira (5.6.14)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.accounts.account.api.Account
import com.tempoplugin.accounts.account.api.AccountService

@WithPlugin("com.tempoplugin.tempo-accounts")

@PluginModule
AccountService accountService

def issue = event.issue
// The issue is an Epic, nothing to do here
if (issue.issueType.name == "Epic") {
    return
}
// Epic is obtained throughout epic link
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueLinkManager = ComponentAccessor.issueLinkManager
def epicLink = issueLinkManager.getLinkCollection(issue, loggedInUser, false).getInwardIssues("Epic-Story Link")
// There is no epic link, nothing to do here
if (!epicLink || epicLink.empty) {
    return
}

def epic = epicLink.first()
// There is no epic, nothing to do here
if (!epic) {
    return
}
// Account field from epic link is obtained
def customFieldManager = ComponentAccessor.customFieldManager
def epicAccountField = customFieldManager.getCustomFieldObjects(epic).find { it.name == "Account" }
def epicAccount = epic.getCustomFieldValue(epicAccountField) as Account
// Related epic has no account defined, nothing to do here
if (!epicAccount) {
    return
}
// Account field from issue is obtained
def issueAccountField = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Account" }
def selectedAccountOnIssue = issue.getCustomFieldValue(issueAccountField) as Account
def accountsRegisteredForProject = (accountService.getAccountsByProject(issue.projectObject.id).returnedValue as List<Account>)*.key
def globalAccounts = (accountService.globalAccounts.returnedValue as List<Account>)*.key

// If the account is configured for the target issue or it is a global account then update
if (epicAccount.key in accountsRegisteredForProject || epicAccount.key in globalAccounts) {
    issueAccountField.updateValue(null, issue, new ModifiedValue(selectedAccountOnIssue, epicAccount), new DefaultIssueChangeHolder())
    // A custom comment is added to the issue in order to notify that the issue account has been changed automatically
    def commentManager = ComponentAccessor.commentManager
    def body = "The Tempo Account was changed ${!selectedAccountOnIssue || selectedAccountOnIssue?.id == -1 ? '' : "from ${selectedAccountOnIssue} "}to ${epicAccount} by the Scriptrunner validation script"
    commentManager.create(issue, loggedInUser, body, false)
}
Having an issue with this script?
Report it here