3

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.

4
  • 1
    Can you get more details about the error by excepting it and printing? except HTTPError as e: print(e.response.text) Commented Sep 5, 2023 at 15:16
  • @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'? Commented Sep 5, 2023 at 15:19
  • You need to add that error to namespace by importing it from request first... Commented Sep 5, 2023 at 15:20
  • 3
    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 Commented Sep 7, 2023 at 6:49

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Printing out the exception text really helped; as in "HTTPError as e: print(e.response.text)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.