Missed Team ’24? Catch up on announcements here.

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

Need groovy script to update the filters

Meghana K V
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 27, 2023

Need groovy script to run from Jira script runner to update the filters:

Fetch all the JQL filters based on issue type which is equal to "xyz",

then exclude the ones with issue type "Profile xyz", then update the filters with issue type as "feature".

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 29, 2023

Hi @Meghana K V

Could you provide a little bit more information, i.e. what Automation function are you using for this? Is the Issue Create or Transition Issue? 

It would be beneficial if you could share a screenshot of your Automation configuration.

I am requesting this so I can provide a sample script.

Thank you and Kind regards,

Ram

Meghana K V
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 2, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Thank you for your reply.

I have created the following script for the requirement mentioned above, but it's not working, please let me know what I am missing.

I have to fetch all the filters with issuetype = EPIC and exclude the ones with issuetype = Portfolio EPIC and then to the resulting filters, I need to change the issuetype to Feature.

import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.bc.filter.SearchRequestService

import com.atlassian.jira.issue.search.SearchRequest

import com.atlassian.jira.bc.user.search.UserSearchParams

import com.atlassian.jira.bc.user.search.UserSearchService

import com.atlassian.jira.bc.issue.search.SearchService

import com.atlassian.jira.bc.JiraServiceContextImpl

import java.lang.StringBuffer

 

SearchRequestService searchRequestService = ComponentAccessor.getComponent(SearchRequestService.class)

UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService)

SearchService searchService = ComponentAccessor.getComponent(SearchService)

def sb = new StringBuffer()

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def ctx = new JiraServiceContextImpl(loggedInUser)

 

UserSearchParams userSearchParams = new UserSearchParams.Builder()

    .allowEmptyQuery(true)

    .includeInactive(false)

    .ignorePermissionCheck(true)

    .maxResults(5000)

    .build()

 

// Define the issue type name to search for

def issueTypeName = "EPIC"

def excludedIssueTypeName = "Portfolio EPIC" // Add the excluded issue type name here

def newissueTypeName = "Feature"

 println "Starting script execution"

// Iterate over each user's filters

userSearchService.findUsers("", userSearchParams).each { ApplicationUser filter_owner ->

    try {

        searchRequestService.getOwnedFilters(filter_owner).each { SearchRequest ->

            def query = SearchRequest.query

           

            def queryString = searchService.getJqlString(query)

            println("Old Filter '$queryString' updated successfully.")

           

            if (queryString.contains(issueTypeName) && !queryString.contains(excludedIssueTypeName)) {

           

            String newQueryString = queryString.replace(issueTypeName, newissueTypeName)

           

            def newQuery = searchService.parseQuery(filter_owner, newQueryString).query

            sb.append("Filter for ${filter_owner.displayName}: ${query.name}, ${query.getPermissions().isPrivate() ? 'Private' : 'Shared'}\n")

            SearchRequest.setQuery(newQuery)

           

            searchRequestService.updateFilter(ctx, SearchRequest)

        }

    }

    }catch (Exception e) {

        // If filter is private or encounters an error

        sb.append("Unable to get filters for ${filter_owner.displayName} due to ${e}\n")

    }

}

 

// Output results

return sb.toString()

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events