Skip to content

Commit

Permalink
[Jira] two new methods: download_attachments_from_issue and get_attac…
Browse files Browse the repository at this point in the history
…hments_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 <>
  • Loading branch information
gkowalc authored Feb 10, 2024
1 parent fe2cfb6 commit da1c38c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
51 changes: 51 additions & 0 deletions atlassian/jira.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
import logging
import re
import os
from warnings import warn
from deprecated import deprecated
from requests import HTTPError
Expand Down Expand Up @@ -163,6 +164,18 @@ def get_application_role(self, role_key):
Reference: https://docs.atlassian.com/software/jira/docs/api/REST/8.5.0/#api/2/attachment
"""

def get_attachments_ids_from_issue(self, issue):
"""
Get attachments IDs from jira issue
:param jira issue key: str
:return: list of integers attachment IDs
"""
issue_id = self.get_issue(issue)["fields"]["attachment"]
list_attachments_id = []
for attachment in issue_id:
list_attachments_id.append({"filename": attachment["filename"], "attachment_id": attachment["id"]})
return list_attachments_id

def get_attachment(self, attachment_id):
"""
Returns the meta-data for an attachment, including the URI of the actual attached file
Expand All @@ -173,6 +186,44 @@ def get_attachment(self, attachment_id):
url = "{base_url}/{attachment_id}".format(base_url=base_url, attachment_id=attachment_id)
return self.get(url)

def download_attachments_from_issue(self, issue, path=None, cloud=True):
"""
Downloads all attachments from a Jira issue.
:param issue: The issue-key of the Jira issue
:param path: Path to directory where attachments will be saved. If None, current working directory will be used.
:param cloud: Use True for Jira Cloud, false when using Jira Data Center or Server
:return: A message indicating the result of the download operation.
"""
try:
if path is None:
path = os.getcwd()
issue_id = self.issue(issue, fields="id")["id"]
if cloud:
url = self.url + f"/secure/issueAttachments/{issue_id}.zip"
else:
url = self.url + f"/secure/attachmentzip/{issue_id}.zip"
response = self._session.get(url)
attachment_name = f"{issue_id}_attachments.zip"
file_path = os.path.join(path, attachment_name)
# if Jira issue doesn't have any attachments _session.get request response will return 22 bytes of PKzip format
file_size = sum(len(chunk) for chunk in response.iter_content(8196))
if file_size == 22:
return "No attachments found on the Jira issue"
if os.path.isfile(file_path):
return "File already exists"
with open(file_path, "wb") as f:
f.write(response.content)
return "Attachments downloaded successfully"

except FileNotFoundError:
raise FileNotFoundError("Verify if directory path is correct and/or if directory exists")
except PermissionError:
raise PermissionError(
"Directory found, but there is a problem with saving file to this directory. Check directory permissions"
)
except Exception as e:
raise e

def get_attachment_content(self, attachment_id):
"""
Returns the content for an attachment
Expand Down
6 changes: 6 additions & 0 deletions docs/jira.rst
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ Attachments actions
# Add attachment (IO Object) to issue
jira.add_attachment_object(issue_key, attachment)
# Download attachments from the issue
jira.download_attachments_from_issue(issue, path=None, cloud=True):
# Get list of attachments ids from issue
jira.get_attachments_ids_from_issue(issue_key)
Manage components
-----------------

Expand Down
9 changes: 9 additions & 0 deletions examples/jira/jira_download_attachments.from_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from atlassian import Jira

jira_cloud = Jira(url="<url>", username="username", password="password")
jira_dc = Jira(url="url", token="<token>>")
path = "/Users/<username>>/PycharmProjects/api_python_atlassian_features/api_python_atlassian_features/atlassian-python-api/attachments"
# JIRA DC using custom directory path
jira_dc.download_attachments_from_issue("TEST-1", path=path, cloud=False)
# Jira cloud. Attachemtns will be saved to same director where script is being executed.
jira_cloud.get_attachments_ids_from_page("SC-1", cloud=True)

0 comments on commit da1c38c

Please sign in to comment.