JIRA Python add_attachment()405方法不允许

时间:2015-05-07 21:42:02

标签: python jira python-jira

我正在尝试使用此处的python库通过其REST API将文件上传到JIRA:jira python documentation

看起来很简单我写了一个方法,允许我传递一个问题然后它附加一个文件名。并且允许我从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

然后在我的主脚本中我遇到问题并将文件附加到我从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"

如果需要,我可以解决问题/创建问题,但在使用self.jira.add_attachment()方法时,我得到405方法不允许。

该文件存在且可以打开。

以下是source代码中的add_attachment()方法:

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

2 个答案:

答案 0 :(得分:0)

documentation中提到,作为一个参数,他们期望类似文件的对象。

尝试做类似的事情:

file_obj = open('test.txt','rb')
jira.add_attachment(issue,file_obj,'test.txt')
file_obj.close()

答案 1 :(得分:0)

检查您为JIRA指定的URL(如果使用按需服务)是https://instance.atlassian.net.

我也点击了它,它向http://instance.atlassian.net发送POST请求并重定向到https://instance.atlassian.net,但是客户端向重定向的地址发送GET请求(请参阅:{{3} }了解更多信息)