How to list all Assignees in JQL query, using Script Runner or Python API

Gian Israel Nampi October 22, 2020

Hello Everyone,

 

I would like to know how can I use Script Runner to get all "Assignee" from the JQL query?

My JQL query sample is:

project = "project_name" AND "custom_field" = value AND status not in (Closed) AND assignee changed ON 2020-10-05"

After that I will need to update all the Assignee field to the original User assigned before the the changed date. I have tried testing some of the solved cases here in Community, but its not working on my end. can you give me an example code or guide that I can follow?

Thank you.

1 answer

0 votes
Ravi Sagar _Sparxsys_
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.
October 22, 2020

Hi @Gian Israel Nampi 

Share the code that you tried and let us see what is not working.

Ravi

Gian Israel Nampi October 22, 2020

Hi @Ravi Sagar _Sparxsys_ 

Thanks for checking in this issue.  Here is my tested code which is working but does not fully get my requirements.

JQL is achieve but I wanted to display the results of Assignee of those tickets and once it shows the list I can issue another script to update the Assignee of those issues which I will issue in another server. 

Jira Server is running version 8.5.4 and scrip runner version 6.7.0 .

I have this references from Adaptavist Library:

Bulk Update the Value of a System Field on Jira Issues 

Perform a JQL Search in ScriptRunner for Jira 

 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level

// Set log level to INFO
log.setLevel(Level.INFO)

// The JQL query you want to search with
final jqlSearch = "project = \"Documentation Status\" AND status not in (Closed) AND assignee is EMPTY ORDER BY status DESC"

// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.isValid()) {
log.error('Invalid query')
return null
}

try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
issues.each {
log.info(it.key)
}

issues*.key
} catch (SearchException e) {
e.printStackTrace()
null
}

 

Thank you.

Ravi Sagar _Sparxsys_
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.
October 23, 2020

Hi @Gian Israel Nampi 

instead of it.key try the following

  • it.assignee.username
  • it.assignee.emailAddress
  • it. displayName

or display along with the issue key like this.

log.info(it.key + "," + it.assignee.username)

 I hope it helps.

Gian Israel Nampi October 23, 2020

Hi @Ravi Sagar _Sparxsys_

Great it is working! where can I get those definitions you have given? can you provide a link please?

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level

// Set log level to INFO
log.setLevel(Level.INFO)

// The JQL query you want to search with
final jqlSearch = "project = \"Documentation Status\" AND status not in (Closed) AND assignee is EMPTY ORDER BY status DESC"

// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.isValid()) {
log.error('Invalid query')
return null
}

try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
issues.each {
log.info(it.assignee.username)
log.info(it.key)
}

issues*.key
} catch (SearchException e) {
e.printStackTrace()
null
}

 

I tested it on my personal sandbox but when I run it to our server-uat, I got an error I think maybe because I have 400+ tickets/issues on that query.  

error.PNGAdvanced Logging which part of the documentation am I going to implement so I can view the logs?

Ravi Sagar _Sparxsys_
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.
October 23, 2020

Hi @Gian Israel Nampi 

  • For definitions: Try Jira Java docs but it can be overwhelming, so you can try using the code editor for example type it (or any issue object) and press dot and it will give you suggestion like it.assignee etc. or you can write scripts using IntelliJ.
  • Instead of using Logs tab try to print on the Result tab by using return statement. You will have to build the string that you want to display something like this.
def sb = new StringBuilder()
sb.append("Text")
sb.append("More Text")
return sb
  • Advanced logging: Try changing to log.warn instead. It depends on the level of the log set which you can always change.
Gian Israel Nampi October 29, 2020

Hi @Ravi Sagar _Sparxsys_

For my last question.  What is the best way of automating my script to update the assignee of base on the JQL queries which consist of multiple JQL queries and Assignees?

I have tried using searchQuery1, searchQuery2  but it is not working. below is my code.

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.issue.search.SearchQuery

// The issues returned from that JQL will get altered
//final searchQuery = "project = PX AND \"Custom Field Name\" ~ \"custom field value\" AND status not in (Closed) AND assignee changed ON 2020-10-05"

final searchQuery = "project = \"Project\" AND issuekey = \"ISSUE-KEY1\""
final searchQuery = "project = \"Project\" AND issuekey = \"ISSUE-KEY2\""
final searchQuery = "project = \"Project\" AND issuekey = \"ISSUE-KEY3\""
final searchQuery = "project = \"Project\" AND issuekey = \"ISSUE-KEY4\""
final searchQuery = "project = \"Project\" AND issuekey = \"ISSUE-KEY5\""

// Get some components
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueService = ComponentAccessor.issueService

// Perform the search
def query = jqlQueryParser.parseQuery(searchQuery)
def searchResults = searchProvider.search(SearchQuery.create(query, loggedInUser), PagerFilter.unlimitedFilter)

// Iterate all the results to update each issue
searchResults.results.each { documentIssue ->
// Define the new params (a new description)
def issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.setAssigneeId("Username1") //Username
issueInputParameters.setAssigneeId("Username2") //Username
issueInputParameters.setAssigneeId("Username3") //Username
issueInputParameters.setAssigneeId("Username4") //Username
issueInputParameters.setAssigneeId("Username5") //Username


// Update the issue
def issueId = documentIssue.document.fields.find { it.name() == "issue_id" }.stringValue().toLong()
def updateValidationResult = issueService.validateUpdate(loggedInUser, issueId, issueInputParameters)
assert updateValidationResult.isValid() : updateValidationResult.errorCollection

// Validate the update
def issueResult = issueService.update(loggedInUser, updateValidationResult)
assert issueResult.isValid() : issueResult.errorCollection
}

Suggest an answer

Log in or Sign up to answer