问题描述:

In a workflow transition I have a screen with an "attach file" input. In the post-function I want to access the attached file (if any) and create another issue with this file as attachment.

I tried to achieve this via the ServletActionContext.getRequest() but I can't seem to get the uploaded file this way. The HttpServletRequest does not have the getPart() function.

Is there an official way to access attached files from post-functions?

Many thanks in advance

网友答案:

Thanks to this answer from Naykipap I found a working solution. Here's what I came up with:

// copy attachments uploaded in screen to new issue
def changeItems = transientVars["changeItems"]
def uploadChanges = changeItems.findAll { item -> item.getField() == "Attachment" && item.getFieldType() == "jira" }

uploadChanges.each { uploadChange ->
  def Attachment attachment = attachmentManager.getAttachment(uploadChange.getTo()?.toLong())
  if (attachment) {
    def filePath = PathUtils.joinPaths(pathManager.attachmentPath, currentIssue.projectObject.key, currentIssue.key, attachment.id.toString())
    def atFile = new File(filePath)
    if (atFile.canRead()) {
        log.debug("Cloning attachment ${attachment.filename}")
        attachmentManager.createAttachmentCopySourceFile(atFile, attachment.filename, attachment.mimetype, attachment.author, newIssue, [:], attachment.created)
    }
  }
}

Important: the script has to be executed after the issue is saved to db. Else there is no changeItems in the transientVars and/or the attachment is not saved to disk. I moved it right down to the end of executions and it worked fine for me.

相关阅读:
Top