It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How can I delete attachments from Issue on workflow post-function?

Question in a subject. We're handle sensitive data in attachments and want to purge all of them after reporter will aprove that he received them. Is there an easy way to automate this process?

6 answers

1 accepted

2 votes
Answer accepted

Download post-function plugin from here.

https://bitbucket.org/bhushan154/jira-delete-attachments-post-function/downloads#download-263211

Haven't tested it yet. Let me know if you have any issues.

You can use the Script Runner plugin and code like this in a scripted workflow postfunction.

import com.atlassian.jira.component.ComponentAccessor

def attachmentManager = ComponentAccessor.attachmentManager def attachments = attachmentManager.getAttachments(issue) attachments.each {attachment -> attachmentManager.deleteAttachment(attachment) }

 

 

i have a error on -&gt can you help me?

It has to be „->“ instead of „->“

Like Stefano Rosario Aruta likes this

what is a import i need to add on this post function?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.AttachmentManager

should do the trick. :-)

Like Stefano Rosario Aruta likes this

I updated/simplified the script to incorporate your comments. This was not possible on mobile yesterday.

Like Stefano Rosario Aruta likes this

sure dude, thank you very much
can you help my  here below  i dont want copy attachment but i want create a link of the origina attachment, so i want that sub-task have a attahcment of  issue parent  and dont copy attachment. 

can you help me with this?



import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.config.SubTaskManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.jira.issue.IssueFactory;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;

if (issue.getIssueType().getName() != "Sub-task") {
if (ComponentAccessor.getAttachmentManager() .getAttachments(issue).size() >= 1) {
for(int i = 0; i < ComponentAccessor.attachmentManager.getAttachments(issue).size(); i++) {
//__________________________________________________________________________________________________________________________________________________
Issue parentIssue = issue
def att = parentIssue.getAttachments()[i];
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
IssueFactory issueFactory = ComponentAccessor.getIssueFactory();
String subTaskId = ComponentAccessor.getConstantsManager().getAllIssueTypeObjects().find { it.getName() == "Sub-task" }.id

IssueManager issueManager = ComponentAccessor.getIssueManager();
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();
AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager()
//__Fase
MutableIssue newSubTask = issueFactory.getIssue();
newSubTask.setSummary("Sub-Task Attachment: " + (i+1));
newSubTask.setDescription("Sub-Task with an Attachment");
newSubTask.setParentObject(parentIssue);
newSubTask.setReporterId(currentUser.username);
newSubTask.setProjectObject(parentIssue.getProjectObject());
newSubTask.setIssueTypeId(subTaskId);
//__Fase
issueManager.createIssueObject(currentUser, newSubTask);
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, currentUser);

attachmentManager.copyAttachment(att, currentUser, newSubTask.getKey().toString())
//__________________________________________________________________________________________________________________________________________________
}
}
}

So you want the Sub-task to have an attachment (displayed as a normal attachment) which is only a link to the attachment of the parent issue? I don't think this is possible.

You maybe able to create links in a field (like the description) to the parent attachments, but not have these displayed as attachments.

def pathManager = ComponentAccessor.getAttachmentPathManager()
def filePath = PathUtils.joinPaths(pathManager.attachmentPath, issue.projectObject.key, issue.key, att.id.toString())

Or you have to copy the attachments from the parent issue to the Sub-task.

Henning 

Try this to copy each attachment to a single sub-task.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.atlassian.jira.util.PathUtils

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name != SUBTASK_ISSUETYPE_NAME) {
def issueFactory = ComponentAccessor.issueFactory
def constantsManager = ComponentAccessor.constantsManager
def issueManager = ComponentAccessor.issueManager
def subTaskManager = ComponentAccessor.subTaskManager
def pathManager = ComponentAccessor.attachmentPathManager
def attachmentManager = ComponentAccessor.attachmentManager

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def subTaskId = constantsManager.allIssueTypeObjects.find{it.name == SUBTASK_ISSUETYPE_NAME}.id

issue.attachments.eachWithIndex{att, i ->

def newSubTask = issueFactory.getIssue()
newSubTask.setSummary("Sub-Task Attachment: " + (i + 1))
newSubTask.setDescription("Sub-Task with an Attachment")
newSubTask.setParentObject(issue)
newSubTask.setReporterId(currentUser.username)
newSubTask.setProjectObject(issue.projectObject)
newSubTask.setIssueTypeId(subTaskId)

issueManager.createIssueObject(currentUser, newSubTask)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)

def filePath = PathUtils.joinPaths(pathManager.attachmentPath, issue.projectObject.key, issue.key, att.id.toString())
def atFile = new File(filePath)
if (atFile.exists()) {
try {
if (atFile.canRead()) {
attachmentManager.createAttachment(
(new CreateAttachmentParamsBean.Builder(
atFile,
att.filename,
att.mimetype,
att.authorObject,
newSubTask)
)
.createdTime(att.created)
.copySourceFile(true)
.build()
)
}
}
catch (SecurityException se) {
log.warn("Could not read attachment file. Not copying. (${se.message})")
}
} else {
log.warn("Attachment file does not exist where it should. Not copying.")
}
}
}

it' doesn't work there isn't attahcment in sub-task

Ok, simplify it:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name != SUBTASK_ISSUETYPE_NAME) {
def issueFactory = ComponentAccessor.issueFactory
def constantsManager = ComponentAccessor.constantsManager
def issueManager = ComponentAccessor.issueManager
def subTaskManager = ComponentAccessor.subTaskManager
def attachmentManager = ComponentAccessor.attachmentManager

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def subTaskId = constantsManager.allIssueTypeObjects.find{it.name == SUBTASK_ISSUETYPE_NAME}.id

issue.attachments.eachWithIndex{att, i ->

def newSubTask = issueFactory.getIssue()
newSubTask.setSummary("Sub-Task Attachment: " + (i + 1))
newSubTask.setDescription("Sub-Task with an Attachment")
newSubTask.setParentObject(issue)
newSubTask.setReporterId(currentUser.username)
newSubTask.setProjectObject(issue.projectObject)
newSubTask.setIssueTypeId(subTaskId)

issueManager.createIssueObject(currentUser, newSubTask)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)


attachmentManager.copyAttachment(att, att.authorObject, newSubTask.key)
}
}

The previous script is for Jira < 7.0.

Henning 

Like Stefano Rosario Aruta likes this

have a last version jira, but this script make a a new file on sub-task?

i think that the solution is to after this script delete a attachment on issue and add a post function at last transaction in the sub-task where copy attachment on issue and after delete on sub-task

Yes, this script creates one sub-task for each attachment. Isn't this what you want? If you want to delete the attachments from the parent issue after creating the sub-task simply add

issue.attachments.each{att ->
attachmentManager.deleteAttachment(att)
}

before the last }

Henning

yes, i idid but i don't know how to add  sub task attchament at main issue in the attchament field

For the last transaction of the sub-task you could use

import com.atlassian.jira.component.ComponentAccessor

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name == SUBTASK_ISSUETYPE_NAME) {
def attachmentManager = ComponentAccessor.attachmentManager
issue.attachments.each{att ->
attachmentManager.copyAttachment(att, att.authorObject, issue.parentObject.key)
attachmentManager.deleteAttachment(att)
}
}

to transfer the attachments of the sub-task back to the parent issue.

Does this help?

Henning

Like Stefano Rosario Aruta likes this

amazing dude, you are masterpiece

You're welcome :-)

i think that the solution is to after this script delete a attachment on issue and add a post function at last transaction in the sub-task where copy attachment on issue and after delete on sub-task

Code to create Sub-task with a single attachment:

//Code make by Henning Tietgens
import
com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name != SUBTASK_ISSUETYPE_NAME) {
def issueFactory = ComponentAccessor.issueFactory
def constantsManager = ComponentAccessor.constantsManager
def issueManager = ComponentAccessor.issueManager
def subTaskManager = ComponentAccessor.subTaskManager
def attachmentManager = ComponentAccessor.attachmentManager

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def subTaskId = constantsManager.allIssueTypeObjects.find{it.name == SUBTASK_ISSUETYPE_NAME}.id

issue.attachments.eachWithIndex{att, i ->

def newSubTask = issueFactory.getIssue()
newSubTask.setSummary("Sub-Task Attachment: " + (i + 1))
newSubTask.setDescription("Sub-Task with an Attachment")
newSubTask.setParentObject(issue)
newSubTask.setReporterId(currentUser.username)
newSubTask.setProjectObject(issue.projectObject)
newSubTask.setIssueTypeId(subTaskId)

issueManager.createIssueObject(currentUser, newSubTask)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)


attachmentManager.copyAttachment(att, att.authorObject, newSubTask.key)
}
}

After Second POST-Fucntion to delete every Attachment in the main issue:

//
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.AttachmentManager

AttachmentManager attachmentManager = ComponentAccessor.attachmentManager

def attachments = attachmentManager.getAttachments(issue)
attachments.each {attachment ->;
attachmentManager.deleteAttachment(attachment)
}

And this code is for sub-task Transaction(POST_Function): this script copy Attahcment on the main after delete every attachment in this sub-task

//Code make by Henning Tietgens

import
com.atlassian.jira.component.ComponentAccessor

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name == SUBTASK_ISSUETYPE_NAME) {
def attachmentManager = ComponentAccessor.attachmentManager
issue.attachments.each{att ->
attachmentManager.copyAttachment(att, att.authorObject, issue.parentObject.key)
attachmentManager.deleteAttachment(att)
}
}

 ok

I think I can expand my add-on https://marketplace.atlassian.com/plugins/ru.mail.jira.plugins.attachdeleter to add new post-function. I will create a issue in my bug tracker

That will be a very nice feature to have!

Try using ComponentAccessor.getAttachmentManager()

And see similar artical.

Hope this helps you

Patina

Thanks, but writing a plugin for this procedure isn't an easy way for me :)

if you need to automate then you have to write postfunction or listener to do that job

else you can delete attachments by using rest api

/rest/api/2/attachment/{id}(DELETE)

check this

https://docs.atlassian.com/jira/REST/latest/#d2e3993

free plugin is there to delete all attachements at once

https://marketplace.atlassian.com/plugins/ru.mail.jira.plugins.attachdeleter

That's an interesting plugin but still not a lot difference between manual deleting.

Maybe there is a way to check existence of attachments in Issue on transition?

Hi i am using smart attachment plugin

I need to delete the attachements in some categories after the invoice is been done in the title so kindly help me to write a script in the post fuction.'

For ex : i need only input file and output file from the attachment and i need to delete other files from the attachment after the transition is been done to invoice.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you