Skip to content

Commit da1c38c

Browse files
authored
[Jira] two new methods: download_attachments_from_issue and get_attachments_ids_from_issue (#1334)
* fixing minor issue in scrap_regex_from_issue method * new Confluence method scrap_regex_from_page+ docs + examples * added method get_attachments_ids_from_page to jira.py * added method download_attachments_from_issue * refactoring download_all_attachments_from_page method * finished download_attachments_from_issue * added two new methods: download_attachments.from_issue and get_attachments_ids_from_issue --------- Co-authored-by: gkowalc <>
1 parent fe2cfb6 commit da1c38c

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

atlassian/jira.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding=utf-8
22
import logging
33
import re
4+
import os
45
from warnings import warn
56
from deprecated import deprecated
67
from 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

docs/jira.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ Attachments actions
489489
# Add attachment (IO Object) to issue
490490
jira.add_attachment_object(issue_key, attachment)
491491
492+
# Download attachments from the issue
493+
jira.download_attachments_from_issue(issue, path=None, cloud=True):
494+
495+
# Get list of attachments ids from issue
496+
jira.get_attachments_ids_from_issue(issue_key)
497+
492498
Manage components
493499
-----------------
494500

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from atlassian import Jira
2+
3+
jira_cloud = Jira(url="<url>", username="username", password="password")
4+
jira_dc = Jira(url="url", token="<token>>")
5+
path = "/Users/<username>>/PycharmProjects/api_python_atlassian_features/api_python_atlassian_features/atlassian-python-api/attachments"
6+
# JIRA DC using custom directory path
7+
jira_dc.download_attachments_from_issue("TEST-1", path=path, cloud=False)
8+
# Jira cloud. Attachemtns will be saved to same director where script is being executed.
9+
jira_cloud.get_attachments_ids_from_page("SC-1", cloud=True)

0 commit comments

Comments
 (0)