Changing default security level for specific issue type

Hi jira community,

I need to change security level for specific issue type for once jira project. I found this script and I tried using it.

*************************

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.opensymphony.user.User
import org.apache.log4j.Category
import org.ofbiz.core.entity.GenericValue

log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.debug ("PostFunction function running")

// Set the following according to what you want to be private
def secLevelString = "Internal"
def privateIssueTypes = ["Risk", "Bug"]

if (privateIssueTypes.contains(issue.issueTypeObject.name)) {

ComponentManager componentManager = ComponentManager.getInstance()
IssueSecurityLevelManager securityLevelManager = componentManager.getComponentInstanceOfType(com.atlassian.jira.issue.security.IssueSecurityLevelManager)

IssueSecuritySchemeManager issueSecuritySchemeManager = ManagerFactory.getIssueSecuritySchemeManager()
IssueSecurityLevelManager issueSecurityLevelManager = ManagerFactory.getIssueSecurityLevelManager()

GenericValue srcProjectGV = issue.getProject()
def issueSecurityScheme = issueSecuritySchemeManager.getSchemes(srcProjectGV).size() == 0 ? null : issueSecuritySchemeManager.getSchemes(srcProjectGV)[0]

User currentUser = componentManager.getJiraAuthenticationContext().getUser()
def secLevelGv = issueSecurityLevelManager.getUsersSecurityLevels(issue.getGenericValue(), currentUser).find ({
it.get("name") == secLevelString
});

if (secLevelGv) {
log.debug ("Set ${issue.getKey()} to $secLevelString")
issue.setSecurityLevel (secLevelGv)
issue.store()

}
else {
log.error ("Could not find security level for $secLevelString")
}
}

log4j.category.com.onresolve.jira.groovy = DEBUG, console, filelog
log4j.additivity.com.onresolve.jira.groovy = false

*****************************************************

I've added that script in the "create event" so that the script will run during issue creation.

I tried creating an issue but it does not change the security level. I checked the logs and I found this:

***************************************************

/secure/CreateIssueDetails.jspa [onresolve.jira.groovy.PostFunction] Could not find security level for Internal

***************************************************

I changed the value of "secLevelString" to "Private" but the same issue exist. It just changed Internal to private.

I tried to open the Database of Jira and I found this table "schemeissuesecuritylevels" and I found the corresponding "ID" for security level "Internal". I changed the value of "secLevelString" to corresponding ID number I found in the database but the same issue exist.

May I know what is missing in my script? Do you have suggestion/s to resolve this issue.

Thanks,

GV

3 answers

This widget could not be displayed.

Seems to be an old question, but, there is a built-in script for this now:https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-SetIssueSecurity

You just need to set the condition to check for the issue type you care about:

issue.issueTypeObject.name == "Bug"

wow, thanks Jamie! This is definitely going to save a lot of time than building my own code :D Thank you x 1000 :)

Hello Jamie,

I have researched and discovered that you have given this same answer for several people and it has worked for all of them.

Please tell me what I am doing wrong.  I place the  following script in the conditions:

issue.issueType.name == 'Error' || issue.issueType.name == 'Issue' || issue.issueType.name == 'Enhancement Request'

I then set the Name of the Security level to the one I want.  I place this in the Create Post-Function.

However when I create the issue, the issues keeps resulting to the default Security Level which is different from the one I want to set.

I'm sure the answer is simple but I am confused and drawing a blank right now.

This widget could not be displayed.

Few things that comes to mind:

1) Did you set the Issue Security Scheme before running this script? It should be under Issues -> Issue Security Schemes. That name on there should match exactly (case sensitive) to find the security issue security type

2) I am not sure why you set secLevelGv based by getUsersSecurityLevels? Cuz if you did setup issue security scheme(#1),then all you have to do is secLevelGv = issueSecurityLevelManager.getSecurityLevelsByName(auditLevelString).iterator().next();

So I did simliar things last year and I use groovy script and add it to run as part of post-functions:

import com.atlassian.jira.ComponentManager   
import com.atlassian.jira.ManagerFactory   
import com.atlassian.jira.issue.security.IssueSecurityLevelManager   
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager   
import com.opensymphony.user.User   
import org.apache.log4j.Category   
import org.ofbiz.core.entity.GenericValue   

log = Category.getInstance("com.accenture.jira.groovy.PostFunction")   
  
// Set the following according to what you want to be private   
def excludeLevelString = "ExcludeIssueSecurity"  
def privateIssueTypes = ["Testing Issue Type"]   
  	
ComponentManager componentManager = ComponentManager.getInstance()   
  
IssueSecuritySchemeManager issueSecuritySchemeManager = ManagerFactory.getIssueSecuritySchemeManager()   
IssueSecurityLevelManager issueSecurityLevelManager = ManagerFactory.getIssueSecurityLevelManager()   
  
GenericValue srcProjectGV = issue.getProject()   
def issueSecurityScheme = issueSecuritySchemeManager.getSchemes(srcProjectGV).size() == 0 ? null : issueSecuritySchemeManager.getSchemes(srcProjectGV)[0]   
def secLevelGv = null

if (	issueSecurityScheme != null){
    	if (privateIssueTypes.contains(issue.issueTypeObject.name)) {   
     				secLevelGv = issueSecurityLevelManager.getSecurityLevelsByName(excludeLevelString).iterator().next();
     	
		//now set the SecLev
		if (secLevelGv){
			issue.setSecurityLevel(secLevelGv)   
      		issue.store()   
		}
	}
}


This widget could not be displayed.

in case someone finds this article.... you can now do this with the built in 'Bulk Update' feature from the Issues screen

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

269 views 5 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you