Restrict Issue Types from Behaviours Plugin

Sai Ram Kumar Singarapu January 14, 2019

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours

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

@BaseScript FieldBehaviours fieldBehaviours

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
def availableIssueTypes = []

if ("Users" in remoteUsersRoles) {
availableIssueTypes.addAll(allIssueTypes.findAll { it.name in ["Query", "General Request"] })
}

if ("Developers" in remoteUsersRoles) {
availableIssueTypes.addAll(allIssueTypes.findAll { it.name in ["Bug", "Task", "New Feature"] })
}

issueTypeField.setFieldOptions(availableIssueTypes)

 

 

I am using the above code provided by adaptavist on https://library.adaptavist.com but getting the below error:

/rest/scriptrunner/behaviours/latest/validatorsByPid.json [c.o.j.groovy.user.FormField] Unsupported type class com.atlassian.jira.issue.issuetype.IssueTypeImpl in setFieldOptions()

 

What class to be used for setting the Issue type field?

2 answers

1 accepted

0 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
January 14, 2019

Hi Sai, 

Can I ask you which version of SR you use ?

Sai Ram Kumar Singarapu January 14, 2019

 

Adaptavist Scriptrunner plugin version - 5.4.11

Thanos Batagiannis _Adaptavist_
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.
January 14, 2019

Hi Sai,

That feature was introduced in version 5.4.39 onwards. 

0 votes
herve ranchin March 31, 2020

For me it's perfectly works but only when you try to open a new issue on a new tab (with only project and issuetype menu)
if you have all the screen with all the other field, the behaviour doesn't activate and i see all the issue type...

 

any clue ?

herve ranchin March 31, 2020


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 availableIssueTypes = []
def remoteUsersRoles = ComponentAccessor.getGroupManager().isUserInGroup(user, "cop-socloud")
def remoteUsersRoles2 = ComponentAccessor.getGroupManager().isUserInGroup(user, "cop-exaly")

if (remoteUsersRoles) {
availableIssueTypes.addAll(allIssueTypes.findAll { it.id in ["10210","12207","10000","10700","10800","10211","10305","10400","11100","10308","10300","10302","10304","10301"]})
}
else {
availableIssueTypes.addAll(allIssueTypes.findAll { it.id in [ "11100", "10210"] })
}
issueTypeField.setFieldOptions(availableIssueTypes)

Suggest an answer

Log in or Sign up to answer