I am having trouble trying to update filters using ScriptRunner plugin. Edited

Hi,

I am having trouble trying to update filters using ScriptRunner plugin.

Below is my script I want to use to update filters of all the users using fixVersion in the filter.


Following are the issues I am facing:


1) Getting list of active users.


I tried it 2 ways using UserUtil and UserSearchService, Both are giving different values for user count.
(We have dealt with disabling users differently historically. In some cases we disable the user by unchecking the Active flag and in other cases we did it by removing the user from all the groups he is part of.)
What is the right way to get all the active users on the instance?

2) searchService.parseQuery is returning a null value and setQuery is complaining about that and the script fails.


The newQueryString has the updated query with replaced values but the parseQuery is returning null values.
Can you please point me in the right direction?

Appreciate your time and inputs.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.query.*
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService

UserUtil userUtil = ComponentAccessor.getUserUtil()
def activeUsers = userUtil.getUsers().findAll{userUtil.getGroupsForUser(it.getName()).size() > 0}

def userSearchService = ComponentAccessor.getComponent(UserSearchService)
def userSearchParams = new UserSearchParams.Builder()
.allowEmptyQuery(true)
.ignorePermissionCheck(true)
.maxResults(1000)
.build()
List<ApplicationUser> usersList = userSearchService.findUsers("", userSearchParams);
def activeUsersList = usersList.findAll{userUtil.getGroupsForUser(it.getName()).size() > 0}

def searchRequestService = ComponentAccessor.getComponent(SearchRequestService)
def searchService = ComponentAccessor.getComponent(SearchService)
def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def ctx = new JiraServiceContextImpl(loggedInUser)

log.debug(activeUsers.size())
log.debug(activeUsersList.size())

def fixVersionCount = 0
def output = ""
for (user in activeUsers) {
searchRequestService.getOwnedFilters(user).each() { searchRequest ->
def query = searchRequest.query
def queryString = searchService.getJqlString(query)
if (queryString.contains("fixVersion") && queryString.contains("11.11.11")) {
String newQueryString = queryString.replace("11.11.11", "22.22.22 (11.11.11)")
def newQuery = searchService.parseQuery(user, newQueryString).query
searchRequest.setQuery(newQuery)
searchRequestService.updateFilter(ctx, searchRequest)
log.debug ("\n\r $newQueryString \n\r $newQuery")
fixVersionCount += 1
output = "${output}${queryString}\n"
}
}
}
return output + "\n\r Total filters " + fixVersionCount

 

3 answers

1 accepted

Accepted Answer
1 vote
Henning Tietgens Community Champion Jan 15, 2018

You can get all active users using the following script (for recent Jira versions).

import javax.annotation.Nullable

import com.atlassian.application.api.ApplicationKey
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.application.ApplicationAuthorizationService
import com.atlassian.jira.application.ApplicationKeys
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.google.common.base.Predicate

def userSearchService = ComponentAccessor.getComponent(UserSearchService)

def appFilter = new ApplicationFilter([ApplicationKeys.SOFTWARE, ApplicationKeys.SERVICE_DESK])
def userSearchParams = new UserSearchParams.Builder()
.allowEmptyQuery(true)
.ignorePermissionCheck(true)
.maxResults(10000)
.includeInactive(false)
.filter(appFilter)
.build()

def allActiveUsers = userSearchService.findUsers("", userSearchParams)
allActiveUsers.size()

// OR filter for all applications provided with the constructor
class ApplicationFilter implements Predicate<User> {
Collection<ApplicationKey> appKeys
ApplicationAuthorizationService applicationAuthorizationService
UserManager userManager

ApplicationFilter(Collection<ApplicationKey> appKeys) {
this.appKeys = appKeys
applicationAuthorizationService = ComponentAccessor.getComponent(ApplicationAuthorizationService)
userManager = ComponentAccessor.userManager
}

@Override
boolean apply(@Nullable User input) {
def result = false
def user = userManager.getUserByName(input.name)
appKeys.each {
if (applicationAuthorizationService.canUseApplication(user, it)) {
result = true
}
}
return result
}
}

This script returns the number of users found. In this version all active users for the applications Jira Software and Jira Service Desk are found. You can adjust this by changing the params for the ApplicationFilter constructor call.

If you want to find all active users, even users without access to any application (not counting against your license), you should remove the .filter(appFilter) from the userSearchParams.

Henning

I was able to move a little further but still need your help.

As my repalcement string has parenthesis in it, I have to pass it in quotes.

for (user in activeUsers) {
searchRequestService.getOwnedFilters(user).each() { searchRequest ->
def query = searchRequest.query
def queryString = searchService.getJqlString(query)
if (queryString.contains("fixVersion") && queryString.contains("11.11.11")) {
String newQueryString = queryString.replace("11.11.11", "22.22.22 (11.11.11)")
def newQuery = searchService.parseQuery(user, newQueryString).query
searchRequest.setQuery(newQuery)
searchRequestService.updateFilter(ctx, searchRequest)
log.debug ("\n\r $newQueryString \n\r $newQuery")
fixVersionCount += 1
output = "${output}${queryString}\n"
}
}
}

But the problem now is, in the replaced query each condition is getting enclosed in curly braces.
For Ex:
The following JQL query -
project = AAA AND assignee = test AND fixVersion = "11.11.11" ORDER BY key ASC
Is replaced to -
{project = "AAA"} AND {assignee = "test"} AND {fixVersion = "22.22.22 (11.11.11)"} order by key ASC

Any ideas why?
Thanks

My Bad.

The code was working all the while.

The parseQuery output when printed/logged is displayed with curly braces but when passed to updateFilter is working as expected.

The issue is resolved but any inputs on the differences in active user counts will be helpful.

Thank you all.

John

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Thursday in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

1,803 views 4 6
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