Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Listener - Setting assignee based off Component Lead

patricia_scott_honeywell_com May 20, 2022

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 20, 2022

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)
patricia_scott_honeywell_com May 20, 2022

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)
}
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 20, 2022

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)
}
}
patricia_scott_honeywell_com June 7, 2022

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? 

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2022

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.

patricia_scott_honeywell_com June 8, 2022

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!

TAGS
AUG Leaders

Atlassian Community Events