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,367,729
Community Members
 
Community Events
168
Community Groups

run a query in ScriptRunner JQL to know the number of issues in a project that were last updated mor

I would like to run a query in ScriptRunner JQL to know the number of issues in a project that were last updated more than 1 year ago. I am using the following code in the script console:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger;
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.web.bean.PagerFilter
import java.text.SimpleDateFormat
import com.atlassian.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.user.DelegatingApplicationUser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRoleManager

def log = Logger.getLogger("atlassian-jira.log")

List<Project> prList = ComponentAccessor.getProjectManager().getProjectObjects()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def projectManager = ComponentAccessor.projectManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)


for(Project myproject: prList){

def searchService = ComponentAccessor.getOSGiComponentInstanceOfType(SearchService.class)
ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def lastUpdatedDate = new Date(Long.MIN_VALUE)
def builder = JqlQueryBuilder.newBuilder()
builder.where().project(myproject.id)
def query = builder.buildQuery()

// edit this query to suit
def query2 = jqlQueryParser.parseQuery("project = "+myproject.getKey()+" and updated < 2021-08-25")

def results2 = searchProvider.search(query2, user, PagerFilter.getUnlimitedFilter())

log.warn("Total issues: ${results2.total}")


log.warn("Project name "+myproject.getName()+" |||Date: "+lastDate + " ||| number of issues: "+results.getResults().size() + " ||| Project Lead: "+ myproject.getProjectLead() +" ||| List of Admins: "+admins)



}


I am getting the error:
**groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.search.providers.LuceneSearchProvider.search() is applicable for argument types: (com.atlassian.query.QueryImpl, com.atlassian.jira.user.DelegatingApplicationUser...) values: [{project = "EAP"} AND {updated < "2021-08-25"}, mouh(JIRAUSER59200), ...]
Possible solutions: search(com.atlassian.jira.issue.search.SearchQuery, com.atlassian.jira.web.bean.PagerFilter), search(com.atlassian.jira.issue.search.SearchQuery, com.atlassian.jira.web.bean.PagerFilter, java.util.Set), search(com.atlassian.jira.issue.search.SearchQuery, org.apache.lucene.search.Collector), each(groovy.lang.Closure)
at Script19999.run(Script19999.groovy:60)**

but I am using some really simple code that I have found online in the scriptrunner website.
https://scriptrunner.adaptavist.com/4.3.6/jira/recipes/misc/running-a-jql-query.html
Anyone knows what I need to fix in order to be able to fix my problem and run my query?

1 answer

0 votes

I prefer to use the "SearchService" to perform searches

Try this bit of code:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import groovy.xml.MarkupBuilder

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

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
builder.table {
tr{
th "Project Name"
th "Old Issue Count"
th "Project Lead"
}
def allProjects = ComponentAccessor.projectManager.projectObjects
allProjects.each { project ->
def jql = "project=$project.key and Update < -365d"
def parseResult = searchService.parseQuery(currentUser, jql)
assert parseResult.valid: parseResult.errors
def resultCount = searchService.searchCount(currentUser, parseResult.query)
log.warn "Total issues: $resultCount"

tr{
td project.name
td resultCount
td project.projectLead.name
}
}
}
writer.toString()

If you want to access the individual issues:

def results = searchService.search(currentUser, parseResult.query, PagerFilter.unlimitedFilter).results
results.each{issue->
log.info "issue: $issue.key"
}

But be careful with "unlimitedFilter". If you have a lot of issues. The "results" variable can be very large and fill up all your memory.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events