Work with Attachments

With HAPI, we've made it easy for you to add, access, and delete attachments.

Adding attachments

You can add attachments when you are creating, updating or transitioning an issue. Alternatively, you can add an attachment to an issue without having to update or transition it. To add an attachment you must specify a File object or a path to the file.

The file must be accessible by the path given on the Jira node running this code.

Adding attachments to an issue

You can attachments to any given issue:

            def issue = Issues.getByKey('SR-1')
            issue.addAttachment(new File('/path/to/file'))

            // or
            issue.addAttachment('/path/to/file')                    

You must have permission to create attachments on the project of the given issue.

Image showing how you add an attachment with HAPI

Adding attachments when creating, updating or transitioning an issue

Enter the following into the script console:

In this example we're adding an attachment to the issue we're creating. You can also use addAttachment when updating or transitioning an issue.

            Issues.create('JRA', 'Bug') {
                setSummary('help me!')

                // by File
                addAttachment(new File('/path/to/file'))

                // by String 
                addAttachment('/path/to/other/file')
            }

Transient data

If you have transient data you wish to add as an attachment, write it to a file, then add the file as an attachment as above, and then delete the file.

Accessing attachments

You can get access to the attachments of an issue by using the getAttachments() extension method, for example:

            Issues.getByKey('SR-1').attachments.each { attachment ->
                // do something with "attachment"
                log.warn(attachment.filename)
            }

Image showing how you access attachment with HAPI

Accessing attachments during transitions

If a user adds attachments in a workflow transition, normally you cannot validate them because the attachment is not accessible via the Jira API until the transition is complete.

However, with HAPI you can access these with the getAllAttachments() extension on MutableIssue. Because the attachment has not been committed to the database, getAllAttachments() returns a collection of objects that have the following methods: getMimetype()getFilename()getFilesize()getCreated(), and withInputStream.

This is enough that you can write workflow validators that can check file size, file names, or the content of attachments, without worrying about distinguishing between attachments previously added to the issue and attachments added during this transition.

As an example, you could check in a workflow validator that any attachment on the issue has the contents blah :

            issue.allAttachments.any {
                it.withInputStream {
                    it.text.trim() == 'blah'
                }
            }

Download URL

We have made it easy to get the download URL for any attachment. This method is useful for reports or email notifications:

            issue.attachments*.downloadUrl

Deleting attachments

You can delete attachments using the delete() extension method of Attachment. For example, to delete all the attachments on a given issue:

            issue.attachments.each {
                it.delete()
            }

To override security, use:

            issue.attachments.each {
                it.deleteOverrideSecurity()
            }



Related content

On this page