Restrict issue type to role or group (with or without scriptrunner)

Mark Fallaize February 20, 2020

Hi,

We are using many issue types, but want role "service desk team" to view and create issue type "Incidents" only. The rest of the issue types should not be available, either to view in the create menu or anywhere else. It will just cause confusion.

I have tried the following script from the scriptrunner library, but get an error. Any ideas?

Scripterror.PNG

2 answers

1 accepted

0 votes
Answer accepted
Mark Fallaize March 9, 2020

Restrict issue types.PNGHi Hana,

This worked.

Now users in a given role have the issue types available to them restricted accordingly.

It was also very simple to implement.

Thank you

Mark

0 votes
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 20, 2020

Hi @Mark Fallaize,

welcome to the Atlassian community!

 

Instead of ... (three dots) there should be listed some classes, which needs to be imported.

One of them would probablby be:

import com.atlassian.jira.component.ComponentAccessor

Also it looks like variable constantsManager is not defined, so you should add after line 5 (definition of projectRoleManager) line

def constantsManager = ComponentAccessor.getConstantsManager()

Would you please provide me url to the ScriptRunner Library, where you get the script? 

Thank you.

Mark Fallaize February 27, 2020

Hi Hana,

Thank you for your quick reply, I just returned back to office.

I used: https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/restricting-issue-types.html

With your additions, I get the following error message:

The script could not be compiled: The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script91.groovy: 2: unexpected token: com @ line 2, column 8. static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE ^ 1 error </pre>.

script 2.PNG

Sorry but I am new to this code, what might be the issue?

Thanks

Mark

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 28, 2020

Hi @Mark Fallaize,

sorry for the late response.

Please add the word "import" before the word "static" on the second line.

 

I tried to write the script for you - unfortunately I'm not able to try it right now because of the problems with my test instance, but I believe it could work.

Basically the script just checks, if logged in user has project role Service Desk Team. If so, then the type field is limited to only Incident and set to read only. There are no changes for other users without role Service Desk Team.

 

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

import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def allIssueTypes = ComponentAccessor.constantsManager.allIssueTypeObjects

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueTypeField = getFieldById(ISSUE_TYPE)

def remoteUsersRoles = projectRoleManager.getProjectRoles(user, issueContext.projectObject)*.name

if ("Service Desk Team" in remoteUsersRoles) {
    def availableIssueTypes = allIssueTypes.findAll { it.name in ["Incident"] }
    issueTypeField.setFieldOptions(availableIssueTypes)
    issueTypeField.setReadOnly(true)
}

Mark Fallaize March 2, 2020

Hi Hana,

Thank you for trying. Unfortunately we get the following error:

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getFieldById() is applicable for argument types: (java.lang.String) values: [issuetype] at Script94.run(Script94.groovy:10)script 3.PNG

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 3, 2020

Hi @Mark Fallaize,

to be honest - this is very strange. Would you please write me information, where exactly in your system do you have this code? Is it really saved as a Behaviours script? Thank you. 

Mark Fallaize March 4, 2020

Hi Hana,

Thank you for your reply.

The above errors were highlighted in the Scriptrunner console. I also tried it in the Script Editor with the same result.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2020

Hi @Mark Fallaize ,

thank you, now I understand, where the problem is. This script (and also the original one from the library) must be used in Behaviour, it won't work in "standard" ScriptRunner.

Please follow these instructions:

  1. In Administration of Jira click on Manage Apps -> Behaviours
  2. In the section Add Behaviour fill in some name and click on Add - new behaviour will be created
  3. In the row with newly created behaviour click on Add Mapping - now select all the projects, where you want to apply this restriction. You can also add Service Desk mapping, which means, that this restriction will be applied also when your team will try to create the issue from the portal.
  4. In the same row click on Fields - in the behaviour create initialiser and insert the provided code.
  5. Save the behaviour
  6. Test it :-)

Your settings should look something like this:behaviour.png

Steve Letch
Contributor
January 19, 2022

Anyone know how to get a 'not in' working? All I'm trying to do is to say, if the user is NOT IN "Users" role, basically the rest of the company, then restrict the issue type to Service Request, I'm using the template on the Behaviours site, but when I put in not in or != it doesnt work.

 

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

import
static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

// if the current user is in the Users role only, set the issue type to "Query", and lock it
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def remoteUsersRoles = projectRoleManager.getProjectRoles(user, issueContext.projectObject)*.name
if (remoteUsersRoles != ["Users"]) {
def constantsManager = ComponentAccessor.getConstantsManager()
def queryIssueType = constantsManager.getAllIssueTypeObjects().find { it.name == "Service Request" }
getFieldById(ISSUE_TYPE).with {
setFormValue(queryIssueType.id)
setReadOnly(true)
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events