为什么这不会造成JIRA问题?

时间:2013-02-20 22:31:56

标签: python api rest jira pycharm

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

这会产生我的错误stacktrace / log

/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 任何人吗?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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.verify设置为False

至于调用restjira.py的原因,那很可能是加载这个模块的模块。


其他一些想法:

  • 除非您将jira更改为import jira.exceptions之类的内容,否则不应将变量命名为import jira.exceptions as jiraexceptions。它使代码更容易阅读。
  • verify = False... verify=verify有点无关紧要。为什么不把False放在kwargs?