So what are workflow conditions? They determine whether a workflow action is available or not. Considering the importance of workflow in installations and how there is a need to restrict the actions either to a set of people, roles etc or based on some criteria (Eg: the field is not empty!), writing workflow conditions is inevitable.
As usual, have your skeleton plugin ready. Let us start with the atlassian-plugin.xml which you can find in your skeleton plugin.
The workflow condition module in the plugin descriptor looks like this:
Now we come to the next important class which is the condition-class. This is the place where the actual condition is checked and we need to extend AbstractJiraCondition here. We will more about both these classes shortly.
We also have 3 views in a condition depending on your requirement (Much similar to post functions!). view, input-parameters and edit-parameters. As you might have guessed already, input-parameters and edit-parameters are required only if there are user inputs while setting the post function.
Let us now look a bit deeper.
As mentioned earlier, this is the class to define the inputs and set the velocity context. In out case we have a single input 'project role'. Here is what the class look like:
getRoleName(descriptor) is the method that retrieves the role from the descriptor and that is done as follows:
We have one more method getDescriptorParams in the class and this just return a map of sanitized parameters which will be passed into workflow plugin instances from the values in array form submitted by velocity. More info on that here.
getProjectRoles() does nothing but retrieves all the project roles:
Let us now move to the actual condition class. Here is how it looks like:
Before We wrap up, let us quickly look at the velocity templates. We are using the same template 'edit-roleCondition.vm' for both input-parameters and edit-parameters.
view-roleCondition.vm looks like this:
We now have our condition ready. Deploy the plugin into jira-home/plugins/installed-plugins (WEB-INF/lib if you created plugin-1 version), Create the Condition in a valid workflow, Add yourself to the relevant project roles and test it!!
More details on workflow condition module can be found here! Njoy!!
Download the full source code at the end of the page. And let us know your comments/feedback!
Note: The tutorial is just to explain the basic concepts of Conditions. Modify the buisness logic accordingly!