Is there any plugin that can validate if the current assignee is in a given project role when executing a transition?
Thank you,
Below is the code for 4 files you will be using in the plugin
1. AssigneeInProjectRole
public class AssigneeInProjectRole implements Validator { private static final Logger log = LoggerFactory.getLogger(AssigneeInProjectRole.class); public static final String PROJECT_ROLES="PROJECT_ROLES"; public static final String SELECTED_PROJECT_ROLES="SELECTED_PROJECT_ROLES"; private final ProjectRoleManager projectRoleManager; private final JiraAuthenticationContext authContext; public AssigneeInProjectRole(ProjectRoleManager projectRoleManager, JiraAuthenticationContext authContext) { this.projectRoleManager = projectRoleManager; this.authContext = authContext; } public void validate(Map transientVars, Map args, PropertySet ps) throws InvalidInputException { Issue issue = (Issue) transientVars.get("issue"); User user = authContext.getLoggedInUser(); String selectedProjectRoles = (String) args.get(SELECTED_PROJECT_ROLES); List<String> selectedProjectRolesList = Arrays.asList(selectedProjectRoles.split("\\s*,\\s*")); boolean userIsInProjectRole = false; for(String role:selectedProjectRolesList) { ProjectRole projectRole = projectRoleManager.getProjectRole(role); Project project = issue.getProjectObject(); ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRole, project); if(projectRoleActors.getUsers().contains(user)){ userIsInProjectRole = true; } } if(!userIsInProjectRole) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("User must be in one of the following project roles to perform this transition: "); for(String role:selectedProjectRolesList){stringBuilder.append(role+",");} throw new InvalidInputException(stringBuilder.substring(0,stringBuilder.toString().lastIndexOf(","))); } } }
2. AssigneeInProjectRoleFactory
public class AssigneeInProjectRoleFactory extends AbstractWorkflowPluginFactory implements WorkflowPluginValidatorFactory { public static final String PROJECT_ROLES="PROJECT_ROLES"; public static final String SELECTED_PROJECT_ROLES="SELECTED_PROJECT_ROLES"; private final ProjectRoleManager projectRoleManager; public AssigneeInProjectRoleFactory(ProjectRoleManager projectRoleManager) { this.projectRoleManager = projectRoleManager; } protected void getVelocityParamsForInput(Map velocityParams) { //the default message velocityParams.put(PROJECT_ROLES,projectRoleManager.getProjectRoles()); } protected void getVelocityParamsForEdit(Map velocityParams, AbstractDescriptor descriptor) { getVelocityParamsForInput(velocityParams); getVelocityParamsForView(velocityParams, descriptor); } protected void getVelocityParamsForView(Map velocityParams, AbstractDescriptor descriptor) { if (!(descriptor instanceof ValidatorDescriptor)) { throw new IllegalArgumentException("Descriptor must be a ValidatorDescriptor."); } ValidatorDescriptor validatorDescriptor = (ValidatorDescriptor) descriptor; velocityParams.put(SELECTED_PROJECT_ROLES, validatorDescriptor.getArgs().get(SELECTED_PROJECT_ROLES)); } public Map getDescriptorParams(Map validatorParams) { // Process The map String[] value = (String[])validatorParams.get(SELECTED_PROJECT_ROLES); StringBuilder stringBuilder = new StringBuilder(); for(String string:value){ stringBuilder.append(string + ","); } return EasyMap.build(SELECTED_PROJECT_ROLES, stringBuilder.toString().substring(0,stringBuilder.lastIndexOf(","))); } }
3. Edit view
<tr> <td class="fieldLabelArea"> Project Role: </td> <td nowrap> <select name="SELECTED_PROJECT_ROLES" id="SELECTED_PROJECT_ROLES" height="100px;" multiple="multiple"> #foreach ($role in $PROJECT_ROLES) <option value="$role" #if ($SELECTED_PROJECT_ROLES.contains($role.name)) SELECTED #end >$role</option> #end </select> </td> </tr>
4. View
Assignee must be in one of the project roles to perform this transition: <br /> $SELECTED_PROJECT_ROLES
Best bet would be using jira script runner and writing a custom validator
Hi Adolfo,
I have a custom plugin that does this. Let me know if you want me to just post the code for the validatorFactory and Validator class
Hi, if you can share the code i'd appreciate it.
Thank you.
Hi,
Yes, if you can share the code i'd appreciate it.
Do you have the jar for the plugin?
Doesn't allow me to upload jar files here. Give me your email id and I can share it
To extend what Vishwa said, you may use this code in the Script runner.
import com.atlassian.jira.ComponentManager import com.atlassian.jira.security.roles.ProjectRole import com.atlassian.jira.security.roles.ProjectRoleManager ComponentManager componentManager = ComponentManager.getInstance() ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager ProjectRole devRole = projectRoleManager.getProjectRole("Developers") projectRoleManager.isUserInProjectRole(issue.assignee, devRole, issue.projectObject)
adolfo.casari at gmail.com
Yes, you can use the "User in Project Role" Validator from the Plugin Workflow Essentials for JIRA (not free)
180$
It looks like you're new here. Sign in or register to get started.