I have been trying to write a python script to automatically raise Jira tickets, and have been running into some trouble. To be more specific, I tried to use both the issue_create and create_issue methods as outlined in the atlassian-python API reference. In the code provided below, I successfully obtain the correct project id just to verify that my authentication (PAT) works. However the second part fails and a Jira ticket (task) is not created.
我一直在尝试编写一个python脚本来自动筹集Jira门票,但遇到了一些麻烦。更具体地说,我尝试同时使用issue_create和create_issue方法,如atlassian-python API参考中所述。在下面提供的代码中,我成功地获得了正确的项目id,只是为了验证我的身份验证(PAT)是否有效。然而,第二部分失败,并且没有创建Jira票证(任务)。
For reference, here is my code:
作为参考,这里是我的代码:
from atlassian import Jira
jira = Jira(
url = "https://jira.example.com/",
token = "MyPersonalAccessToken"
)
proj = jira.get_project('key', expand=None)
print(proj.get("id")) # to verify that authentication worked
jira = Jira(
url = "https://jira.example.com/rest/api/2/issue",
token = "MyPersonalAccessToken"
)
jira.issue_create(
fields={
'project': {
'key': 'key'
},
'summary': 'Testing JIRA python API',
'description': 'testing',
'issuetype': {
"name": "Task"
},
}
)
Below is the output I get when running the code above:
下面是我在运行上面的代码时得到的输出:
<PROJECT ID>
Creating issue "Testing JIRA python API"
Traceback (most recent call last):
File "/Users/<user>/jira_python/jira.py", line 21, in <module>
jira.issue_create(
File "/Users/<user>/.local/share/virtualenvs/new_project-RFzzfWjC/lib/python3.10/site-packages/atlassian/jira.py", line 1435, in issue_create
return self.post(url, data={"fields": fields})
File "/Users/<user>/.local/share/virtualenvs/new_project-RFzzfWjC/lib/python3.10/site-packages/atlassian/rest_client.py", line 333, in post
response = self.request(
File "/Users/<user>/.local/share/virtualenvs/new_project-RFzzfWjC/lib/python3.10/site-packages/atlassian/rest_client.py", line 257, in request
self.raise_for_status(response)
File "/Users/<user>/.local/share/virtualenvs/new_project-RFzzfWjC/lib/python3.10/site-packages/atlassian/rest_client.py", line 490, in raise_for_status
raise HTTPError(error_msg, response=response)
requests.exceptions.HTTPError
I should also note that I have tried using just the base URL (jira.example.com) but I also received the same error. Please note that in the above code, the url and token have been modified for obvious reasons. I've tried using try-except to catch the error but to no avail.
我还应该注意,我尝试过只使用基本URL(jira.example.com),但也收到了同样的错误。请注意,在上面的代码中,由于明显的原因,url和令牌被修改了。我尝试过使用try,但没有发现错误。
How can I find out where I'm going wrong and why my issues are not being created?
我如何才能找出我哪里出了问题,以及为什么我的问题没有产生?
Please let me know if I should provide further information, and thank you in advance.
请让我知道我是否应该提供进一步的信息,并提前感谢你。
更多回答
Can you get more details about the error by excepting it and printing? except HTTPError as e: print(e.response.text)
你能通过排除错误并打印来获得更多关于错误的详细信息吗?HTTPError除外,如e:print(e.response.text)
@matszwecja This is what I get: Traceback (most recent call last): File "/Users/<user>/jira_python/jira.py", line 33, in <module> except HTTPError as e: NameError: name 'HTTPError' is not defined. Did you mean: 'TabError'?
@matszwecja这就是我得到的:Traceback(最后一次调用):文件“/Users//jira_python/jira.py”,第33行,在中,HTTPError除外,因为e:NameError:名称“HTTPError”未定义。你是说:“TabError”吗?
You need to add that error to namespace by importing it from request first...
您需要通过首先从请求导入该错误来将其添加到命名空间。。。
As it turns out, I was missing a mandatory field in the payload (fields) and the url I was using in the issue_create call was incorrect, but using the exception + print helped me catch it. I just wasn't handling the exception properly before. If you'd like, you can post your comment as an answer and I will mark it as the solution. Thank you for the help @matszwecja
I wanted to provide an answer here, for which I want to provide most of the credit to @matszwecja who hinted how to properly raise an exception so I can find out what's going on.
我想在这里提供一个答案,为此我想向@matszwecja提供大部分信息,他暗示了如何正确地提出异常,以便我了解发生了什么。
After adding an exception handler, I was able to catch the two issues that were preventing my script from working as intended:
在添加了一个异常处理程序后,我发现了两个问题,这两个问题阻碍了我的脚本按预期工作:
The url parameter in the issue_create call should be "https://jira.example.com" instead of "https://jira.example.com/rest/api/2/issue", the issue_create function adds the correct endpoint automatically.
issue_create调用中的url参数应为“https://jira.example.com“而不是”https://jira.example.com/rest/api/2/issue”,issue_create函数会自动添加正确的端点。
I was missing a mandatory custom field, which was specific to my Jira project settings. Using an exception handler helped me find that out. See the code that worked below:
我缺少一个强制性的自定义字段,它是特定于我的Jira项目设置的。使用异常处理程序帮助我发现了这一点。请参阅下面的代码:
from atlassian import Jira
from requests import HTTPError
jira = Jira(
url = "https://jira.example.com/",
token = "MyPersonalAccessToken"
)
try:
jira.issue_create(
fields={
'project': {
'key': 'key'
},
'summary': 'Testing JIRA python API',
'description': 'testing',
'issuetype': {
"name": "Task"
},
}
)
except HTTPError as e:
print(e.response.text)
I hope this helps anyone who may run into similar issues.
我希望这能帮助任何可能遇到类似问题的人。
更多回答
我是一名优秀的程序员,十分优秀!