Forums

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

Create assets object with attachment as post action

Els Bassant
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.
November 27, 2023

Hi Community, 

Does anyone have experience creating assets objects and then adding a specific attachment from a JSM ticket as a post action?

 

The general idea is as follows:

  • I have a JSM ticket regarding with a specific transition where information is entered, and an attachment is added. The fields on this transition are all mandatory.
  • As post action to this transition I want to create an Assets object, which includes the attachment from the transition. 

 

The first part, creating an assets object with the correct attributes is working. But I'm stumped on how to automate copying the attachment from the issue to the Assets Object.

2 answers

1 accepted

1 vote
Answer accepted
Els Bassant
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, 2024

I eventually got this to work through a combination of assets post function, and an automation that runs a scriptrunner script. 

Locating the correct assets object was easy, the biggest piece of the puzzle was dynamically grabbing the attachment location from the issue to be able to copy it to any assets object. More details can be found in this article, but in our case the attachment location was:

  • <JIRA_HOME>/data/attachments/<PROJECT>/<BUCKET>/<ISSUE_KEY>/<ID>

This means I had to do grab a whole lot of information to be able to define which attachment needs to be copied. Then grab some additional information to define the location to which it needs to be copied. In the end we used a two step process:

  • STEP 1: Use a post function to create the assets object and link it to the issue through an assets custom field. 
  • Step 2: Use an automation with the "issue transitioned" trigger to run a scriptrunner script to copy the correct attachment from the issue to the newly created assets object.

Script used:

import java.lang.Math
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment

//defining the required elements of the orginal attachment
def issueKey = issue.getKey()
def project = issue.getProjectObject().key
def attachmentManager = ComponentAccessor.getAttachmentManager()
def lastAttachment = attachmentManager.getAttachments(issue).last()
def fileId = lastAttachment.id
def fileName = lastAttachment.getFilename()
def attachmentPath = ComponentAccessor.getAttachmentPathManager().attachmentPath

//final attachment location
def issueNum = issue.toString()substring(4) toInteger()
def bucket = (Math.ceil((issueNum/10000))*10000).toInteger()
def url = "${attachmentPath}/${project}/${bucket}/${issue}/${fileId}/"

//find the related Assets object by key
def nameCf = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_00000")
def assetKey = issue.getCustomFieldValue(nameCf) toString()
String key = assetKey.substring(assetKey.indexOf("(")+1, assetKey.indexOf(")"));
def asset = Assets.getByKey(key)

//run code as specific user if needed
def actor = Users.setLoggedInUser('usernamehere')

//add attachment to Assets object
def file = new File(url);
asset.addAttachment(file) {
setFilename(fileName)
setComment('yourcomment')
}

 

In which

  • customfield_00000 is the custom field from Step 1 that links to the assets object.
  • usernamehere is the username of the user that will add the attachment to the object. This section is optional and only needed if the actor for the automation rule doesn't have editing permissions in the object schema.
  • yourcomment is an optional comment that is added when adding the attachment to the assets object.
  • This script assumes that your project have three character project keys.

 

There may be more elegant ways to handle the constant switching to and from strings that was nessecary to make the script work.

You could potentially achieve the whole process through a single scriptrunner script in a post function. It is just that I found this the more robust way to create an assets object.

1 vote
Elise Vernier
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.
November 28, 2023

Hi @Els Bassant 

With automation, you can only create, edit or search for Assets objects. It is the same for their attributes.  

But attachments are not considered attributes of an object, so you can't copy them from an issue with automation. 

There is an open issue in Atlassian here, if you want to vote for it: JSDCLOUD-10337

Hope I help

Regards, 

Elise 

Els Bassant
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.
November 29, 2023

Hi Elise, 

Unfortunate that this isn't available yet. Thanks for linking the feature request, I voted on both issues (Cloud and Server).

Were currently looking into whether this can be achieved through REST API.

Kind regards, 

Els

Elise Vernier
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.
November 29, 2023

Hi @Els Bassant

I've found 2 links to help you : 

There is a feature request too for attaching files through REST API: JSDCLOUD-10454.

Kind regards, 

Elise 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events