Changing default security level for specific issue type

Ron Duag May 29, 2012

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

1 vote
JamieA
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.
July 27, 2013

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"

Eva
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.
July 30, 2013

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

Aaron Whigham August 7, 2018

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.

0 votes
Ro December 27, 2017

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

0 votes
Eva
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.
July 26, 2013

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


Suggest an answer

Log in or Sign up to answer