Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner : Groovy Script - Only allow project role Administrators to create certain issue type

Rick van Twillert _TMC_
Contributor
September 25, 2018

Hi,

Im trying to add a custom script to the create issue transition to validate if a user doesnt have the projectrole administrator and the issue type is Initiative. If so, an exception should be thrown.

Resulting in the following code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.opensymphony.workflow.InvalidInputException

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

if (issue.issueType.name == "Initiative" && !projectRoleManager.isUserInProjectRole(currentUser, projectRoleManager.getProjectRole("Administrators"), issue.getProjectObject())) {
throw new InvalidInputException("Only users in the Administrator project role are allowed to create Initiatives.")
}

But I'm getting the following error:

2018-09-25 10:15:21,877 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-09-25 10:15:21,877 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: devProjectRole for class: Script60
	at Script60.run(Script60.groovy:9)

I have no idea what I'm doing wrong as the code looks flawless to me. Any ideas? Thanks in advance!

1 answer

2 votes
Mark Markov
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.
September 25, 2018

Hello @Rick van Twillert _TMC_

Where you place this code? Is this validator?

As i can see this code should work. Try to log everything, this may help you to understand what goes wrong.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.opensymphony.workflow.InvalidInputException

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

log.error("issue type: ${issue.issueType.name}")
log.error("currentUser: ${currentUser.getName()}")
log.error("progetcrole: ${projectRoleManager.getProjectRole("Administrators").getName()}")
log.error("Project: ${issue.getProjectObject().getName()}")
if (issue.issueType.name == "Initiative" && !projectRoleManager.isUserInProjectRole(currentUser, projectRoleManager.getProjectRole("Administrators"), issue.getProjectObject())) {
throw new InvalidInputException("Only users in the Administrator project role are allowed to create Initiatives.")
}
Rick van Twillert _TMC_
Contributor
September 25, 2018

Thanks for the suggestion for debugging Mark! I already tried this, but just for the getProjectRole method, without any hint.

After updating ScriptRunner the latest version, the script was working correctly. v5.4.14 to 5.4.28 on Jira 7.12.1

I was using our acceptance environment. Possibly some caching issue or other acceptance test might've conflicted... I can't explain it yet.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events