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

How to modify a filter with groovy

Ivan D September 15, 2022

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

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 15, 2022

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)

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 15, 2022

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. 

Ivan D September 15, 2022

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.

Ivan D September 15, 2022

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?

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 15, 2022

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())
Ivan D September 15, 2022

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"""

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 15, 2022

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" """

 

Ivan D September 15, 2022

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

Ivan D September 26, 2022

Peter-Dave,

Thanks a lot again.

 

BR,

Ivan

ahmed
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!
November 27, 2022

I'm new to confluence APIs.

Please, can you tell me which dependencies to use?
to resolve this.

import com.atlassian.confluence.spaces.SpaceManager

I included most of the confluence libs but, they never got resolved.
I need the Gradle dependency line like this or maven XML of course.

 implementation group: 'com.atlassian.confluence', name: 'confluence-rest-client', version: '8.0.0-m012'
0 votes
Answer accepted
Alex Koxaras _Relational_
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 15, 2022

Hi @Ivan D 

Why don't you use scriptrunner's Jobs?

Ivan D September 15, 2022

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.

TAGS
AUG Leaders

Atlassian Community Events