Good afternoon,
When users search issues, they whant view Last Comment for each issue.
Has i am jira administrator, i must have do implement that situation.
We have Jira Server v8.20.13.
What i have done:
1) Create a Custom fields, called "Last Comment". So, the Custom field is of the type "Text Field (multi-line)";
2) In "ScriptRunner\Listeners", i created a Custom listener, select events ("Issue Commented", "Issue Commente Edited", "Issue Commente Deleted") and i put a Script made by me. And it works fine.
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
// Set log level to INFO
log.setLevel(Level.INFO)
def issue = event.issue
def commentManager = ComponentAccessor.commentManager
def comment = commentManager.getLastComment(issue)
def customFieldManager = ComponentAccessor.customFieldManager
// Get the custom field
def customField = ComponentAccessor.customFieldManager.customFieldObjects.findByName("Last Comment")
if (comment) {
comment.body
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), comment.body), new DefaultIssueChangeHolder())
} else {
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), ""), new DefaultIssueChangeHolder())
}
But now I wants create a script to execute once, to load de last comment from all issues already existant. In ScriptRunner, Fields i create a "Custom Script Field".
Script:
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
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
def issueService = ComponentAccessor.issueService
def commentManager = ComponentAccessor.commentManager
def customFieldManager = ComponentAccessor.customFieldManager
def customField = ComponentAccessor.customFieldManager.customFieldObjects.findByName("Last Comment")
// Set log level to INFO
log.setLevel(Level.INFO)
// The JQL query you want to search with
final jqlSearch = "project ='Quality Support' and reporter =i2sifo"
// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query')
return null
}
try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
int sizeOfResults = results.getResults().size();
if(sizeOfResults == 0){
log.error("No results from query")
return null
}
log.info("sizeOfResults: "+sizeOfResults)
// Load Last Comment for each issue already exist :
def issues = results.results
issues.each {
//log.info(it.key)
// Get the custom field
def comment = commentManager.getLastComment(it)
log.info(it.key+' - '+comment)
if (comment != null) {
//
if (comment) {
comment.body
log.info('Com: ' + comment.body)
customField.updateValue(null, issue, new ModifiedValue(it.getCustomFieldValue(customField), comment.body), new DefaultIssueChangeHolder())
} else {
log.info('Sem: ' + comment.body)
customField.updateValue(null, issue, new ModifiedValue(it.getCustomFieldValue(customField), ""), new DefaultIssueChangeHolder())
}
} else {
log.info('null')
customField.updateValue(null, issue, new ModifiedValue(it.getCustomFieldValue(customField), ""), new DefaultIssueChangeHolder())
}
}
} catch (SearchException e) {
e.printStackTrace()
log.error('GENERAL ERROR!')
null
}
The problem is that the script don't work like i wants. Some comment are incorrectly.
Could you help me?
Best regards