Assign issue to project role e.g. user group depending on custom field value (Select List (single choice))

Hi Script Runner Users,

I'm new to script runner and want to achieve thing which more of people are using (I think at least). Depending on value from custom field the issue should be assigned  to different user group.

if system=system1, than assign to system1_approval_group

if system=system2, than assign to system2_approval_group

I found post condition where assignment is to first user of project role. This would be also acceptable but I'm not able to extend this function depending on custom field.

Can someone help me, please?

Thanks a lot and nice regards!

4 answers

0 votes

You can't assign an issue to a group.  Only to individual users.

Hi Nic, yes, it should be first user of group or role. I can paste the code for assigning to 1st person in project role (but without most needed condition - depending on system)

 

Script Source

package com.onresolve.jira.groovy.canned.workflow.postfunctions

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.security.roles.ProjectRoleManager

import com.atlassian.jira.util.ErrorCollection

import com.onresolve.jira.groovy.canned.CannedScript

import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils

import org.apache.log4j.Logger

 

class AssignToRoleMember implements CannedScript {

 

    public static String FIELD_ROLE_ID = "FIELD_ROLE_ID"

    def log = Logger.getLogger(AssignToRoleMember.class)

    def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)

 

    String getName() {

        "Assign to first member of role"

    }

 

    String getDescription() {

        "Assign to the first member of the specified role"

    }

 

    List getCategories() {

        ["Function"]

    }

 

    List getParameters(Map<String, String> params) {

        [

            [

                Label: "Role",

                Name: FIELD_ROLE_ID,

               Type: "list",

                Values: CannedScriptUtils.getAllRoles(false),

                Description: "Specify the role the previous assignee must be in",

            ],

        ]

    }

 

    ErrorCollection doValidate(Map<String, String> params, boolean forPreview) {

        null

    }

 

    Map doScript(Map<String, Object> params) {

        def projectRoleId = params[FIELD_ROLE_ID] as Long

        def role = projectRoleManager.getProjectRole(projectRoleId)

        assert role // role must be valid

        def issue = params["issue"] as MutableIssue

        def actors = projectRoleManager.getProjectRoleActors(role, issue.getProjectObject())

        def roleUsers = actors.getUsers()

        if (roleUsers) {

            issue.setAssignee(roleUsers.toList().first())

        }

        else {

            log.warn ("Wanted to set the assignee to a member of the ${role.name} role, but it had no members")

        }

    }

 

    String getDescription(Map<String, String> params, boolean forPreview) {

        def projectRoleId = params[FIELD_ROLE_ID] as Long

        def role = projectRoleManager.getProjectRole(projectRoleId)

        "Assign this issue to the first member of the <b>${role.name}</b> role"

    }

    Boolean isFinalParamsPage(Map<String, String> params) {

        true

    }

}

You need to insert your code into the "doscript" function

0 votes

Hi,

I do not know much about script runner but JIRA Enhancer Plugin comes with this feature.

http://jiraenhancerplugin.com/documentation.html#assign-to-role-based-on-property-function

Tuncay

Hi Nic, and can you help me how the condition should look like? Thanks a lot!

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,525 views 15 21
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you