Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Updating Security Level via Groovy

Filzah Aziz November 13, 2020

I'm trying to bulk update the security level of a project (more than 10k issues hence not using the in house bulk update functionality). This is my script. I can't seem to set the security level.

What returns are all the issues in the JQL but can't seem to set the security level. Anyone has any ideas?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.MutableIssue

// Issues returned from that JQL will get altered
final searchQuery = "project in ('TEST") and level is EMPTY"

// Get some components
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// Perform the JQL search
def query = jqlQueryParser.parseQuery(searchQuery)
def results = searchProvider.search(query, user, PagerFilter.unlimitedFilter)

// Iterate all results to update each issue
results.issues.each {
def issue = issueManager.getIssueObject(it.id)
issue.setSecurityLevelId(12769)
}

1 answer

1 accepted

2 votes
Answer accepted
Hana Kučerová
Community Champion
November 14, 2020

Hi @Filzah Aziz

I think the main problem is, that you are setting security level id, but you are not updating the issues.

Please, try something like this:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query

Long securityLevelId = 12769
String query = 'project in ("TEST") and level is EMPTY'

IssueManager issueManager = ComponentAccessor.getIssueManager()
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Query searchQuery = jqlQueryParser.parseQuery(query)
SearchResults<Issue> results = searchService.search(user, searchQuery, PagerFilter.getUnlimitedFilter())
List<Issue> foundIssues = results.getResults()
foundIssues.each { Issue foundIssue ->
MutableIssue issue = issueManager.getIssueObject(foundIssue.getId())
issue.setSecurityLevelId(securityLevelId)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

I didn't test it, so please, be careful and do some tests on smaller amount of issues. Maybe you will also need to perform the reindex after bulk update.

Filzah Aziz November 16, 2020

@Hana Kučerová  Thanks for taking the time to answer my question. I see what you mean. I tried using your script however there is an error: 

Capture.PNG

It is saying that the class com.atlassian.jira.issue.search.SearchResults takes no parameters.

Hana Kučerová
Community Champion
November 16, 2020

Hi @Filzah Aziz ,

which version of Jira Server and ScriptRunner do you have? Thank you.

Filzah Aziz November 16, 2020

Jira server: 7.13.12

Scriptrunner: 6.5.0

Hana Kučerová
Community Champion
November 16, 2020

@Filzah Aziz The previous code should work in newer versions, searchService and results changed in time, so please try to replace:

SearchResults<Issue> results = searchService.search(user, searchQuery, PagerFilter.getUnlimitedFilter())
List<Issue> foundIssues = results.getResults()

with

SearchResults results = searchService.search(user, searchQuery, PagerFilter.getUnlimitedFilter())
List<Issue> foundIssues = results.getIssues()
Filzah Aziz November 16, 2020

This works- thank you for helping. And definitely need to do a reindex right after!

Hana Kučerová
Community Champion
November 16, 2020

Hi @Filzah Aziz 

glad to help :-).

Would you please mark my answer as accepted? This action will mark the question as resolved and it can also help other users in the future to find the solution, if they have similar problem. Thank you!

Suggest an answer

Log in or Sign up to answer