Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Attachments on Creation in groovy

Burak Kaya
Contributor
March 31, 2021

Hi Folks,

I need a script to get attachments for groovy to use in Create Validator script.

My purpose is to get all attachments which attached to the issue while creating in Customer Portal and check those attachment extensions (txt, sql, zip etc.) and prevent creation if any of those is not SQL file.

Before researching I want to use my existing code in Jira side to get attachments but it does not work in customer portal, it occurs "Java.NullException".

Then check the community to make sure if any topic is related to my issue, but I could not find what I exactly need.

Links I found before;
https://community.atlassian.com/t5/Marketplace-Apps-Integrations/How-to-validate-if-attachment-exists-on-issue-creation-through/qaq-p/627045

https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-get-attachments-during-issue-creation/qaq-p/542108

Guys please help me to find out how to get attachments during creation in validator and prevent in order to its file extension?

Thanks & Regards,
Burak

1 answer

0 votes
Juan Manuel Ibarra
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.
April 2, 2021

With this code you can get all the attachments of the issue, and with that list you can go through it and ask for the attribute of the file name and if they all have .SQL

 

 

import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.IssueManager;

import org.apache.log4j.Logger;
import org.apache.log4j.Level;

def attachmentManager = ComponentAccessor.getAttachmentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

//Defino el logger
def logger = Logger.getLogger("com.attachment.groovy")
logger.setLevel(Level.INFO)

logger.info("Start script")
IssueManager issueManager = ComponentAccessor.getIssueManager()

def issueKey = issue.getKey()

List<Attachment> issueAttachments = attachmentManager.getAttachments(issue)

def validAttachment = issueAttachments.every { issueAttachment ->
issueAttachment.getFilename().contains("SQL")
}
Burak Kaya
Contributor
April 3, 2021

Hi Juan,

It works on Jira itself but not in Jira Service Desk. Still getting error as "java.lang.NullPointerException" in 

List<Attachment> issueAttachments = attachmentManager.getAttachments(issue)

Here are screenshots;

The code which I got from you; (It is in Create validator)

Screen Shot 2021-04-03 at 15.18.11.png

Here is the log which I got error when I create an issue from Service Desk Portal;

Screen Shot 2021-04-03 at 15.18.25.pngThanks,
Burak

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events