ScriptRunner Groovy Script based on issue type for specific group or role

Daniel Mendes February 26, 2018

Hi!

Could you please assist me with the script for routing specific issue type (during the Create transition) to a group or role? Is this possible? Is there an alternative solution?

 

Requirements:

when:

Project = Net

Issuetype = Task

route issues to:

Group or Role = developers

 

Appreciate any help.

 

Regards,

Daniel

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
February 26, 2018

Could you please clarify what do you mean by "route issues to"?

Daniel Mendes February 26, 2018

Hi Ivan,

 

When creating a ticket, from the issue type drop-down, the issue type 'Task' should only be available to users from the group/role 'developers'.

That's the requirement.

Or maybe if not possible to hide it from everyone else, only let proceed and create issues the users that belong to that group/role. The others would get an error message or something.

Let me know.

 

Thanks,

Daniel

Ivan Tovbin
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.
February 26, 2018

Hi Daniel, 

Cheers for the clarification. I see two options here:

1) Create a scripted validator on the 'Create issue' screen, that will check if the current user belongs to either 'Developers' group or 'Developers' project role. If he doesn't then the issue won't be created and an error message will be displayed.

2) Configure issue security first. Then create a scripted post function that will set a proper security level for all issues which belong to a certain issue type. That way anyone can create then but only select users/groups will be able to see them.

So it's up to you which approach suits your requirement the best.

Daniel Mendes February 27, 2018

Thanks for the ideas Ivan.

I have a custom script that I shall be adapting to my needs and will set it as a validator. I believe that's the most straight-forward solution for me.

Will let you know in case something changes.

Have a good day!

Ivan Tovbin
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.
February 27, 2018

If you need help with your script I might be able to help, so feel free to let me know.

Daniel Mendes February 27, 2018

Yes please.

I got the below script working for groups however, I need the same for project role instead. The reason is that I want to give the project lead autonomy to add/remove users that can create Tasks.

Do you think you can help me to tweak this script to work with roles? Or maybe you have a different script...

Thank you.

 

import com.atlassian.jira.component.ComponentAccessor

import com.opensymphony.workflow.InvalidInputException

 

def groupManager = ComponentAccessor.groupManager

def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

 

if(issue.issueType.name == 'Task'){

    if(!groupManager.isUserInGroup(user, 'developers')){

        throw new InvalidInputException("issueType",

            "Tasks can only be created by Developers")

    }

}

Ivan Tovbin
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.
February 27, 2018

Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.security.roles.ProjectRoleManager


def roleMgr = ComponentAccessor.getComponent(ProjectRoleManager)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def role = roleMgr.getProjectRole("role name")


if(issue.issueType.name == 'Task'){

if(roleMgr.isUserInProjectRole(currentUser, role, issue.getProjectObject() == false){

throw new InvalidInputException("issueType",

"Tasks can only be created by Developers")

}

}
Daniel Mendes February 28, 2018

I am getting the below error. There was also a missing closing parentheses in the original script. The screen shot below already has that fixed.

Let me know. Thanks.Screen Shot 2018-02-28 at 8.55.23 AM.png

Ivan Tovbin
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.
February 28, 2018

Indeed, I missed a closing parenthesis! It should be like this:

if(roleMgr.isUserInProjectRole(currentUser, role, issue.getProjectObject()) == false)

Other than that I believe you are facing a common ScriptRunner issue with static type checking not always being accurate. More info here. So I guess you can simply ignore it in this very case. 

Daniel Mendes February 28, 2018

I got this working now. It was really the lack of parentheses that was throwing everything off.

Thanks very much for your support on this Ivan.

Have a good day!

 

As a reference for others in a similar case, here's the final script:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.security.roles.ProjectRoleManager

def roleMgr = ComponentAccessor.getComponent(ProjectRoleManager)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def role = roleMgr.getProjectRole("NET - Developers")


if(issue.issueType.name == 'Task'){

if(roleMgr.isUserInProjectRole(currentUser, role, issue.getProjectObject()) == false){

throw new InvalidInputException("issueType",

"Tasks can only be created by Developers")

}

}
Ivan Tovbin
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.
February 28, 2018

Great news! Glad I could help. Please mark this solution as "Accepted" if it resolved your issue.

Daniel Mendes February 28, 2018

Done. thanks.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events