How to Inspect the Attachment field for a specific file type

PJB July 31, 2024

Is there a way to inspect the "attachment" field within Jira?  we are looking for a specific type of attachment file.

2 answers

2 votes
Samuel Gatica (ServiceRocket)
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 31, 2024

Hi @Phil Burgard 

To inspect the Attachment field in Jira for a specific file type, you can use Jira Automation or a ScriptRunner (if available) to check the file types attached to an issue. Here's how you can approach it:

1. Using Jira Automation

Jira Automation doesn’t natively support inspecting attachments for specific file types directly, but you can work around this by using smart values and regular expressions.

Example Automation Rule:

  • Trigger: Issue Created or Issue Updated (or another relevant trigger)
  • Condition: Advanced Compare Condition or If/Else Condition
    • First Value: {{issue.attachment.filename.match(".*\.pdf")}}
    • Condition: "Does not equal"
    • Second Value: "" (This means if a PDF is attached, the filename will not be empty)
  • Action: Send an alert, comment, or perform any other action if a file with the specific extension (e.g., .pdf) is found.

2. Using ScriptRunner (If Available)

If you have ScriptRunner for Jira, you can write a Groovy script to inspect attachments and determine if a specific file type is present.

Example Script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issueManager = ComponentAccessor.getIssueManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()

Issue issue = issueManager.getIssueObject("ISSUE-KEY") // Replace with your issue key
def attachments = attachmentManager.getAttachments(issue)

def pdfFiles = attachments.findAll { it.filename.endsWith(".pdf") }

if (pdfFiles) {
return "PDF file(s) found: ${pdfFiles*.filename}"
} else {
return "No PDF files attached."
}

 

 

You may also refer to this post for additional information: https://community.atlassian.com/t5/Jira-questions/Use-Case-Verify-Attachment-Type-When-Transitioning/qaq-p/1631128

 

Best regards

Sam

0 votes
Maciej Dudziak _Forgappify_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2024

Hi @Phil Burgard 

If you are in a company-managed project it is possible validate attachments during create/  transitions with Jira expression-based validators. 

I am from Forgappify, and we developed Jira Expression Validator, which is part of the Workflow Building Blocks for Jira app. Here is an example how to restrict .bmp, .exe, .bat, jam.dev file extensions:

issue.attachments.every(item => !item.filename.match('(bmp|exe|bat|jam.dev)$'))

It is also possible to inspect a mimeType, size, author, and even a create date of the attachments. I am leaving a link to our expression library in case you are interested. If you can elaborate on what exactly you need, I can help you write the expression.

I hope it will help.

Cheers

 

PJB August 14, 2024

Thank you for answering the question. 

 

Suggest an answer

Log in or Sign up to answer