11# coding=utf-8
22import logging
33import re
4+ import os
45from warnings import warn
56from deprecated import deprecated
67from requests import HTTPError
@@ -163,6 +164,18 @@ def get_application_role(self, role_key):
163164 Reference: https://docs.atlassian.com/software/jira/docs/api/REST/8.5.0/#api/2/attachment
164165 """
165166
167+ def get_attachments_ids_from_issue (self , issue ):
168+ """
169+ Get attachments IDs from jira issue
170+ :param jira issue key: str
171+ :return: list of integers attachment IDs
172+ """
173+ issue_id = self .get_issue (issue )["fields" ]["attachment" ]
174+ list_attachments_id = []
175+ for attachment in issue_id :
176+ list_attachments_id .append ({"filename" : attachment ["filename" ], "attachment_id" : attachment ["id" ]})
177+ return list_attachments_id
178+
166179 def get_attachment (self , attachment_id ):
167180 """
168181 Returns the meta-data for an attachment, including the URI of the actual attached file
@@ -173,6 +186,44 @@ def get_attachment(self, attachment_id):
173186 url = "{base_url}/{attachment_id}" .format (base_url = base_url , attachment_id = attachment_id )
174187 return self .get (url )
175188
189+ def download_attachments_from_issue (self , issue , path = None , cloud = True ):
190+ """
191+ Downloads all attachments from a Jira issue.
192+ :param issue: The issue-key of the Jira issue
193+ :param path: Path to directory where attachments will be saved. If None, current working directory will be used.
194+ :param cloud: Use True for Jira Cloud, false when using Jira Data Center or Server
195+ :return: A message indicating the result of the download operation.
196+ """
197+ try :
198+ if path is None :
199+ path = os .getcwd ()
200+ issue_id = self .issue (issue , fields = "id" )["id" ]
201+ if cloud :
202+ url = self .url + f"/secure/issueAttachments/{ issue_id } .zip"
203+ else :
204+ url = self .url + f"/secure/attachmentzip/{ issue_id } .zip"
205+ response = self ._session .get (url )
206+ attachment_name = f"{ issue_id } _attachments.zip"
207+ file_path = os .path .join (path , attachment_name )
208+ # if Jira issue doesn't have any attachments _session.get request response will return 22 bytes of PKzip format
209+ file_size = sum (len (chunk ) for chunk in response .iter_content (8196 ))
210+ if file_size == 22 :
211+ return "No attachments found on the Jira issue"
212+ if os .path .isfile (file_path ):
213+ return "File already exists"
214+ with open (file_path , "wb" ) as f :
215+ f .write (response .content )
216+ return "Attachments downloaded successfully"
217+
218+ except FileNotFoundError :
219+ raise FileNotFoundError ("Verify if directory path is correct and/or if directory exists" )
220+ except PermissionError :
221+ raise PermissionError (
222+ "Directory found, but there is a problem with saving file to this directory. Check directory permissions"
223+ )
224+ except Exception as e :
225+ raise e
226+
176227 def get_attachment_content (self , attachment_id ):
177228 """
178229 Returns the content for an attachment
0 commit comments