Forums

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

Identify which attachments belong to the Description section of an Issue

John Diamond
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 8, 2024

Hello, 

I have code that currently downloads all Issue attachments for our Jira Issues. 

However what I need to do is only grab the attachments that are contained in the Description section. 

If I use an API call like this:  

 

/rest/api/3/issue/{keyId} 

Jira_Issue.png

I can see attachments shown in the description section with an Id, but that Id doesn't appear to link to anything? e.g. Can't be used to identify which attachment / file it belongs to. 

Any ideas or suggestions would be great. 

John

1 answer

1 vote
Mikel Garcia Bartolome
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 8, 2024

Hello @John Diamond, as you have said, the key here is the "id" field in the "attrs" section of the API response, which corresponds to the unique identifier of the file attached in the description. To solve this issue, follow these steps:

Steps:

  1. Extract the Media ID: The "id" attribute in the JSON structure ("id": "8c2f448d-d1fc-4070-b5e8-12da214fb7bd") uniquely identifies the file associated with the attachment embedded in the description. You can collect all such IDs from the description field.

  2. Map the ID to the Attachments in Jira: Unfortunately, the ID shown here does not directly map to the standard attachment ID from Jira. However, you can match the filename and other media attributes in the response from the GET /rest/api/3/issue/{keyId} API call. To do this:

    • Make an additional request to retrieve the attachments list (using the same /rest/api/3/issue/{keyId}).
    • Compare the file names and dimensions (height, width) between the description section and the general list of attachments.
  3. Download the Attachment: Once you've identified the correct attachment from the general attachment list, use its actual download URL (from the content field in the attachment object) to retrieve the file.

Code Example:

  1. Step 1: Get the Issue Description Attachments: Use the API call to get the description JSON, find the media type content:
description_content = issue_data['fields']['description']['content']
description_attachments = []

for content in description_content:
if content.get('type') == 'mediaSingle':
media_content = content.get('content', [])
for media in media_content:
if media.get('type') == 'media' and media.get('attrs', {}).get('type') == 'file':
description_attachments.append(media['attrs']['id']) 

    2.  Step 2: Match Attachment IDs: Compare these IDs with the actual attachments fetched using:

attachments = issue_data['fields']['attachment']
matching_attachments = [att for att in attachments if att['id'] in description_attachments]

    3.  Step 3: Download the Matching Files: Download only the files matching those in the description:

for attachment in matching_attachments:
download_url = attachment['content']
# Proceed with downloading the file from download_url

 This approach should allow you to isolate and download only the attachments that are part of the description. Let me know if you need further clarification or help!

John Diamond
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 9, 2024

Hi Mikel, 

Thankyou very much for taking the time to respond. I appreciate it. 

Your idea of matching on properties other than the ID was a good one I thought but unfortunately I haven’t been able to get a solution so far.

The Filename for an attached image it seems is inconsistently shown. It seems to be always shown in attachments section for an Issue but in the comments section it is shown inconsistently (I thought a solution could be to see which filenames were in attachments but not in comments which would mean the remainder would be description images). I’m not sure why the filename would be inconsistently shown. It’s not just between different Issues but within comments for the same Jira. An example below:

Example1.png

Comment section extract 1

Example2.png

Comment section extract 2

Height, Width is shown in Description and Comments sections for attached images. But then there is no way to marry this up with Attachments containing the download link because it does not show these and the Id is not the same.

Perhaps I am missing something from your answer you might be able to point out to me but otherwise I am not sure how to proceed at the moment with this approach.

John

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events