Export CSV Log
Asked by Ashley Roberts:

I am not one for writing scripts, but is there a way to write a groovy script to produce a log of users who have exported an issue out into a CSV file?


Answers:
author: Aron Gombas _Midori_
body: The event when an issue list gets exported to CSV is not recorded anywhere, therefore the information itself is missing.

One approach that could work is fine-tuning the logging around exporting.

I mean if you can find a relevant class in the Jira source code that participates in the export flow, then you could increase its logging level to DEBUG or even to TRACE. That way the exports are logged to the main Jira logfile, which you can analyze in Groovy.

I think the "action" class is a good candidate for this.

comments: author: Ashley Roberts
body: I have tried to make a script to do that, but can't get it to work:  

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.IssueFactory

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.event.type.EventDispatchOption

import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.bc.issue.IssueService

import com.atlassian.jira.jql.builder.JqlQueryBuilder

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.IssueInputParameters

import com.atlassian.jira.bc.issue.ValidateResult

import org.apache.log4j.Logger

enum LogType {

ISSUE_EXPORT_LOG("Issue Export Log"),

OTHER_LOG_TYPE("Other Log Type")

private final String value

LogType(String value) {

this.value = value

}

String getValue() {

value

}

}

Logger issueExportLogger =

Logger.getLogger(LogType.ISSUE_EXPORT_LOG.getValue())

def issueManager = ComponentAccessor.getIssueManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def issueService = ComponentAccessor.getIssueService()

/\*\*

\* Creates an issue export log for the given issue and user.

\*/

void createExportLog(Issue issue, ApplicationUser user, Date exportDate) {

if (!issue \|\| !user \|\| !exportDate) {

throw new NullPointerException("Invalid input: issue=$issue, user=$user, exportDate=$exportDate")

}

CustomField userCustomField =

customFieldManager.getCustomFieldObject(issue.getProjectId(), "User")

if (!userCustomField) {

throw new RuntimeException("Custom field 'User' does not exist")

}

CustomField issueCustomField =

customFieldManager.getCustomFieldObject(issue.getProjectId(), "Issue")

if (!issueCustomField) {

throw new RuntimeException("Custom field 'Issue' does not exist")

}

CustomField exportDateCustomField = customFieldManager.getCustomFieldObject(issue.getProjectId(), "Export Date")

if (!exportDateCustomField) {

throw new RuntimeException("Custom field 'Export Date' does not

exist")

}

IssueFactory issueFactory = ComponentAccessor.getIssueFactory()

Issue exportLogIssue = issueFactory.getIssue()

exportLogIssue.setSummary(String.format("Export log for issue %s",

issue.key))

exportLogIssue.setDescription(String.format("Export log for issue %s created on %s", issue.key, exportDate.format('yyyy-MM-dd HH:mm:ss')))

exportLogIssue.setIssueTypeId(issue.getIssueTypeId())

exportLogIssue.setProjectId(issue.getProjectId())

exportLogIssue.setCustomFieldValue(userCustomField, user.displayName)

exportLogIssue.setCustomFieldValue(issueCustomField, issue.key)

exportLogIssue.setCustomFieldValue(exportDateCustomField, exportDate)

IssueInputParameters issueInputParameters =

issueService.newIssueInputParameters()

issueInputParameters.setProjectId(issue.getProjectId())

issueInputParameters.setIssueTypeId(issue.getIssueTypeId())

issueInputParameters.setSummary(String.format("Export log for issue %s", issue.key))

issueInputParameters.setDescription(String.format("Export log for issue %s created on %s", issue.key,

exportDate.format('yyyy-MM-dd-HH:mm:ss')))

issueInputParameters.setCustomFieldValue(userCustomField.id,

user.displayName)

issueInputParameters.setCustomFieldValue(issueCustomField.id,

issue.key)

issueInputParameters.setCustomFieldValue(exportDateCustomField.id,

exportDate)

ValidateResult validateResult = issueService.validateCreate(user,

issueInputParameters)

if (!validateResult.isValid()) {

issueExportLogger.error("Error creating issue export log: " + validateResult.getErrorCollection().collect { errorCollection -\>

errorCollection.getErrorMessages().join(',') }.join(','))

return

}

Issue createdIssue = issueService.createIssue(user, validateResult)

if (createdIssue != null) {

issueExportLogger.info(String.format("Issue export log created successfully for issue %s and user %s", createdIssue.key, user.displayName))

} else {

issueExportLogger.error("Failed to create issue export log")

}

}


author: Ashley Roberts
body: I get the following error when trying:  

Unexpected character: '"' @ line 58, column 37.


Tags: data-center

jira-data-center

jira-server

server