Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

jira-python attached file filename

snuggles February 14, 2013

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!

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
John Welsh March 6, 2013

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')

snuggles March 11, 2013

Sending hugbot 2000 to you. Thank you! I wish I had thought of that.

0 votes
Jean-Martin Archer July 19, 2013

For people wondering, the library now support definition of the filename.

0 votes
Jean-Martin Archer March 14, 2013

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

0 votes
snuggles February 19, 2013

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!

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events