0

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

看起来很简单,我写了一个方法,允许我传递一个问题,然后它附加一个文件名。还有一个可以让我从 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 Method Not Allowed。

该文件存在并且可以打开。

这是代码中的 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
4

2 回答 2

0

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

尝试做类似的事情:

file_obj = open('test.txt','rb')
jira.add_attachment(issue,file_obj,'test.txt')
file_obj.close()
于 2015-05-08T18:36:33.740 回答
0

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

我也刚刚点击了这个,它发送一个 POST 请求http://instance.atlassian.net并被重定向到https://instance.atlassian.net,但是客户端向重定向的地址发送一个 GET 请求(参见:https ://softwareengineering.stackexchange.com/questions/99894/why-doesnt -http-have-post-redirect了解更多信息)

于 2015-12-16T19:39:16.493 回答