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}
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
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:
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.
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:
/rest/api/3/issue/{keyId}
).height
, width
) between the description section and the general list of attachments.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:
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!
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:
Comment section extract 1
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.