Forums

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

How validate the attachment's name on the creation issue screen?

Rafael Costa
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.
March 15, 2022

There is an issue type that I need validate the attachment before creation. The attachment must be a csv file. How can I validate the ".csv" string from name of attachment?

1 answer

0 votes
Piyush A (STR)
Community Champion
March 15, 2022

Hi,

Haven't tried it now, but it will work like below in the validator section:

import webwork.action.ActionContext
def request = ActionContext.getRequest()
if (request)
{
    if (request.getParameter("filetoconvert") != null)
    {
        // Attachment(s)
    }
}

 

Something you can try with below I found in my old repo.

 

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")
}

 (above works for created issue)

Rafael Costa
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.
March 15, 2022

@Piyush A (STR)  thanks for answer! But the script still can't validate the attachment bc it still not exist. But I can build a javascript to validate and put it on the description of a field:

function att_loaded() {

    attachment = document.getElementsByClassName("upload-progress-bar__file-name")[0]

    if (attachment && !attachment.textContent.endsWith('csv')){

        alert('Você precisa converter a planilha para CSV antes de prosseguir. Siga as etapas a seguir para realizar a conversão\n\n\

        ● Com a planilha aberta no Excel, clique em Arquivo > Salvar como\n\

        ● Abaixo do nome do arquivo, selecione o tipo "CSV (Separado por vírgulas)"\n\

        ● Clique em "Salvar" e tente anexar novamente o arquivo para prosseguir.')

    }

}

button = document.getElementById("create-issue-submit")

button.addEventListener("mouseenter",att_loaded)
Piyush A (STR)
Community Champion
March 15, 2022

This would be good to verify the attachment while on the create screen. But it might not work as when creating via email or issue collector probably. 

Above 1st should be in the create transition validator script that would eventually not accept the create issue.

But the Javascript looks great. 

Suggest an answer

Log in or Sign up to answer