如何通过 python 将附件发布到 jira

zha*_*tao 5 python jira-rest-api

我想使用 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

sad*_*uar 6

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

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)
Run Code Online (Sandbox Code Playgroud)

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


小智 5

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

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)
Run Code Online (Sandbox Code Playgroud)

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