Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Setting issue security level based on custom field value using a scriptrunner listener

Sysad January 27, 2021

Hi Community

I'm trying to set issue security level based on the cUntitled.pngustom field when issue created and when custom field is updated. Here is my code and am not sure where to go from here. Any help is appreciated.

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel

def issueManager = ComponentAccessor.getIssueManager()
def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'User County'}

if (issue.getCustomFieldValue(cf).toString().equals('County A'))
{
issue.setSecurityLevelId('10601')
}
else if (issue.getCustomFieldValue(cf).toString().equals('COunty B'))
{
issue.setSecurityLevelId('10602')
}
else// if county is empty then
{
issue.setSecurityLevelId('10603')
}

3 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 27, 2021

Hi

For starters, you can remove the static type checking errors (which are not always show- stopper, but in the case they are) by using com.atlassian.jira.issue.MutableIssue instead of com.atlassian.jira.issue.Issue and removing the single quotes around your security level Id.

But then, this only modifies an instance of an issue object in memory.

You need to persist that object into the database and into the index.

You can do that with

//additional import
import com.atlassian.jira.issue.index.IssueIndexingService
//additional service
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)

def sendEmailWithChange = false
def user= ComponentAccessor.jiraAuthenticationContext.loggedInUser //or use userManager to get a user that has permission to edit the issue

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,sendEmailWithChange )
indexingService.reIndex(issue)
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 27, 2021

Here taking your original script and adding my suggestions:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'User County'}

if (issue.getCustomFieldValue(cf).toString().equals('County A'))
{
issue.setSecurityLevelId(10601)
}
else if (issue.getCustomFieldValue(cf).toString().equals('COunty B'))
{
issue.setSecurityLevelId(10602)
}
else// if county is empty then
{
issue.setSecurityLevelId(10603)
}

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,false)
indexingService.reIndex(issue)

This should clear all the static type checking issues

Sysad January 27, 2021

Thank you for taking the time for clearing the errors. 

1 vote
Answer accepted
Ravi Sagar _Sparxsys_
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.
January 27, 2021

Hi @Sysad 

You are almost there. Try this code to update the security level.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def issue = ComponentAccessor.getIssueManager().getIssueObject("SD-1261")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
issue.setSecurityLevelId(10001)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()

issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,true)

I hope this code will give an idea of what you can add in your script.

Make sure security scheme is applied to the project.

Ravi

Sysad January 27, 2021

Hi @Ravi Sagar _Sparxsys_

 

This what I produced and looks like it is working! while creating issues and editing USER COUNTY filed on the created issue. Here is the code.

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService

def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'User County'}

if (issue.getCustomFieldValue(cf).toString().equals('ABCD'))
{
issue.setSecurityLevelId(10600)
}
else if (issue.getCustomFieldValue(cf).toString().equals('EFGH'))
{
issue.setSecurityLevelId(10602)
}
else if (issue.getCustomFieldValue(cf).toString().equals('IJKL'))
{
issue.setSecurityLevelId(10603)
}
else// if county is empty then
{
issue.setSecurityLevelId(10601)
}

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def sendEmailWithChange = false
def user= ComponentAccessor.jiraAuthenticationContext.loggedInUser //or use userManager to get a user that has permission to edit the issue
def issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,sendEmailWithChange )
indexingService.reIndex(issue)

 

But I do see few errors and for static type as suggested by @Peter-Dave Sheehan  to use MutableIssue, I get unable to resolve class Issuse at def issue =event.issue as Issue

 

Untitled.png

Untitled.png

 

Ravi Sagar _Sparxsys_
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.
January 27, 2021

Hi @Sysad 

You can ignore these static type checking errors because when you run this code as a post function or as a listener because then you will have issue, a binding available during the execution.

For testing you can always test your code in the console with an example issue like this.

def issue = ComponentAccessor.getIssueManager().getIssueObject("ISD-2")

.. and when pasting the script in the listener or post function comment this line.

Ravi

Sysad January 27, 2021

Thank you for the insight

0 votes
Rakesh Jajper March 29, 2022

Hi just wants to know how to check whats the ID for security level we define?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 29, 2022

In your project, under issue security, select "Edit Issue Security" under the "Actions" menu.

Then, hover over the actions for the security level for which you want to know the ID.

In the URL, you will see something like: &levelId=10230&schemeId=10330

The level id is 10230.

If you want to get it using scriptrunner console:

import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def project = ComponentAccessor.projectManager.getProjectByCurrentKey('SERP')
ComponentAccessor.issueSecurityLevelManager.getUsersSecurityLevels(project, currentUser).collect{"$it.id=$it.name"}.join('<br>')
Like Rakesh Jajper likes this
Rakesh Jajper March 29, 2022

Thanks Peter that helps

How can I achieve this requirement, I am using this code, but not sure where to put listener or Behavior.

And not sure if this is even correct, but I want to set security level as per component.

And if they change component it should set new security level

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService
 
def component = getFieldById("components")
def componentV = component.getValue()
if (componentV.toString().contains("NTD")) {
    issue.setSecurityLevelId(11104);
    }
else {
    issue.setSecurityLevelId(11107);
}
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 30, 2022

"Behaviour" code is always inherently different from most other code in Scriptrunner (listener, post function etc) because it is designed to interact with the screen DOM elements.

Whereas all other Scriptrunner script are designed to run at the server level on java objects.

So "getFieldById" is a behaviour method to get a field from the currently visible screen.

While "issue.setSecurityLevelId" is a java method to change an issue object.

You can't really mix the two (well, there are ways, but it's not recommended).

So you have to decide, do you want to modify the issue security level on the screen on behalf of the user as soon as they change the component (and perhaps lock the field from further changes). Or do you want to let the user pick whatever then react to the change in component using a listener or a workflow script?

These are two very different scenarios with different pros and cons.

One of which is that I'm not sure if Scriptrunner behaviours are capable of interacting with the issue security level drop-down in the create/edit screen.  They might be, I just haven't tried.

Rakesh Jajper March 30, 2022

Thanks Peter,

Look like I was able to make it work with listener if I set security level as per issue type selection, but do you know how to read component via listener.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 30, 2022
issue.components*.name 

will give you a list of component names associated with the issue.

TAGS
AUG Leaders

Atlassian Community Events