Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner: Get users who have updated project issues

padraik
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.
March 31, 2022

I am working on cleaning up an old instance of Jira that grew organically, and messily until bringing on a Jira Administrator. (me)

I need to create a list of users who have updated a ticket for a given project during the past month.

I'm new to Scriptrunner and Groovy, but have experience in other scripting languages and wondered if anyone can advise me on a smart way of tackling this.

It's fine for this to be run from the console

1 answer

1 vote
PD 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.
March 31, 2022

I found that an interesting little challenge and I whipped this up:

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

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

def projectKey = 'KEY'

def jql = "project=$projectKey and updated > -30d"

def parseResult = searchService.parseQuery(currentUser, jql)
assert parseResult.isValid(), parseResult.errors*.errorMessages

def allUsers = []
searchService.search(currentUser, parseResult.query, PagerFilter.unlimitedFilter).results.each{
def issue = issueManager.getIssueObject(it.id)
def issueUsers = changeHistoryManager.getAllChangeItems(issue)
.findAll { it.created > new Date() - 30 }
.collect { userManager.getUserByKey(it.userKey) }
.unique()
allUsers.addAll(issueUsers)
}

allUsers.unique()

This will first search for issues modified in the last 30 days.

Then look through the change history, but only for changes that happened in the last 30 days, and extract the list of unique users. 
It will then add that list of users to an allUser list.
When all issues have been processed, we return the list unique list of users over all the issues.

But what if you want a bit more details?

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

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

def projectKey = 'KEY'

def jql = "project=$projectKey and updated > -30d"

def parseResult = searchService.parseQuery(currentUser, jql)
assert parseResult.isValid(), parseResult.errors*.errorMessages

def userIssues = [:]
searchService.search(currentUser, parseResult.query, PagerFilter.unlimitedFilter).results.each {
def issue = issueManager.getIssueObject(it.id)
changeHistoryManager.getAllChangeItems(issue)
.findAll { it.created > new Date() - 30 }
.collect { userManager.getUserByKey(it.userKey) }
.unique()
.each { user ->
if (userIssues.containsKey(user.name)) {
userIssues[user.name] << issue.key
} else {
userIssues[user.name] = [issue.key]
}
}

}

userIssues.collect{user,issues-> "$user: ${issues.join(', ')}"}.join('<br>')

This will do the same as above with the search and history, but of just returning the list of users, we add the user to a Map object with the key being the username and the value being an array of issues that the user has modified in the last 30 days.
I made some effort to display decently in the console output.

Suggest an answer

Log in or Sign up to answer