Restricting Issue Types based on project Role is not working with Epic

David Henriksson March 26, 2019

Hi!

I am trying to set a set of issuetypes on Create screen when user is trying to create an issue depending on which project role the user belongs to. I am doing this via a script. It is working quite good with one exception. For some reason Epic will not show up even though it is added in the list of issuetypes. I have logged the list of issuetypes (availableIssueTypes) and it is included in the list but for some reason it doesn't show up in the interface. It is working with all the other issuetypes.

Is this a bug?

My Code:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.issue.issuetype.IssueType;
import com.atlassian.jira.project.Project;
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE;

IssueTypeManager issueTypeManager = ComponentAccessor.getComponent(IssueTypeManager.class);
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class);
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();
ApplicationUser applicationUser = jiraAuthenticationContext.getLoggedInUser();

def issueTypeField = getFieldById("issuetype");

ProjectRole administrators = projectRoleManager.getProjectRole(10002L);

Project project = issueContext.getProjectObject();

boolean isUserAnAdministrator = projectRoleManager.isUserInProjectRole(applicationUser, administrators, project);

IssueType feature = issueTypeManager.getIssueType("10000"); //ISSUETYPE EPIC
IssueType programEpic = issueTypeManager.getIssueType("10100");
IssueType portfolioEpic = issueTypeManager.getIssueType("10200");

Collection<IssueType> allIssueTypes = project.getIssueTypes();
List<IssueType> issueTypesExceptFeatureAndEpics = new ArrayList<IssueType>();

for(IssueType issueType : allIssueTypes){
if (issueType != feature && issueType != programEpic && issueType != portfolioEpic) {
issueTypesExceptFeatureAndEpics.add(issueType);
}
}

def availableIssueTypes = [];

if (isUserAnAdministrator) {
availableIssueTypes.addAll(allIssueTypes);
}

else{
availableIssueTypes.addAll(issueTypesExceptFeatureAndEpics);
}

log.warn(availableIssueTypes);

issueTypeField.setFieldOptions(availableIssueTypes);

 

1 answer

0 votes
Matthew Clark
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.
April 5, 2019

Hi David

It looks like you are after for a Behaviour Initialiser script, but you did not mention so I cannot be certain.

However, if it is a behaviour, this script might help you achieve your requirement:

import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

//Only run this behaviour if the Create Button was clicked. i.e. only run for the Create screens
if(actionName == "Create" || actionName == "Create Issue") {

ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
//Get issue context project
Project projectObj = issueContext.getProjectObject()
//Get User Object
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
//Get the IssueType Form Field
def issueTypeField = getFieldById(ISSUE_TYPE)
//Get List of Roles Names for this user in this Project
def usersRoles = projectRoleManager.getProjectRoles(user, issueContext.projectObject)*.name
//Get the issue Types for this project
Collection<IssueType> projectIssueTypes = projectObj.getIssueTypes()
//Remove the IssueTypes with Epic in the Name
Collection<IssueType> allButEpicIssueTypes = projectIssueTypes.findAll { IssueType type ->
!type.name.contains("Epic")
}

if ("Administrators" in usersRoles) {
//If user is member of Administrators project role give all IssueTypes
issueTypeField.setFieldOptions(projectIssueTypes)
log.debug("Set Issue Types for Administrators: ${projectIssueTypes*.name}")
} else {
//All other cases only allow them to choose Non Epic Issue Types
issueTypeField.setFieldOptions(allButEpicIssueTypes)
log.debug("Set Issue Types for Non-Administrators : ${allButEpicIssueTypes*.name}")
}
}

Use this as the Initialiser and Map it to your Project and All Issue Types

David Henriksson April 10, 2019

Hi Matthew!

Thanks for trying to help. Correct, I am using Behaviour Initialiser.

Unfortunately your code doesn't solve the problem either. I have tried to simplify the code as much as possible but even this little piece of code doesn't solve my problem:

import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.component.ComponentAccessor;

def itm = ComponentAccessor.getComponent(IssueTypeManager)

getFieldById("issuetype").fieldOptions = [
"10000", //EPIC
"10100",
"10200"
].collect {
itm.getIssueType(it)
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events