changeset 35:94b0b7b4f190

attachment filename for bzapi
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 14 Dec 2012 14:54:27 -0800
parents 1ce13b2b54a4
children 619e27c447b8
files bzconsole/api.py
diffstat 1 files changed, 15 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/bzconsole/api.py	Fri Dec 14 14:38:27 2012 -0800
+++ b/bzconsole/api.py	Fri Dec 14 14:54:27 2012 -0800
@@ -20,8 +20,8 @@
     console API to bugzilla
     """
 
+    # currently there is only one cache for configuration
     config_cache = '.bzconfiguration'
-    # XXX currently this is hard coded for one server
 
     def __init__(self,
                  server='https://api-dev.bugzilla.mozilla.org/latest',
@@ -155,12 +155,12 @@
         # return the URL
         return results['ref']
 
-    def attach(self, bug, contents, description=None, reviewer=None, comment=None):
+    def attach(self, bug, attachment, description=None, reviewer=None, comment=None):
         """
         add an attachment to a bug
 
         - bug: bug number to attach to
-        - contents: file or URL of attachment
+        - attachment: file or URL of attachment
         - reviewer: flag for review (r?)
         - comment: add this comment to the bug
         """
@@ -168,16 +168,27 @@
         if not description:
             description = contents
 
+        # read contents
+        if '://' in attachment:
+            # URL
+            basename = attachment.rstrip('/').rsplit('/', 1)[-1]
+            contents = urllib2.urlopen(attachment).read()
+        else:
+            # file path
+            basename  = os.path.basename(attachment)
+            contents = file(attachment).read()
+
         # create attachment data structure
         # https://wiki.mozilla.org/Bugzilla:REST_API:Objects#Attachment
         attachment= {'bug': int(bug),
                      'description': description,
+                     'file_name': basename
                      }
         if reviewer:
             flags = []
             pass # TODO
 
-        self.request('/bug/%s/attachment', attachment)
+        self.request('/bug/%s/attachment' % bug, attachment)
 
     def configuration(self):
         """bugzilla configuration"""