I've created a change request JSD project. It is used to terminate existing employees/contractors. When terminating an employee, I need to ensure that only HR staff can create such a request. Anyone can terminate a contractor.
I have a custom field called "Category" that has options a) Employee b) and Contractor. So if "Employee" is selected, I need some validator that ensures the issue create is a member of the HR Termination project role. I found a script online that I've modified, but it isn't quite working.
The following script throws the error no matter what option is selected, Employee or Contractor. So somehow this code isn't specifically checking to see what value was selected in my Category custom field.
Any help would be appreciated.
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRole
import com.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
ComponentManager componentManager = ComponentManager.getInstance();
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager;
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10201");
def cfCategory = (String)issue.getCustomFieldValue(customField);
ApplicationUser currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser();
ProjectRole hrRole = projectRoleManager.getProjectRole("HR Termination");
ProjectRoleActors hr = projectRoleManager.getProjectRoleActors(hrRole, issue.getProjectObject());
Boolean isHR = hr.contains(currentUser);
if (!isHR) {
if (cfCategory == "Employee")
return false
}
Try this:
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.security.roles.ProjectRoleManager
def projectManager = ComponentAccessor.projectManager
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
ApplicationUser currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser();
String customFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10201"));
if(customFieldValue == "Employee"){
def projectRole = projectRoleManager.getProjectRole("HR Termination")
def project = projectManager.getProjectByCurrentKey('IT')// enter your project key
def isExistinProjectRole = projectRoleManager.isUserInProjectRole(currentUser, projectRole, project)
if (!isExistinProjectRole) {
invalidInputException = new InvalidInputException("You don't have permission to open this ticket");
return null;
}
}
Notice that you need to change the project key according to your relevant project
Hi Neta,
Thanks for the response, but the solution is not working. As before, I get a validation error, but it is happening no matter what selection is made. It should only occur when "Employee" is selected.
Any ideas?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The script should run as script validator -> Custom script validator
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OK, I made the change and got the following error:
2018-07-18 15:54:52,874 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-07-18 15:54:52,874 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> groovy.lang.MissingPropertyException: No such property: JiraAuthenticationContext for class: com.atlassian.jira.component.ComponentAccessor Possible solutions: jiraAuthenticationContext at Script4026.run(Script4026.groovy:10)
The custom field "Category" that I am checking against is a single select dropdown field. Does that change the way that we are checking its value for 'Employee'?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No, the script should be fine because we take the field value and the field is a single select so there should be one string that the script should return.
It seems like the script can take the current user.
Let run the script on the reporter field (how should be automation the current user)
Try this:
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.security.roles.ProjectRoleManager
def projectManager = ComponentAccessor.projectManager
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
ApplicationUser reporter = issue.getReporter()
String customFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10201"));
if(customFieldValue == "Employee"){
def projectRole = projectRoleManager.getProjectRole("HR Termination")
def project = projectManager.getProjectByCurrentKey('IT')// enter your project key
def isExistinProjectRole = projectRoleManager.isUserInProjectRole(reporter, projectRole, project)
if (!isExistinProjectRole) {
invalidInputException = new InvalidInputException("You don't have permission to open this ticket");
return null;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That seems to have fixed the issue. I'll check a bit more. But I think you've got it. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great! if it's work for you, accept the answer :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Neta Elyakim !
Was curious if youd be able to guide on the cloud solution to this? My use case is almost exact to above, but the above script does not work for the cloud version that ive tried.
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In the cloud, custom validators are implemented using Jira expressions. If you can give me specifics, I can help you write one. However, I think it would be best to create a new question regarding the cloud.
Cheers
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Maciej Dudziak _Forgappify_ ,
I got it working with the logic below:
!issue.customfield_X || issue.customfield_X.value != "VALUE" || user.groups.includes("GROUP")
This states that if custom field X is a particular value, then user must be in this specific group to allow tranisition. This allowed for when custom field is not X, or is empty, the validator will allow to transition disregarding user group membership. Only requiring the group membership when X is that certain value.
Thanks all!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.