jira-python attached file filename

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

2 votes
Answer accepted

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

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

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!

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 6 hours ago in United States

Atlassian acquires AgileCraft

         Good Day, Bad bad traffic, not sure why!!!! 1/2 hour commute took me 2 hours today 🤯 What helped me is that I kept browsing LinkedIn until...

29 views 1 0
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you