Get Issue Change History using Script Console not giving result

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hello Guys,

Need one help. I am trying to get issue change history of all the issues get via JQL seach  using Script Console . But getting error :- 

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.changehistory.DefaultChangeHistoryManager.getAllChangeItems() is applicable for argument types: (ArrayList) values: [[CJS-416, CJS-414, CJS-401, CJS-400, CJS-399, CJS-398, CJS-391, ...]] Possible solutions: getAllChangeItems(com.atlassian.jira.issue.Issue) at Script1659$_run_closure2.doCall(Script1659.groovy:27) at Script1659.run(Script1659.groovy:25)

Kindly suggest how can i fix this issue. 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.issue.changehistory.ChangeHistoryItem


def appUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkManager = ComponentAccessor.getIssueLinkManager()
def searchService = ComponentAccessor.getComponent(SearchService.class)
def issueManager = ComponentAccessor.getIssueManager()
def log = Logger.getLogger("Jira Log")
log.setLevel(Level.DEBUG)


def jqlSearch = "project = CJS"

SearchService.ParseResult parseResult = searchService.parseQuery(appUser, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(appUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = searchResult.results.collect {issueManager.getIssueObject(it.id)}
//return searchResult.results
issues.each { issue ->
def changehistoryManager = ComponentAccessor.getChangeHistoryManager()
return changehistoryManager.getAllChangeItems(issues)
}
}
else{
log.error("Invalid JQL :" + jqlSearch)
}

  Thanks

1 answer

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Vikrant Yadav

Instead of using the JQL search, you could try something like this:-

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def projectManager = ComponentAccessor.projectManager
def project = projectManager.getProjectByCurrentKey("MOCK")

def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))

issues.each { issue ->
log.warn "${changeHistoryManager.getAllChangeItems(issue)}"
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the sample output:-

sample.png

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_  Thanks a lot, Mate :)

But i need change history for a particular date range like for last 1 Year issues only, that's why i used JQL. Kindly suggest.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Vikrant Yadav,

I need to clarify with you when you say date rage, i.e. is it for the date range when the issue was created or updated?

Thank you and Kind Regards,

Ram

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_  

date range like project = XYZ AND created >= "2021/01/01" AND created <= "2021/06/30" . I need change history for these issue only.

Can i use script like this ? Please suggest.

if (parseResult.isValid()) {
def searchResult = searchService.search(appUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = searchResult.results.collect {issueManager.getIssueObject(it.id)}
//return searchResult.results
issues.each { issue ->
log.warn "${changeHistoryManager.getAllChangeItems(issues)}"
}
}

 

 

Thanks

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Vikrant Yadav

You could try something like this:-

import com.atlassian.jira.component.ComponentAccessor

import java.text.SimpleDateFormat

def issueManager = ComponentAccessor.issueManager
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def projectManager = ComponentAccessor.projectManager
def project = projectManager.getProjectByCurrentKey("MOCK")

def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))

def cal = Calendar.getInstance()
cal.add(Calendar.DAY_OF_MONTH, -5) // Modify the Range according to your requirement
def sdf = new SimpleDateFormat("yyyy-MM-dd")
def last5Days = sdf.parse(sdf.format(cal.time))

issues.each { issue ->
def createdDate = sdf.parse(sdf.format(issue.created.time))

if(createdDate <= last5Days) {
log.warn "${changeHistoryManager.getAllChangeItems(issue)}"
}
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to modify it accordingly.

You can use the calendar to determine the date range using Days and filter the results.

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram 

Like Vikrant Yadav likes this
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

@Ram Kumar Aravindakshan _Adaptavist_  Thanks a lot, it works. :) 

How can i get rid of this " Start of logs truncated as they exceeded 300 lines." ? 

I want to see complete result. How can i get complete data ?

Thanks Again!

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Vikrant Yadav

Could you share what is printed out in the log?

Are you referring to the empty lists returned, i.e.:-

2021-07-06 19:13:47,908 WARN [runner.ScriptBindingsManager]: [] 
2021-07-06 19:13:47,909 WARN [runner.ScriptBindingsManager]: []
2021-07-06 19:13:47,909 WARN [runner.ScriptBindingsManager]: []
2021-07-06 19:13:47,910 WARN [runner.ScriptBindingsManager]: []
2021-07-06 19:13:47,910 WARN [runner.ScriptBindingsManager]: []
2021-07-06 19:13:47,910 WARN [runner.ScriptBindingsManager]: []

Thank you and Kind Regards,

Ram

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_   Result is fine. Getting change history in Logs. It won't showing all issues / It's showing only 300 issues, how can i see change history for all 352 issue ? After 300 lins it shows this message "Start of logs truncated as they exceeded 300 lines." Kindly advise.

Log truncted.PNGLogs.PNG

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Vikrant Yadav

That's because the limit has exceeded.

You can, however, use ScriptRunner's Built-In Script to view all the logs as shown below:-

view_log1.png

You can set the limit and view it.

view_log2.png

 

Another alternative I can suggest is to write out the result in a file instead of using the log.

Below is the modified code sample:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def issueManager = ComponentAccessor.issueManager
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def projectManager = ComponentAccessor.projectManager
def project = projectManager.getProjectByCurrentKey("MOCK")

def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))

def cal = Calendar.getInstance()
cal.add(Calendar.DAY_OF_MONTH, -5) // Modify the Range according to your requirement
def sdf = new SimpleDateFormat("yyyy-MM-dd")
def last5Days = sdf.parse(sdf.format(cal.time))

issues.each { issue ->
def createdDate = sdf.parse(sdf.format(issue.created.time))

if(createdDate <= last5Days) {
def logFile = new File("/home/ram/Downloads/Example.log") // Modify according to your file path and file name

if(!logFile.exists()) {
logFile.createNewFile()
}
logFile.append("${changeHistoryManager.getAllChangeItems(issue)}\n")
}
}

This will write the result to the file you have specified.

Thank you and Kind Regards,

Ram

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Thanks Mate! 

Have a Great Evening! 

what is this "sdf.parse" ? What does it do ?

Thanks

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2021

Hi @Vikrant Yadav

The sdf is a variable name that is basically referring to the SimpleDateFormat class.

It's basically used to change the format of your date. For example from US format Month Day Year (MM-dd-yyyy) to ISO format Year Month Date (yyyy-MM-dd).

Thank you and Kind Regards,
Ram

Suggest an answer

Log in or Sign up to answer