Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,369,821
Community Members
 
Community Events
168
Community Groups

How to modify a filter with groovy

Hello,

There is the task to modify monthly the filter like "issuetype = Bug AND created >= 2022-10-20", that is instead of 10 some variable should be used.

To schedule the update, I'm going to use the Jira Automation.

Appreciate any help.

BR,

Ivan

2 answers

2 accepted

2 votes
Answer accepted

I can help with a groovy script to modify a filter. 

But I'm not sure this business case necessarily requires it.

If for example, you're interested in all issues created in the previous month you can use strict JQL with relative date functions:

created > StartOfMonth(-1) and created < EndOfMonth(-1)

Right now, this shows all issues created in August. On October 1 it will start to show all issues created in the month of September

If you need "exactly" issues created after the 20th of the month... that won't work. But you could have issues created since 10days before the start of the month (that will be after 20th or 21st)

created > StartOfMonth(-10d)

Here is a short script to retrieve and update a filter:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequestManager

def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager)
def searchService = ComponentAccessor.getComponent(SearchService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def filterId = 123456L
def monthVariable = 10
def filter = searchRequestManager.getSearchRequestById(filterId)
def jql = """issuetype = Bug AND created >= 2022-$monthVariable-20"""
def parseResult = searchService.parseQuery(currentUser, jql)
assert parseResult.valid: parseResult.errors

filter.query = parseResult.query
searchRequestManager.update(currentUser, filter)

User executing the script must have permissions to edit the filter. 

Hi Peter-Dave,

I see your doubt and share it - yes, it's not a big deal once a month to change a filter just manually. Maybe because of it I haven't found any examples of this kind to start from. But they want some automation.

Regarding the query, it makes sense in format like created >= 2022-XX-20, as they need  tickets since some date till a date when they require it. It's not always the end of a month.

Thanks a lot, Peter-Dave.

I'll try it. Probably instead of monthVariable = 10 anything like now.getMonth() to be used, as the script will run by schedule?

There is no "now" in groovy.

You can do 

new Date().getMonth() + 1

+1 because the getMonth returns the zero-base month index

You can also use: 

import java.text.SimpleDateFormat

new SimpleDateFormat('M').format(new Date())

Or to get "09"

import java.text.SimpleDateFormat

new SimpleDateFormat('MM').format(new Date())

Or even get your full date for the current year:

import java.text.SimpleDateFormat

new SimpleDateFormat('YYYY-MM-20').format(new Date())

Then if using full date calculation, would it look like this?

def fullDataVariable = SimpleDateFormat('YYYY-MM-20').format(new Date())

//

def jql = """issuetype = Bug AND created >= $fullDataVariable"""

You need to create an instance of the SimpleDateFormat

So either 

def formattedDate = new SimpleDateFormat('YYYY-MM-20').format(new Date())
def jql = """issuetype = Bug AND created >= "$formattedDate" """

Or

def formatter = new SimpleDateFormat('YYYY-MM-20)
def formattedDate = formatter.format(new Date())
def jql = """issuetype = Bug AND created >= "$formattedDate" """

 

Sorry for my inexperience and thanks a ton for your patience and the good lesson. Will try everything tomorrow.

Peter-Dave,

Thanks a lot again.

 

BR,

Ivan

0 votes
Answer accepted
Alex Koxaras Community Leader Sep 15, 2022

Hi @Ivan D 

Why don't you use scriptrunner's Jobs?

Hi Alex,

It's not essential how to schedule the execution. I'm more familiar with the automation and it has such action as running groovy scripts.

The main concern for me is the script.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events