Using Python to modify Jira test plan status

Hey everyone,

I’m trying to update the status of tests in a Jira test plan. There are 10 tests linked to test execution ‘RO-6665’ and I want to change their status from ‘TODO’ to ‘PASS’.

I wrote some Python code to do this, but I’m getting a 401 error when trying to retrieve issue details. I’ve double-checked my tokens and access, but it’s still not working.

Here’s a simplified version of what I’m trying to do:

import requests

JIRA_SERVER = 'https://myjira.example.com'
JIRA_USER = 'myusername'
JIRA_API_TOKEN = 'mytoken'

test_execution = 'RO-6665'
new_status = 'PASS'

issue_url = f'{JIRA_SERVER}/rest/api/2/issue/{test_execution}'

headers = {'Content-Type': 'application/json'}
auth = (JIRA_USER, JIRA_API_TOKEN)

response = requests.get(issue_url, headers=headers, auth=auth)

if response.status_code != 200:
    print(f'Error: {response.status_code}')
else:
    # Code to update status would go here
    print('Successfully retrieved issue details')

Any ideas why I might be getting a 401 error? Are there any common pitfalls with Jira API authentication that I should watch out for? Thanks for your help!

hey there! i’ve run into similar issues before. Make sure ur using an API token, not ur regular password. Also, double-check the permissions on ur Jira account - sometimes u need specific access for API calls. if that doesnt work, try using the requests.auth.HTTPBasicAuth module for authentication. good luck!

I’ve dealt with Jira API authentication headaches before, and there are a few things you might want to check. First, ensure your API token is current - they can expire. Also, verify that your account has the necessary permissions for the specific API endpoint you’re hitting.

One thing that helped me was using the Jira Python library (jira) instead of raw requests. It handles a lot of the authentication complexity for you. Here’s a quick example:

from jira import JIRA

jira = JIRA(server=JIRA_SERVER, basic_auth=(JIRA_USER, JIRA_API_TOKEN))

issue = jira.issue(test_execution)
# Now you can work with the issue object

This approach simplified things for me and resolved similar 401 errors. If you’re still stuck, double-check your server URL - sometimes a small typo can cause authentication failures. Hope this helps!

I’ve encountered similar authentication issues with the Jira API. One often overlooked aspect is the API token scope. Ensure your token has the necessary permissions for the specific operations you’re trying to perform.

Another potential issue could be with the Jira server’s SSL certificate. If it’s self-signed or expired, you might need to disable SSL verification (though this isn’t recommended for production use).

Have you tried using the Jira Python library as suggested by another user? If not, you could also try debugging by adding more detailed error handling:

response = requests.get(issue_url, headers=headers, auth=auth)
print(f'Response content: {response.content}')
print(f'Response headers: {response.headers}')

This might provide more insight into why the authentication is failing. If all else fails, regenerating your API token and double-checking your account permissions in Jira would be my next steps.