【问题标题】:How can I post an attachment to jira via python如何通过python将附件发布到jira
【发布时间】:2015-02-24 08:41:31
【问题描述】:

我想使用 python 通过 jira rest api 将附件发布到 jira,并且不需要安装任何其他包。 我注意到“这个资源需要一个多部分的帖子。”,我试了一下,但也许我的方法是错误的,我失败了

我只想知道如何通过 python urllib2 执行以下命令: "curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" /rest/api/2/issue/TEST-123/attachments" 而且我不想使用 subprocess.popen

【问题讨论】:

  • 做一些研究,自己尝试一些事情,如果您遇到困难或失败,我们随时为您提供帮助。告诉我们你的代码没有给你结果,我们会让你知道你哪里出错了:)

标签: python jira-rest-api


【解决方案1】:

和官方文档一样,我们需要以二进制方式打开文件,然后上传。我希望下面的一小段代码可以帮助你:)

from jira import JIRA    

# Server Authentication
username = "XXXXXX"
password = "XXXXXX"

jira = JIRA(options, basic_auth=(str(username), str(password)))

# Get instance of the ticket
issue = jira.issue('PROJ-1')

# Upload the file
with open('/some/path/attachment.txt', 'rb') as f:
    jira.add_attachment(issue=issue, attachment=f)

https://jira.readthedocs.io/en/master/examples.html#attachments

【讨论】:

  • 但是有一个问题,当你使用python3.8时,它会抛出一个错误,就像“dict在迭代过程中不断变化......”。你必须降级到 3.7
【解决方案2】:

让它工作的关键是设置多部分编码的文件:

import requests

# Setup authentication credentials
credentials  = requests.auth.HTTPBasicAuth('USERNAME','PASSWORD')

# JIRA required header (as per documentation)
headers = { 'X-Atlassian-Token': 'no-check' }

# Setup multipart-encoded file
files = [ ('file', ('file.txt', open('/path/to/file.txt','rb'), 'text/plain')) ]

# (OPTIONAL) Multiple multipart-encoded files
files = [
    ('file', ('file.txt', open('/path/to/file.txt','rb'), 'text/plain')),
    ('file', ('picture.jpg', open('/path/to/picture.jpg', 'rb'), 'image/jpeg')),
    ('file', ('app.exe', open('/path/to/app.exe','rb'), 'application/octet-stream'))
]
# Please note that all entries are called 'file'. 
# Also, you should always open your files in binary mode when using requests.

# Run request
r = requests.post(url, auth=credentials, files=files, headers=headers)

https://2.python-requests.org/en/master/user/advanced/#post-multiple-multipart-encoded-files

【讨论】:

  • 谢谢,当 atlassian python API 包让我失望时,我能够使用此方法将临时附件上传到 Jira Service Desk。HTTP 错误 415。
【解决方案3】:

对不起,我不清楚的问题

感谢How to POST attachment to JIRA using REST API?。 我已经解决了。

    boundary = '----------%s' % ''.join(random.sample('0123456789abcdef', 15))
    parts = []

    parts.append('--%s' % boundary)
    parts.append('Content-Disposition: form-data; name="file"; filename="%s"' % fpath)
    parts.append('Content-Type: %s' % 'text/plain')
    parts.append('')
    parts.append(open(fpath, 'r').read())

    parts.append('--%s--' % boundary)
    parts.append('')

    body = '\r\n'.join(parts)

    url = deepcopy(self.topurl)
    url += "/rest/api/2/issue/%s/attachments" % str(jrIssueId)
    req = urllib2.Request(url, body)
    req.add_header("Content-Type", "multipart/form-data; boundary=%s" % boundary)
    req.add_header("X-Atlassian-Token", "nocheck")
    res = urllib2.urlopen(req)

    print res.getcode()
    assert res.getcode() in range(200,207), "Error to attachFile " + jrIssueId
    return res.read()

【讨论】:

    【解决方案4】:

    您可以使用jira-python 包。

    这样安装:

    pip install jira-python
    

    要添加附件,请使用jira.client.JIRA 类的add_attachment 方法:

    add_attachment(*args, **kwargs) 将附件附加到问题并为其返回资源。

    客户端将不会尝试 打开或验证附件;它期望一个类似文件的对象 准备好使用它。用户仍负责整理 (例如,关闭文件、杀死套接字等)

    参数:
  • issue – 附加附件的问题 到
  • attachment – 类似文件的对象 附加到问题上,如果它是带有 文件名。
  • 文件名 – 可选名称 附件。如果省略,文件对象的名称属性 用来。如果您通过任何其他方法获取类文件对象,而不是 打开(), 确保以一种或另一种方式指定名称。
  • 您可以在official documentation中找到更多信息和示例

    【讨论】:

    • 对不起,我需要在 jenkins 中运行这个脚本,所以无法安装任何包。但幸运的是,我已经解决了。还是谢谢
    猜你喜欢
    • 2012-08-06
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2014-09-18
    相关资源
    最近更新 更多