From df3b03dc99821b89b7807204e877c2ecb9fa00e2 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 2 Apr 2014 00:17:19 +0800 Subject: [PATCH] New API method to get issue attrachments from the tracker --- lib/jira.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/jira.py b/lib/jira.py index 051c520..a4f60db 100644 --- a/lib/jira.py +++ b/lib/jira.py @@ -96,6 +96,33 @@ class Jira(object): """Returns a property of this instance""" return self.info().get(param, default) + def getAttachments(self, key): + """Get a dict of attachments + + The keys are the filenames, the values are another dict containing: + - id: The file ID on the Tracker + - filename: The file name + - URL: The URL to download the file + - date: A datetime object representing the date at which the file was created + - mimetype: The mimetype of the file + - size: The size of the file in bytes + - author: The username of the author of the file + """ + issueInfo = self.getIssue(key, fields='attachment') + results = issueInfo.get('fields').get('attachment', []) + attachments = {} + for attachment in results: + attachments[attachment.get('filename')] = { + 'id': attachment.get('id'), + 'filename': attachment.get('filename'), + 'url': attachment.get('content'), + 'date': Jira.parseDate(attachment.get('created')), + 'mimetype': attachment.get('mimeType'), + 'size': attachment.get('size'), + 'author': attachment.get('author', {}).get('name'), + } + return attachments + def getIssue(self, key, fields='*all,-comment'): """Load the issue info from the jira server using a rest api call. @@ -237,7 +264,7 @@ class Jira(object): @staticmethod def parseDate(value): - """Parse a date returned by Jira API""" + """Parse a date returned by Jira API and returns a datetime object.""" # Strips the timezone information because of some issues with %z. value = re.sub(r'[+-]\d+$', '', value) return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f') -- 2.11.0