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.
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.
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.
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.
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.
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:
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.