Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,364,743
Community Members
 
Community Events
168
Community Groups

Jira Comment Export using Script Runner

We have script runner piad add on installed in our Jira Data Center. It provides a lot of functionalities out of the box that doesn't natively exist in Jira.
Just wondering if it can help in export of issues from jira in csv format, etc. where comments can be exported under one single column. By default comments get exported in separate columns which is each comment has its own column of its own and it becomes tedious to concatenate them later in excel.
I tried the below script but it doesn't work :-

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


// The JQL query you want to search with
final jqlSearch = "project = UBT AND issuetype = Task"

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

// 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)
def issues = results.results

issues*.key

issues.each { issue ->


def commentManager = ComponentAccessor.commentManager
def comment = commentManager.getComments(issue)
if (comment) {
comment.body
}

}

FileWriter attFds = new FileWriter("/jira_home/issues.csv")

attFds.write("COLUMN 1,COLUMN 2,COLUMN 3 \r\n")
attFds.close()


}
catch (SearchException e) {
e.printStackTrace()
null
}

 

All i would like to do is the below :-
Instead of doing the default native jira export of issues for a given JQL (includes all fields) where comments get exported in csv file in separate columns (each comment for each issue key in a separate column), i would like to do same export for the issues for a certain number of fields including comments but the export to a csv file should have all comments for a given issue key under one single comments column in a proper readable format. I am also fine just doing export of issue keys with its respective comments so i can merge them later into natively exported csv from jira that has all fields by deleting comments column and pasting the results for comments for the csv outputted via script runner against each issue key if that helps.

Thanks!

 

1 answer

1 accepted

0 votes
Answer accepted

Are you suggesting concatenating ALL comments for a single issue into a single string to be placed in a single cell for a given issue?

That could quickly exceed the size limit from just about any system that you might use to read that cell.

But if you really need to, you could try something like this:

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

// The JQL query you want to search with
final jqlSearch = "project = UBT AND issuetype = Task"

// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
def commentManager = ComponentAccessor.commentManager
def jiraDateFormatter = ComponentAccessor.getComponent(DateTimeFormatterFactory).formatter().withStyle(DateTimeStyle.DATE_TIME_PICKER)

// 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)
def issues = results.results
FileWriter attFds = new FileWriter("/apps/dev/jira-home/log/issues.csv")
attFds.write('Key,Summary,Reporter,Assingee,Created,Updated,Comments\r\n')
issues.each { issue ->
def comments = commentManager.getComments(issue)
def combinedComments = comments.collect { comment ->
"$comment.authorApplicationUser.displayName ($comment.authorApplicationUser.name) - ${jiraDateFormatter.format(comment.created)}:\n$comment.body"
}.join('\r\n\r\n')
attFds.write(issue.key + ',')
attFds.write(/"$issue.summary",/)
attFds.write(/"$issue.reporter.name",/)
attFds.write(/"$issue.assignee.name",/)
attFds.write(/"${jiraDateFormatter.format(issue.created)}",/)
attFds.write(/"${jiraDateFormatter.format(issue.updated)}",/)
attFds.write(/"${combinedComments.replaceAll('"', '""')}"/)
attFds.write('\r\n')
}
attFds.close()
} catch (SearchException e) {
e.printStackTrace()
null
}

Thanks a lot! That worked!

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events