Missed Team ’24? Catch up on announcements here.

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

JIRA file attach listener

Radek Kantor
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 19, 2012

Hi,

what is the best way to monitor file attach action? There is nothing like EventType.ISSUE_FILEATTACH for listener implementation. I need to detect actions, when user add new file attachments and execute own post processing methods.

Is the correct way to do this by servlet-filter plugin module on action AttachFile?

Or it is better catch ISSUE_UPDATED_ID event and check issue change log event if contans change item with field "Attachment", than get file ID according "newvalue" in change log by attachment manager.

List<GenericValue> changeItems = issueEvent.getChangeLog().getRelated("ChildChangeItem");
for (GenericValue changeItem : changeItems) {
if (((String)changeItem.get("fieldtype")).equalsIgnoreCase("JIRA") && 
       ((String)changeItem.get("field")).equalsIgnoreCase("Attachment")) {
          String id = changeItem.get("newvalue").toString();
          Attachment attachment = ComponentAccessor.getAttachmentManager().getAttachment(Long.valueOf(id));
   }
}

In the second case, how can I get attachment comment (there are no method) and attachment file content?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
JamieA
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 19, 2012

Given an event you can see if attachments have been added using the following code:

List changeItems = event.getChangeLog()?.getRelated("ChildChangeItem")
                    changeItems.each {GenericValue gv ->
                        if (gv["field"] == "Attachment" && gv["newvalue"]) {
                            attachmentIds.add(gv["newvalue"])
                        }
                    }

treat it as pseudocode if you like.

Then...

attachmentIds.each {attachmentId ->
                Attachment attachment = issue.attachments.find {Attachment attachment ->
                    attachment.id == new Long(attachmentId)
                }
                File attFile = AttachmentUtils.getAttachmentFile(attachment)

Jannik Mortensen October 2, 2014

Where should i put this code? is it a listner?

3 votes
Radek Kantor
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 20, 2012

Thanks Jamie, exactly the same solution as my.

0 votes
Mizan
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 19, 2012

Hi Xrakan,

you can refer this question. Its something similar

Radek Kantor
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 19, 2012

Hi, I found this thread, but I think that extends AttachFile is not the best solution. How can I used new Action in listener? I dont want to overwrite AttachFile class (to do this this must be plugin v1). Can you help with get file content and attachment comment? What manager, servoce or helper class should I use?

Radek Kantor
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 19, 2012
File f = AttachmentUtils.getAttachmentFile(attachment);
Mizan
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 19, 2012

dont know how to get the contents of the file attached . an attachment can be a jar , a jpg file , etc .. what you want to do after you get the content of the attachment ? i am not sure but extending actions also work for ver 2 plugins .

Radek Kantor
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 19, 2012
Comment comment = issueEvent.getComment();

Im looking comment on the attachment, but it is associated with event :)

2Mizan: Im working on synchronization adapter JIRA vs. some another helpdesk system and I need detect some basic events like create/edit issue, add comment, attachment, ... than use customer WS interface and populate JIRA changes (including file content).

TAGS
AUG Leaders

Atlassian Community Events