如何使用REST API从Python脚本更新Jira v5.1中的问题

我希望能够使用REST API从Python脚本更新Jira v5.1中的问题.我具有以下代码来提取现有问题的信息,这很完美:

import urllib2
import urllib
import cookielib
import json

serverURL = 'http://jiraserver.com'

# Get the authentication cookie using the REST API
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
authURL = serverURL + '/rest/auth/latest/session'
creds = {'username' : jirauser, 'password' : passwd}
req = urllib2.Request(authURL)
req.add_data(json.dumps(creds))
req.add_header("Content-type", "application/json")
req.add_header("Accept", "application/json")
fp = opener.open(req)
fp.close()

queryURL = serverURL + '/rest/api/latest/issue/SANDBOX-150'
req = urllib2.Request(queryURL)
req.add_header("Content-type", "application/json")
req.add_header("Accept", "application/json")
fp = opener.open(req)
data = json.load(fp)
fp.close()

我想扩展它以能够更新相同的问题,并且我有以下代码:

queryURL = serverURL + '/rest/api/latest/issue/SANDBOX-150'

issueUpdate =  {
    'update': {
        'comment': [
            {
                'add': {
                    'body': 'this is a comment'
                }
            }
        ]  
    }
}
req = urllib2.Request(queryURL)
req.add_data(json.dumps(issueUpdate))
req.add_header("Content-type", "application/json")
req.add_header("Accept", "application/json")
fp = opener.open(req)
fp.close()

当我尝试执行代码时,出现以下错误消息:

File "/usr/lib64/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
    File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
        'http', request, response, code, msg, hdrs)

    File "/usr/lib64/python2.6/urllib2.py", line 435, in error
        return self._call_chain(*args)

    File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
        result = func(*args)
    File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default 
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 405: Method Not Allowed

错误指向我的代码中的“ fp = opener.open(req)”.

我试图在网上搜索,看看是否可以找到解决方案,但是没有运气.有人知道我在做什么错吗?

谢谢并恭祝安康

解决方法:

如果您使用的是Python 2.7.x,建议您使用jira-python.这是一个Python包,用于处理与Jira的整个REST通信:

http://jira-python.readthedocs.org/en/latest/

上一篇:python-Confluence WIki页面未通过API更新


下一篇:更新选择列表自定义字段-Jira Python