Unable to add an attachment in issue creation using Groovy script and in Create issue post function.
Below is the code which is not working in run time, where as when tested in Groovy Test Runner it is working fine.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
def attachmentManager = ComponentAccessor.getAttachmentManager()
def user = ComponentAccessor.getJiraAuthenticationContext()?.getUser()
def issueManager = ComponentAccessor.getIssueManager()
def description = issue.description
def key = issue.key
log.info(description)
log.info(key)
if (description != null){
def String filename = key + ".txt"
File f = new File("/tmp/"+filename);
f.getParentFile().mkdirs();
f.createNewFile();
FileWriter fw = new FileWriter("/tmp/"+filename, true)
BufferedWriter bw = new BufferedWriter(fw)
PrintWriter out = new PrintWriter(bw)
out.println(description);
out.close()
def bean = new CreateAttachmentParamsBean.Builder()
.file(f)
.filename(filename)
.contentType("text/plain")
.author(user)
.issue(issue)
.copySourceFile(true)
.build()
attachmentManager.createAttachment(bean)
}