Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,362,297
Community Members
 
Community Events
168
Community Groups

Convert existing Scriptrunner listener to write to the history table

Edited
ScriptRunner Listener
Installed version 6.58.0 using Jira DC
I've been attempting to convert the script below to one of the following, but each attempt has failed. Any suggestions would be greatly appreciate. 
  1. Update using issueManager
  2. Update using issueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.util.ImportUtils

def issue = event.issue as Issue
def assignee =issue.getAssignee()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dtField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("DTField")[0]
def cfConfig = devTeamField.getRelevantConfig(issue)
def changeHolder = new DefaultIssueChangeHolder()
def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)
def issueManager = ComponentAccessor.getIssueManager()
def groupManager = ComponentAccessor.getGroupManager()

//Dev-Teams
def Anml = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.toString() == 'Animal'}

//Set DT based on assignee group
if(groupManager.isUserInGroup(assignee, 'AnGroup')) {
dtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(dtField), Anml),changeHolder)
}

//10 other if else added here.

//For assignees that are not in a group, set DT to Unassigned
else {
dtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(dtField), null),changeHolder)
log.warn("Assignee not in a Group. DT Field was not auto-populated. Exit Script")
log.warn("Issue = '+ issue)
}

 

2 answers

1 accepted

1 vote
Answer accepted

Try something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def sendEmail = false
def dispatchEvent = EventDispatchOption.DO_NOT_DISPATCH //EventDispatchOption.ISSUE_UPDATED
def groupTeamMap = [
AnGroup:'Animal',
anotherGroup:'AnotherTeamOptionLabel'
]

def issue = event.issue as MutableIssue
def assignee =issue.assignee
def customFieldManager = ComponentAccessor.customFieldManager
def devTeamField = customFieldManager.getCustomFieldObjectsByName("DTField")[0]
def cfConfig = devTeamField.getRelevantConfig(issue)
def issueManager = ComponentAccessor.issueManager
def groupManager = ComponentAccessor.groupManager

def devTeamOptions = ComponentAccessor.optionsManager.getOptions(cfConfig)

def groupTeamItem = groupTeamMap.find{group, team->
groupManager.isUserInGroup(assignee, group)
}
def teamOption
if(!groupTeamItem){
log.warn("Assignee not in a Group. DT Field will be empty")
} else {
teamOption = devTeamOptions.find{it.value == groupTeamItem.value}
if(!teamOption){
log.error("Assignee belongs to group $teamOption.key wich maps to team option $teamOption.value which doesn't exist in DTField context")
}
}
//update the custom field
issue.setCustomFieldValue(devTeamField, teamOption)
//store the issue
issueManager.updateIssue(currentUser, issue, dispatchEvent, sendEmail)
//reindex the issue
def wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
ImportUtils.indexIssues = wasIndexing

After making a few modification, I got this to work. Thank you so much for your help. 

@Peter-Dave Sheehan  -

Is there a way to check for 2 events?

I need this to run at issue created and issue assignee. Tried adding instanceof and IssueEventBundle no luck. 

def dispatchEvent = EventDispatchOption.ISSUE_CREATED || EventDispatchOption.ISSUE_ASSIGNED

This code is not checking for an event, it's defining what kind of event the update transaction should trigger.

Both the IssueManager.updateIssue() method and IssueService.update() methods only allow a single EventDispatchOption

You define which event to check for in the Listener configuration screen. This selection will impact what type of object "event" from the following line is:

def issue = event.issue

When I use . . . 

def issue = event. issue

it breaks issueManager.updateIssue(currentUser, issue, dispatchEvent, sendEmail)

as it seems to only like . . . 

def issue = event.issue as MutableIssue

 

Should event.issue capture issue created and issue updated? 

You can use either 

def issue = event.issue

or

def issue = event.issue as MutableIssue

Both will work.

The first one might show some static type checking errors, but those aren't usually fatal. They're only telling you that the code checker can't determine for sure what type of data it's going to receive. So it doesn't know whether that variable will work downstream.

But I wasn't suggesting you remove the 'as MutableIssue'.

I was only trying to explain that the supplier variable "event" will be a different type of object based on the event you select in your listener configuration.

That this 'event' is different than the "dispatchEvent" that has to be supplied to the updateIssue method. 

Event is what you list to.

DispatchEvent is what new event you'll generate when you make an update from inside the listener. In fact, most of the time, I prefer to do "DO_NOT_DISPATCH" as suggested in my code. Otherwise, you could end up in a loop.

Thank you so much for that update. That makes perfect sense, as those are two different functions. 

I've been testing out several options, and can't get it to run on creation and also found that when it clears the field the index stops, so any update after that pulls in the field prior. Verified Issue Created is an active event and the create post functions are using issue create as well. 

I also have problems with the updates in Structure, but according to this post, it should work.  https://community.atlassian.com/t5/Jira-Software-questions/Sync-between-structure-and-issues/qaq-p/1748359?utm_source=atlcomm&utm_medium=email&utm_campaign=subscription_search_question&utm_content=topic

Any suggestions? 

Events: Issue Created, Issue Assigned

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils


//def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def sendEmail = false
def dispatchEvent = EventDispatchOption.ISSUE_UPDATED

def issue = event.issue as MutableIssue
def assignee =issue.assignee
def customFieldManager = ComponentAccessor.customFieldManager
def dTField = customFieldManager.getCustomFieldObjectsByName("DT")[0]
def cfConfig = devTeamField.getRelevantConfig(issue)
def issueManager = ComponentAccessor.issueManager
def groupManager = ComponentAccessor.groupManager

def AnimationDT = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.toString() == 'Animation'}
def AudioDT = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.toString() == 'Audio'}
def AutomationDT = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.toString() == 'Automation'}
def ArtDT = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.toString() == 'Art'}

log.warn("Issue = " + issue)
log.warn ("Assignee = " + issue.assignee)

//Set DT based on assignee sg group
if(groupManager.isUserInGroup(assignee, 'art-anim')) {
issue.setCustomFieldValue(dTField, AnimationDT)
issueManager.updateIssue(currentUser, issue, dispatchEvent, sendEmail) }

else if(groupManager.isUserInGroup(assignee, 'des-audio')) {
issue.setCustomFieldValue(dTField, AudioDT)
issueManager.updateIssue(currentUser, issue, dispatchEvent, sendEmail) }

else if(groupManager.isUserInGroup(assignee, 'eng-automation')) {
issue.setCustomFieldValue(dTField, AutomationDT)
issueManager.updateIssue(currentUser, issue, dispatchEvent, sendEmail) }

else {
issue.setCustomFieldValue(dTField, null)
issueManager.updateIssue(currentUser, issue, dispatchEvent, sendEmail)
log.warn("Assignee not in a DT Group. DT field cleared.")
log.warn("Issue = " + issue)
}
//reindex the issue
//issueIndexManager.reIndex(issue)
def wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
ImportUtils.indexIssues = wasIndexing

Do you get any log outputs when the indexing is skipped?

You can try to add a few additional log statement to try to get to the cause.

Added the event to the logs, still nothing shows up anytime I try to create a new issue. 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events