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:
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.
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:
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
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.