Workflow validator for current assignee is in projectrole

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
Accepted answer

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

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

adolfo.casari at gmail.com

Thank you,

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

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

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

Thank you.

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)

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,

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

Thank you.

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,725 views 17 21
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you