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

1 votes

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 :)

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


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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,813 views 11 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot