The Adaptavist Group LogoDocumentation

Validating Attachments Links In Transitions

If a user adds attachments or links in a workflow transition, normally you cannot validate them because they're not accessible via the Jira API until the transition is complete. However, we have made it easy to access links or attachments by using extension methods on MutableIssue. Check out the following documentation for more information on accessing attachments and links during transition:

Validating attachments added

The following example details how to find properties of attachments added to this transition or on creation, for example the file name.

Note: The Attachments field must be on the transition screen to get attachments like this.
issue.attachmentsAddedInTransition.each { attachment ->
    log.debug("Uploaded attachment name: ${attachment.filename}")
}

Making attachments required on issue creation

Using the following script in conjunction with a Custom script validator, you can make attachments required on issue creation:

Tip: This validator must be added to the Create transition.
import com.opensymphony.workflow.InvalidInputException
 
if(issue.attachmentsAddedInTransition.size() == 0) {
 throw new InvalidInputException("attachment", "Attachment is required")
}

Blocking files by extension

Using the following script in conjunction with a Simple scripted validator, you can block files with certain extensions from being added:

import com.google.common.io.Files

def extensionsToBlock = ['exe', 'reg']

!issue.attachmentsAddedInTransition.any { attachment ->
    Files.getFileExtension(attachment.filename).toLowerCase() in extensionsToBlock
}
CAUTION: You cannot select the Attachments field as the field to show the error on. Leave the error field blank, and the error message will be shown at the top of the form.

Reading attachments added this transition

If you need to read the contents of newly-added attachments, for in-depth validation, you can get the data as follows:

issue.attachmentsAddedInTransition.each { attachment ->
    attachment.withInputStream { stream ->
        log.warn('File text: ' + stream.text)
    }
}
Note: Check out our HAPI documentation on Accessing attachments during transition for more information on how to access attachments during transition.

Search documentation

Start typing to search the docs.