Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,017
Community Members
 
Community Events
184
Community Groups

Scripted field to read linked attachment reads too many attachments

I have a scripted field that reads any attachments from a specific Link type and shows the file name of the linked attachment inside the scripted field.

The issue that I am having, is it reads all of the attachments including the source issue. It should only read the LINKED issues attachments.

I am getting an error when looking at the inline script - Cannot call java.util.List

(see attachement)

Setup -

JIRA 7.3.1
Adaptavist ScriptRunner for JIRA Version: 5.0.1

Issue Linking: New Issue Link Created
SD-DeliveryTeam-OutLink
Outward Description = SD-DeliveryTeam-OutLink
Inward Description = SD-DeliveryTeam-InLink

CODE -

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issues = [issue] as List<Issue>
issues.addAll(issueLinkManager.getOutwardLinks(issue.id).findResults {
    if (it.issueLinkType.name == "SD-DeliveryTeam-Link") {
        return it.destinationObject
    }
    null
})
issues.addAll(issueLinkManager.getInwardLinks(issue.id).findResults {
    if (it.issueLinkType.name == "SD-DeliveryTeam-Link") {
        return it.sourceObject
    }
    null
})
issues.collect { issue ->
    issue.getAttachments().collect { att ->
        "<a href=\"" + "/secure/attachment/" + att.id + "/" + att.filename + "\">" + att.filename + "</a>" + "<br>"
    }
}.flatten().join("")

Error:

Scripted Field Error.png

Results Pulls Back to many Attachments:

Scripted Field Error 2.png

2 answers

1 accepted

1 vote
Answer accepted
Jenna Davis
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.
Jun 27, 2017

Hello, 

I think this will get you close: 

 

import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.issueLinkManager
def loggedUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def links = issueLinkManager.getLinkCollection(issue, loggedUser).allIssues
def linkedAttachments = []

links.each{
    if(it.issueType.name as String == "SD-DeliveryTeam-Link"){
        linkedAttachments.addAll(it.attachments)
    }
}

(linkedAttachments).collect { att ->
    "<a href=\"" + "/secure/attachment/" + att.id + "/" + att.filename + "\">" + att.filename + "</a>" + "<br>"
}.flatten().join("")

 

Hope this helps!

Jenna

Hi Jenna,

I tried that version however the inline script doesn't like the att.id, att.filename and att.filename.

It returns no results.

ATT Errors.png

Jenna Davis
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.
Jun 28, 2017 • edited

Hmm, try this instead:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment

def issueLinkManager = ComponentAccessor.issueLinkManager
def loggedUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def links = issueLinkManager.getLinkCollection(issue, loggedUser).allIssues
def linkedAttachments = []

links.each{
    if(it.issueType.name as String == "SD-DeliveryTeam-InLink"){
        linkedAttachments.addAll(it.attachments)
    }
}

(linkedAttachments).collect { att ->
    "<a href=\"" + "/secure/attachment/" + (att as Attachment).id + "/" + (att as Attachment).filename + "\">" + (att as Attachment).filename + "</a>" + "<br>"
}.flatten().join("")

Let me know if it works!

Jenna

Hi Jenna,

Yes, that worked. Is there someway to allow a wildcard for the issuetype? Otherwise I would need to define multiple Strings for each issuetype.

Jenna Davis
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.
Jun 28, 2017

Great! And sure; 

For any issue type, just take out the if statement on the links.each portion:

links.each{
    linkedAttachments.addAll(it.attachments)
}

For a few different types you can define a list with them as so:

def myTypes = ["typeOne", "typeTwo"]

And then change the links.each statement to this:

links.each{
    if(myTypes.contains(it.issueType.name as String)){
        linkedAttachments.addAll(it.attachments)
    }
}

Let me know if you have any other questions! 

Thank you!

I used the myTypes array, works well.

For some reason, reading the Link name, just doesn't work.

How it works at JIRA Cloud? Is it possible to read anyhow attachment at Jira Cloud?
Thank you 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events