1
from jira.client import JIRA
import requests
import jira.exceptions
import json
import re

class MyJira(JIRA):

    def _create_http_basic_session(self, username, password):
        url = self._options['server'] + '/rest/auth/1/session'
        payload = {
            'username': username,
            'password': password
        }
        verify = False
        self._session = requests.session(verify=verify,
            hooks={'args': self._add_content_type})
        r = self._session.post(url, data=json.dumps(payload))
        jira.exceptions.raise_on_error(r)

options = {'server':'http://localhost:8080',
           'verify': False}
username = 'usrName'
password = 'passWord'

basic_auth =(username,password)
jira = MyJira(options, basic_auth)

issue_dict = {
    'project':{'key':'PROJKEY'},
    'summary': 'New Issue from jira-python',
    'description': 'Test Description for new issue created from jira-python',
    'issueType': {'name': 'Bug'}}

new_issue = jira.create_issue(fields=issue_dict,preftech=True)
print new_issue.id

这产生了我这个错误堆栈跟踪/日志

/home/ve/bin/python/home/PycharmProjects/RestJira/restjira.py
Traceback (most recent call last):
  File "/home/PycharmProjects/RestJira/myrestjira.py", line 81, in <module>
    new_issue = jira.create_issue(fields=issue_dict,preftech=True)
  File "/home/ve/lib/python2.6/site-packages/jira/client.py", line 369, in create_issue
    raise_on_error(r)
  File "/home/ve/lib/python2.6/site-packages/jira/exceptions.py", line 29, in raise_on_error
    error = errorMessages[0]

IndexError: list index out of range

我究竟做错了什么?我不明白为什么除了我目前正在使用的文件(myrestjira.py)之外,还有对另一个文件(restjira.py)的引用我正在使用 python 2.7,jira-python 库,请求 0.14.1,jira- python 0.12 和 pycharm 作为 IDE 有人吗?

4

2 回答 2

6

试试这个,它的工作...

from jira.client import JIRA

jira_options={'server': 'http://jira-address'}
jira=JIRA(options=jira_options,basic_auth=('user','password'))

#Creating an issue:

new_issue = jira.create_issue(project={'key': 'key'}, summary='New issue from jira-   python',   description='Look into this one', issuetype={'name': 'Bug'})

# or creating issues using dict:

issue_dict = {
    'project': {'key': 'PROJ'},
    'summary': 'New issue from jira-python',
    'description': 'Look into this one',
    'issuetype': {'name': 'Bug'},
}
new_issue = jira.create_issue(fields=issue_dict)
于 2013-06-28T05:34:42.493 回答
1

您的问题在于这两行:

r = self._session.post(url, data=json.dumps(payload))
jira.exceptions.raise_on_error(r)

您的回复self._session.post是一个错误,但收到的错误有问题。这导致了几个问题。

  • 您能否确认您的 Jira 服务器已启动并正在运行localhost:8080
  • 如果你会发生什么pprint(r)
  • 用户名和密码是否正确?
  • 设置self._session.verifyFalse

至于为什么restjira.py被调用,嗯,很可能是加载了这个模块的那个模块。


其他一些想法:

  • jira除非您更改为import jira.exceptions类似import jira.exceptions as jiraexceptions. 它使代码更易于阅读。
  • verify = False... verify=verify有点无关紧要。为什么不直接放在Falsekwargs 中?
于 2013-02-21T04:23:36.647 回答