Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,299,254
Community Members
 
Community Events
165
Community Groups

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

Edited

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

2 votes
Answer accepted

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)

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

Thank you for taking the time for clearing the errors. 

1 vote
Answer accepted

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

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

 

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

Thank you for the insight

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

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

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);
}

"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.

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.

issue.components*.name 

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

264 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you