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
Hi @Isabel Fonseca,
I checked your scripting code and tested. It looks fine to me and pulling the data of last comments in the tickets based on the JQL query you have executed.
Can you please elaborate what is not working for you? Is the script returning 'null' if there are no comments in the ticket and you don't want to include those?
In that case, check out the updated code here:
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)
if (comment != null) {
//
log.info(it.key+' - '+comment)
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!')
}
Hope this helps.
Thanks,
Vamsi
The situation is resolved.
I run the script in the "ScriptRunner\Console", after Re-indexing:
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 and key = QS-609752"
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")
}
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)
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(it.key)
if (comment != null) {
//
log.info(it.key+' - '+comment)
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!')
}
Thanks,
Isabel Fonseca
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.