Forums

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

How can i get URL of attachment?

Ilya Stekolnikov
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 11, 2022

123.png

I need some how to get URL like string form Attachment.

how can i get it using scriptrunner? Jira Server.

 

May case:

1) doing JQL and from result - choose first issue with index 0.

2) from result issue trt to get attachment

3) from attachmetn try to get URL (content or thumbnail)

 

some part of a code below:

 

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)

String jqlSearch = "project = info and (cf[$cfId3] ~ \"$currentUserName\") "

Query query = jqlQueryParser.parseQuery(jqlSearch)
SearchResults results = searchService.search(fromUser,query, PagerFilter.getUnlimitedFilter())
if (results.total == 0) {
return log.debug("Got error resultJQL = " + results.total + fromUser + currentUserName)
}

ArrayList issues = results.results.collect {issueManager.getIssueObject (it.id)} /
MutableIssue infoIssue = ComponentAccessor.getIssueManager().getIssueObject(issues[0].toString()) 

def thumbnailsURL = ThumbnailManager.getThumbnails(infoIssue, fromUser).getImageURL() as String //here i got some problem


 

3 answers

2 accepted

3 votes
Answer accepted
Piyush A (STR)
Community Champion
March 11, 2022

Hi,

Generally I use below to get the issue attachment:

 

the path:

def attachmentPathManager = ComponentAccessor.getAttachmentPathManager().attachmentPath.toString()

 

Below is I use to fetch file:

File file = new File("")
def issueManager = ComponentAccessor.getIssueManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()
def issue = issueManager.getIssueObject(jiraTicket); // e.g. SP-1
def attachments = attachmentManager.getAttachments(issue)


//some code



if (attachments.size() < 1) {
log2.info("EXIT - NO ATTACHMENT FOUND FOR " + jiraTicket)
return 0
} else {
//Check if tickets contains required attachment
attachments.each {
attachment ->
if (attachment.filename == "Book1.csv") {
def filePath = AttachmentUtils.getAttachmentFile(attachment)
file = new File(filePath.toString())
fileExists = true
attachmentName=attachment.filename
}
}
2 votes
Answer accepted
Ilya Stekolnikov
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 12, 2022
0 votes
Steve Suranie
Contributor
September 21, 2023

You could do it this way: 


from jira import JIRA
from jira.client import ResultList
from jira.resources import Issue
import urllib.parse

rootURL = "https://xxxxxxxxxx.atlassian.net"

##############################################
# FUNCTIONS STARTS HERE
##############################################


def getAttachmentsFromIssue(strBoard, jira):

#search for issues
lstIssues = jira.search_issues(f"project={strBoard}")

#iterate through the issues - we cannot filter for attachments in the search so we need to do it in the results. : (
for thisIssue in lstIssues:

if len(thisIssue.fields.attachment) > 0: 
for thisAttach in thisIssue.fields.attachment: 

attachURL = f"{rootURL}/secure/attachment/{thisAttach.id}/{urllib.parse.quote(thisAttach.filename)}"
print(attachURL)

##############################################
# APP STARTS HERE
##############################################

jiraOptions = {'server': "https://xxxxxxxx.atlassian.net"}
jira = JIRA(options=jiraOptions,
basic_auth=("xxxxxxxx@xxxxxx.com", "xxxxxxxxxxxxxxxxx"), # Jira Cloud: a username/token tuple
)

getAttachmentsFromIssue("{YOUR PROJECT}", jira)

 



Suggest an answer

Log in or Sign up to answer