Identify which attachments belong to the Description section of an Issue
Asked by John Diamond:

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](https://community.atlassian.com/t5/image/serverpage/image-id/346192i64F06154F73BB855/image-size/large?v=v2&px=999 "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


Answers:
author: Mikel Garcia Bartolome
body: Hello [@John Diamond](/t5/user/viewprofilepage/user-id/5581692), 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!

comments: author: John Diamond
body: 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](https://community.atlassian.com/t5/image/serverpage/image-id/346453i6FDB577D2C17D7F5/image-size/large?v=v2&px=999 "Example1.png")

Comment section extract 1

![Example2.png](https://community.atlassian.com/t5/image/serverpage/image-id/346454i21D266E970DDDA7A/image-size/large?v=v2&px=999 "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


Tags: cloud

jira-cloud