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,300,064
Community Members
 
Community Events
165
Community Groups

Listener - Setting assignee based off Component Lead

I have a custom scriptrunner listener that checks if the Components field was updated, and if it was retrieve the Component lead for the first component in the list.  Then I want it to set the assignee to that component lead.

I know it is retrieving the component lead correctly, but the script is not setting the assignee as expected and I am not sure why.

I must be missing something, can someone help me know what I am doing wrong?  Here is my code:

 

import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import org.ofbiz.core.entity.GenericValue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;

def int icount = 1
def String DefaultAssignee = ""

// Checking if the "Components" field value has changed.
def event = event as IssueEvent;
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
def issue = event.getIssue() as MutableIssue;

def changelog = event.getChangeLog();
def changedFields = findFieldsMentionedInChangeLog(changelog)

// Component field has changed, then set the Component Lead as the Assignee
if (changedFields.contains("Component")) {
    // def String ComponentName = issue.components.name
    def String ComponentDefaultAssignee = issue.components.lead
    def ilength = ComponentDefaultAssignee.length()

    // If Component has multiple values, retrieve the Lead for the first component.
    if (ilength > 0) {
        while (icount <= 7) {
            DefaultAssignee = DefaultAssignee + ComponentDefaultAssignee[icount]
            icount = icount + 1
        }

        // Find the user name for the user to set as the assignee
        def user = ComponentAccessor.userManager.getUserByName(DefaultAssignee)

        // Set the assignee field to the component lead
        issue.setAssignee(user)
        issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
    }
}

1 answer

1 accepted

0 votes
Answer accepted

I can't see anything wrong with your update method. B

I'm not sure what's going on with your while loop, however. And maybe some part of the script is missing. I don't see when "user" is defined and where "findFieldsMentionedInChangeLog" is defined.

The whole thing can be simplified to:

/* get the changed fields above */
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
if
(changedFields.contains("Component")) {
def componentLeads = issue.components*.componentLead
if (componentLeads) {
issue.assignee = componentLeads.first()
ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

The one thing I'll add is that you are missing code to re-index the issue. After this listener runs, you will be able to see the new assignee when you open the issue, but if you search for issues with that assignee, the issue will not come up because the issue will not have been added to search index.

Here is how I re-index an issue:

import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils

boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

Thank you for your help.

The loop was doing what the simplified code you gave me was doing, so I have updated my code.  I have also added the index code too and now everything is working.

Below is my updated code that works if anyone else is interested.  Still not exactly sure why my code before wasn't working, other than maybe the first parameter to the updateIssue may have been incorrect.

________________________________________________

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;
import com.atlassian.jira.event.issue.IssueEvent;

// Checking if the "Components" field value has changed.
def event = event as IssueEvent;
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
def issue = event.getIssue() as MutableIssue;

def changelog = event.getChangeLog();
def changedFields = findFieldsMentionedInChangeLog(changelog)

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def componentLeads = issue.components*.componentLead

if (componentLeads) {

    // Set the assignee field to the component lead
    issue.setAssignee(componentLeads.first())
    ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

    // Performing reindex of issue
    boolean wasIndexing = ImportUtils.isIndexIssues()
    ImportUtils.setIndexIssues(true)
    ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
    ImportUtils.setIndexIssues(wasIndexing)
}

Be careful, I fear that your current code might run with every event update whether the component is modified or not.

Unless there is a new feature in ScriptRunner in a version that I have not upgraded to yet, the following is NOT a built-in function and probably doesn't do anything:

def changedFields = findFieldsMentionedInChangeLog(changelog)

 Also, you are not re-using this "changeFields" variable anywhere subsequently.

Here is a full script that should work:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
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

event = event as IssueEvent

// Checking if the "Components" field value has changed.
def changeHistoryManager = ComponentAccessor.changeHistoryManager;
def changeItems = changeHistoryManager.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans
if(!changeItems.any{it.field == 'Components'}){
return //nothing to do when Components is not changed
}

MutableIssue issue = event.issue as MutableIssue
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
// Component field has changed, then set the Component Lead as the Assignee
if (changedFields.contains("Component")) {
def componentLeads = issue.components*.componentLead
if (componentLeads) {
issue.assignee = componentLeads.first()
ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

// Performing reindex of issue
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
}

Hi,

I am trying to use the last script you supplied, but on this line:

if(!changeItems.any{it.field == 'Components'}){
return //nothing to do when Components is not changed
}

It is always returning true and exiting, no matter what field is being updated.  Any idea why? 

Try with 

if(!changeItems.any{it.field == 'Component/s'}){
return //nothing to do when Components is not changed
}

Generally, the changeItem field is the same string that you'll see in your audit history tab for your issue. If you see something else inf your instance, try with that.

The Change History is using "Component/s" and I have tried that, but I get the same result.  It never recognizes this field has been modified.

I finally figured out it is using just "Component", without the "s".  I have done a bit of testing, and it is now working.  Strange that it doesn't have the s on the field name though.

Thanks again for your help!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

An update on Jira Software customer feedback – June 2022

Hello Atlassian Community! Feedback from customers like you has helped us shape and improve Jira Software. As Head of Product, Jira Software, I wanted to take this opportunity to share an update on...

51 views 1 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you