Hi, I'm using jira-python (http://jira-python.readthedocs.org/en/latest/) to attach files to existing issues. The files are being attached ok, but the problem is how Jira is presenting them in the UI.
Here's the code:
from jira.client import JIRA jira = JIRA(basic_auth=([username], [password]), options={'server': [our_jira_server]}) issue = jira.issue([issue_id]) file = open('/tmp/test2.txt') attachment_object = jira.add_attachment(issue,file)
The above attaches the file to the issue, but the url to the file in the jira issue is this:
https://[our_jira_server]/jira/secure/attachment/58750/%2Ftmp%2Ftest2.txt
ie: it includes the *path* in the filename used by jira. The above url does not exist on jira, however, if I go here:
https://[our_jira_server]/jira/secure/attachment/58750/test2.txt
The file is there.
Note that this works perfectly:
from jira.client import JIRA jira = JIRA(basic_auth=([username], [password]), options={'server': [our_jira_server]}) issue = jira.issue([issue_id]) file = open('test.txt') attachment_object = jira.add_attachment(issue,file)
Since there is no path in the file name, the attachment is displayed in the jira issue like this:
https://[our_jira_server]/jira/secure/attachment/58751/test.txt
I'm not sure what to do. I've tried:
1. finding some way to rename the file before uploading it to JIRA. EG:
file.filename = 'test3.txt'
But filename is readonly attr.
2. tried to change the name of the file once it was uploaded by editing the returned attachment object. EG:
attachment_object.filename = 'test3.txt'
This changes the name in the attachment_object dict in the python session, but doesn't make any change on the jira side.
If you can point me in the right direction that would be very helpful. Thank you!
Community moderators have prevented the ability to post new answers.
The workaround I used was to call os.chdir() to change the working directory to the folder that contains the file, then file = open('myfilename', 'rb')
For people wondering, the library now support definition of the filename.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I changed client.py (so you can pass a filename):
def add_attachment(self, issue, filename, attachment): """ Attach an attachment to an issue and returns a Resource for it. The client will *not* attempt to open or validate the attachment; it expects a file-like object to be ready for its use. The user is still responsible for tidying up (e.g., closing the file, killing the socket, etc.) :param issue: the issue to attach the attachment to :param attachment: file-like object to attach to the issue :rtype: an Attachment Resource """ # TODO: Support attaching multiple files at once? url = self._get_url('issue/' + issue + '/attachments') files = { 'file': (filename,attachment) } r = self._session.post(url, files=files, headers={'X-Atlassian-Token': 'nocheck', 'content-type': JIRA.SUPPRESS_CONTENT_TYPE_AUTODETECT}) raise_on_error(r) attachment = Attachment(self._options, self._session, json.loads(r.text)[0]) return attachment
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It doesn't seem possible that nobody knows how to do this. I'm wondering if I'm either not being clear or if this is the wrong place to ask this question. If you can even just tell me I'm in the wrong place, that would be extremely helpful. I'm not sure what the best place to get help on the jira-python module is... Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.