0

I am trying to upload a file to JIRA via its REST API using the python lib found here: jira python documentation

It seems pretty straight forward I wrote a method that allows me to pass an issue and then it attaches a filename. and one that lets me retrieve an issue from JIRA.

from jira.client import JIRA

class JIRAReport (object):
    def attach(self,issue):
            print 'Attaching... '
            attachment = self.jira.add_attachment(issue, attachment=self.reportpath, filename='Report.xlsx')
            print 'Success!'

    def getissue(self):
            if not self.issue == None:
                return self.jira.issue(self.issue)
            return None

then in my main script I am getting the issue and attaching the file to an issue I retrieved from JIRA

report = JiraReport()
report.issue = 'ProjectKey-1'
report.reportpath = '../report_upload/tmp/' + filename
issue = report.getissue()
if not issue == None:
    report.attach(issue)
else:
    print "No Issue with Key Found"

I am able to get the issue/create issues if needed but when using the self.jira.add_attachment() method I am getting 405 Method Not Allowed.

The file exists and is able to be opened.

Here is the add_attachment() method from the source code:

def add_attachment(self, issue, attachment, filename=None):
        """
        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, also works if it is a string with the filename.
        :param filename: optional name for the attached file. If omitted, the file object's ``name`` attribute
            is used. If you aquired the file-like object by any other method than ``open()``, make sure
            that a name is specified in one way or the other.
        :rtype: an Attachment Resource
        """
        if isinstance(attachment, string_types):
            attachment = open(attachment, "rb")
        # TODO: Support attaching multiple files at once?
        url = self._get_url('issue/' + str(issue) + '/attachments')

        fname = filename
        if not fname:
            fname = os.path.basename(attachment.name)

        content_type = mimetypes.guess_type(fname)[0]
        if not content_type:
            content_type = 'application/octet-stream'

        files = {
            'file': (fname, attachment, content_type)
        }
        r = self._session.post(url, files=files, headers=self._options['headers'])
        raise_on_error(r)

        attachment = Attachment(self._options, self._session, json.loads(r.text)[0])
        return attachment
Richard Christensen
  • 2,046
  • 17
  • 28

2 Answers2

0

It is mentioned in documentation that as a argument they expect file-like object.

Try to do something like :

file_obj = open('test.txt','rb')
jira.add_attachment(issue,file_obj,'test.txt')
file_obj.close()
ThePavolC
  • 1,693
  • 1
  • 16
  • 26
0

Check that the URL that you are specifying for JIRA (if using the on-demand service) is https://instance.atlassian.net.

I just hit this as well, and it sends a POST request to http://instance.atlassian.net and gets redirected to https://instance.atlassian.net, but the client sends a GET request to the redirected address (see: https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect for more information)

Community
  • 1
  • 1