Workflow validator for current assignee is in projectrole

Adolfo Casari
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.
October 10, 2012

Is there any plugin that can validate if the current assignee is in a given project role when executing a transition?

Thank you,

5 answers

1 accepted

0 votes
Answer accepted
Bhushan Nagaraj
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.
October 10, 2012

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
Adolfo Casari
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.
October 10, 2012

Do you have the jar for the plugin?

Bhushan Nagaraj
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.
October 10, 2012

Doesn't allow me to upload jar files here. Give me your email id and I can share it

Adolfo Casari
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.
October 15, 2012

adolfo.casari at gmail.com

Thank you,

DI2E Licensing
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.
March 4, 2015

Can you send the jar to us? sysadmin@di2e.net . THANKS!

0 votes
LukasG December 5, 2016

Yes, you can use the "User in Project Role" Validator from the Plugin Workflow Essentials for JIRA (not free)

Андрей Зонин June 6, 2019

180$

0 votes
Adolfo Casari
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.
October 10, 2012

Hi,

Yes, if you can share the code i'd appreciate it.

Thank you.

0 votes
Bhushan Nagaraj
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.
October 10, 2012

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

0 votes
Vishwajeet Singh
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.
October 10, 2012

Best bet would be using jira script runner and writing a custom validator

Adolfo Casari
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.
October 10, 2012

Hi, if you can share the code i'd appreciate it.

Thank you.

Renjith Pillai
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.
October 13, 2012

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)

Suggest an answer

Log in or Sign up to answer