Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Updating Security Level via Groovy

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 Leader Nov 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.

@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 Leader Nov 16, 2020

Hi @Filzah Aziz ,

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

Jira server: 7.13.12

Scriptrunner: 6.5.0

Hana Kučerová Community Leader Nov 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()

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

Hana Kučerová Community Leader Nov 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
TAGS
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

7,071 views 8 28
Join discussion

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