# HG changeset patch # User Jeff Hammel # Date 1355525667 28800 # Node ID 94b0b7b4f19001532e3ae6f251678ad57e27cb6e # Parent 1ce13b2b54a4ae3f8081f6a1ee5b4ee79ce3f0c4 attachment filename for bzapi diff -r 1ce13b2b54a4 -r 94b0b7b4f190 bzconsole/api.py --- 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"""