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

Scripted post function copy attachment

Junaid Shah
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.
September 15, 2016

Hello,

I am trying to create a scripted post function which copies the attachment from subtask to parent, so far this is how far I've gotten, it is working OK till the attachment manager where I receive an error.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.PathUtils
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.util.AttachmentUtils

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()

attachmentManager = ComponentAccessor.getAttachmentManager()

pathManager = ComponentAccessor.getAttachmentPathManager()

attachmentManager.getAttachments(issue).each {attachment ->

    
    filePath = PathUtils.joinPaths(pathManager.attachmentPath, issue.projectObject.key, issue.key, attachment.id.toString())

    
    atFile = new File(filePath)
    File atFile = AttachmentUtils.getAttachmentFile(attachment);
    
    if (atFile.exists()) {


        if (atFile.canRead()) {
            attachmentManager.copyAttachments(atFile, attachment.filename, attachment.mimetype, attachment.author, parent_issue, [:], attachment.created)
        }
    }
}

This results in error:

[scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: TESTISSUE-170, actionId: 831, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.managers.DefaultAttachmentManager.copyAttachments() is applicable for argument types: (java.io.File, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.LinkedHashMap, java.sql.Timestamp) values: [/opt/jirainstallation/data/data/attachments/TESTISSUE/TESTISSUE-170/28800, ...]
        at Script5$_run_closure1.doCall(Script5.groovy:44)
        at Script5.run(Script5.groovy:23)

What could be causing this to occur ? any help is appreciated, I am on JIRA version 6.2

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
September 15, 2016

Hi Sy,

I cannot see a copyAttachments method in the 6.2 API, so probably you have to use the createAttachment

Junaid Shah
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.
September 15, 2016

Hi Thanos,

I tried but I get a very similar error;

kflow.ScriptWorkflowFunction] Script function failed on issue: TESTISSUE-170, actionId: 831, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.managers.DefaultAttachmentManager.createAttachment() is applicable for argument types: (java.io.File, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.LinkedHashMap, java.sql.Timestamp) values: [/opt/jirainstallation/data/data/attachments/TESTISSUE/TESTISSUE-170/28800, ...]
Possible solutions: createAttachment(java.io.File, java.lang.String, java.lang.String, com.atlassian.crowd.embedded.api.User, com.atlassian.jira.issue.Issue), createAttachment(java.io.File, java.lang.String, java.lang.String, com.atlassian.crowd.embedded.api.User, org.ofbiz.core.entity.GenericValue), createAttachment(java.io.File, java.lang.String, java.lang.String, com.atlassian.crowd.embedded.api.User, com.atlassian.jira.issue.Issue, java.util.Map, java.util.Date), createAttachment(java.io.File, java.lang.String, java.lang.String, com.atlassian.crowd.embedded.api.User, org.ofbiz.core.entity.GenericValue, java.util.Map, java.util.Date), createAttachment(org.ofbiz.core.entity.GenericValue, com.atlassian.crowd.embedded.api.User, java.lang.String, java.lang.String, java.lang.Long, java.util.Map, java.util.Date)
        at Script3$_run_closure1.doCall(Script3.groovy:44)
Thanos Batagiannis _Adaptavist_
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.
September 16, 2016

I can see for the API that it needs a User but you pass a String - attachment.author.

So try replace this with 

attachment.authorObject.directoryUser

and then I suppose the parent_issue you have over there is of type Issue, because in the script you posted there is no parent_issue variable.

 

Junaid Shah
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.
September 16, 2016

Sorry I missed out the string variable for parent_issue, this is as

def String parent_issue = "TESTISSUE-167"

also added the directoryUser but still getting the exact same error.

 

Thanos Batagiannis _Adaptavist_
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.
September 16, 2016

this should be of type Issue and not String. Use the issueManager 

def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueByCurrentKey("ISSUEKEY-1111")
Junaid Shah
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.
September 16, 2016

Thanks a lot for your help, it worked smile

TAGS
AUG Leaders

Atlassian Community Events