Forums

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

Using ScriptRunner to restrict priority

apidcock February 19, 2020

I am trying to use ScriptRunner to restrict a couple of Priority choices for a particular project, however the documentation is deprecated, and I'm on mac which means I don't get code-completion, so I need some help (a link to up to date docs or just the code I'm missing?)

This is the code I use (from the example given)

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def constantsManager = ComponentAccessor.getConstantsManager()

def userUtil = ComponentAccessor.getUserUtil()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (!userUtil.getGroupNamesForUser(currentUser.name).contains("boardgames")) {

def allowedPriorities = constantsManager.getPriorities().findAll {
it.id.toInteger() > 2
}.collectEntries {
[(it.id): it.name]
}

getFieldById(PRIORITY).setFieldOptions(allowedPriorities)
}

Result: Nothing.  All the priorities are showing.

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Alejandro Suárez García
Atlassian Partner
February 19, 2020

Without testing sound good to me. Where did you put this code in?

It should go to the initialiser, in other hand your code only filters the priorities with id > 2, and in my experience the priority ids its something like 5 digit number.

apidcock February 20, 2020

Priorities have a sequence and an ID number, at least in the database (and I don't know anywhere else they are shown.)
Ours sequences run from 1 to 11, and

our IDs run from 1 to 11

Yes, it is the initialiser

I amended the code to this:

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def constantsManager = ComponentAccessor.getConstantsManager()

def userUtil = ComponentAccessor.getUserUtil()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (!userUtil.getGroupNamesForUser(currentUser.name).contains("boardgames")) {

def allowedPriorities = constantsManager.getPriorities().findAll {
( it.id.toInteger() != 2 && it.id.toInteger() != 3 )
}.collectEntries {
[(it.id): it.name]
}

getFieldById(PRIORITY).setFieldOptions(allowedPriorities)
}

and it does nothing at all.

Alejandro Suárez García
Atlassian Partner
February 20, 2020

hmmm, I guess the problem could be with the group condition? can you remove it and test?

Or maybe if you get the priorities with the priority manager:

def priorityManager = ComponentAccessor.getComponent(PriorityManager)

I cant tell any more issues with your code.

Alejandro Suárez García
Atlassian Partner
February 21, 2020

One more question, do you have JavaScript enabled in your web browser? Behaviours is client sided, it inyects javaScript, and if you dont have this enabled you wont see any changes. 

apidcock March 6, 2020

Yes, javascript enabled.

I changed the role to "jira-administrator"

Is there an updated version of the script somewhere?

The statements

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser() 
def allowedPriorities = constantsManager.getPriorityObjects().findAll {

have yellow warnings that they are deprecated (but the code suggested in (!) is not recognized)

And the statement

getFieldById(PRIORITY).setFieldOptions(allowedPriorities) 

has a red x in the script console (but not in the initialiser)

 

BTW our JIRA is 7.2.4 and
our ScriptRunner is 5.4.28 - issue? Should be OK right, according to the version history https://marketplace.atlassian.com/apps/6820/scriptrunner-for-jira/version-history

LIndsay Kiefer
Contributor
April 24, 2020

I am having the same issue. once the code is applied then users in our training group can still see all priorities. here is what i have in the initialiser

 

import com.atlassian.jira.issue.IssueFieldConstants
import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def priorityField = getFieldById(IssueFieldConstants.PRIORITY)
def constantsManager = ComponentAccessor.getConstantsManager()
def userUtil = ComponentAccessor.getUserUtil()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (!userUtil.getGroupNamesForUser(currentUser.name).contains("Training")) {

def allowedPriorities = constantsManager.getPriorities().findAll {
it.id.toInteger() > 2
}.collectEntries {
[(it.id): it.name]
}

getFieldById("Priority").setFieldOptions(allowedPriorities)
}

TAGS
AUG Leaders

Atlassian Community Events