I'm working on a workflow that will allow tasks to get passed down an echelon for action, but only one level at a time, giving leadership the chance to edit/respond to tasks before it goes all the way down to work centers, but I'm getting kinda stuck when it comes to attachments. I know I can copy all the attachments in the post-script, but I'd like to give users the option to edit the attachments before passing them along: enter the customfield "Apwide File Field". Currently I'm using scriptrunner to pre-fill dummy fields on the transition screen, leaving the original issue un-changed. Here's an example screen capture:
I've managed to set the "Tasker Description for X" fields using Scriptrunner's Behaviors and this:
However the Apwide Attachment Field ("Attachments for Units") is being a PITA. Below is the closest solution I've found, but it doesn't actually update the form, it updates the field in the background, so I have to go to transition the task and hit cancel, and then when I go to transition again it updates the form value.
import com.atlassian.jira.issue.Issue
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
@WithPlugin("com.apwide.document.file-field")
import com.apwide.file.api.FileManager;
import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def attachmentManager = ComponentAccessor.getAttachmentManager()
def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor.class)
@PluginModule
FileManager fileManager
// Load the issue you want to update
def issueManager = ComponentAccessor.getIssueManager()
Issue issue = Issues.getByKey(getIssueContext().toString())
// find the target File Field to update
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fileField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Attachments for Units"}
log.warn "File Field found: ${fileField}"
if (!issue.getCustomFieldValue(fileField)) {
// check for attachments, then load them
String fileName = ""
List<Attachment> lastAttachments = attachmentManager.getAttachments(issue)
if (lastAttachments.size() > 0) {
log.warn "${lastAttachments.size()} attachments found"
for (int i=0; i < lastAttachments.size(); i++) {
// get next attachment
fileName = lastAttachments[i].getFilename()
//Attachment attachment = lastAttachments[i]
Attachment attachment = lastAttachments.find({ attachment -> attachment.getFilename().equals(fileName) })
log.warn "Found Attachment: ${attachment}"
// get attachment binary file
log.warn "Files found in issue folder: ${attachmentDirectoryAccessor.getAttachmentDirectory(issue).listFiles()}"
def file = attachmentDirectoryAccessor.getAttachmentDirectory(issue)
.listFiles()
.find({ it-> it.getName().equals(""+attachment.id)})
log.warn "File found: ${file}"
if (file){
// Size limit of file to upload (ex: 10Mo)
long maxFileSize = 10*1000*1000;
// upload the attachment binary file to File Field and get its unique id
String fileId = fileManager.upload(fileName, new FileInputStream(file), maxFileSize)
log.warn "FileId of newly updated file: ${fileId}"
if (fileField) {
def changeHolder = new DefaultIssueChangeHolder()
// update customfield's value using a String containing the file id
fileField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fileField), fileId), changeHolder)
log.warn "File field successfully updated with the new fileId"
}
}
}
}
}
Edit: I forgot to mention, but it's also only copying one attachment (the last one based on an alphanumeric scale running from 0-z). Haven't really started to deal with that yet, but any tips would be appreciated :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.