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

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.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

139 views 1 10
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