API python jira

170 Views Asked by At

i want to change the status of an jira test plan , inside this test plan there is 10 tests that are associated to test execution 'RO-6665' i want to update the status of those test from 'TODO' to 'PASS'

BUT I GOT THIS : Failed to retrieve issue details. Status code 401 even i checked all tokens and acces :

here is my code:

import os
from jira import JIRA
import requests
import json


# Define your Jira server URL, username, and API token
JIRA_SERVER = 'example tokens'
JIRA_USER = 'example tokens'
JIRA_API_TOKEN = 'example tokens'

# Define the Jira issue key for the test plan and execution
test_plan_key = 'RO-6666'
execution_key = 'RO-6665'

# Define the new status to set (e.g., 'PASS')
new_status = 'PASS'

# Set the Jira API endpoints for issue retrieval and transition
issue_url = f"{JIRA_SERVER}/rest/api/2/issue/{execution_key}"
transitions_url = f"{issue_url}/transitions"

# Define Jira options with headers for authentication and content type
jira_options = {
    'server': JIRA_SERVER,
    'headers': {
        'Content-Type': 'application/json'
    }
}

# Authenticate using basic auth (username and API token)
auth = (JIRA_USER, JIRA_API_TOKEN)

# Step 1: Get the issue details
response = requests.get(issue_url, headers=jira_options['headers'], auth=auth)
if response.status_code != 200:
    print(f"Failed to retrieve issue details. Status code: {response.status_code}")
    exit()

issue_data = response.json()
current_status = issue_data["fields"]["status"]["name"]

# Check if the current status is 'TODO'
if current_status != 'TODO':
    print(f"Test execution {execution_key} is not in 'TODO' status. Current status: {current_status}")
else:
    # Step 2: Get available transitions for the issue
    response = requests.get(transitions_url, headers=jira_options['headers'], auth=auth)
    if response.status_code != 200:
        print(f"Failed to retrieve available transitions. Status code: {response.status_code}")
        exit()

    transitions_data = response.json()

    # Find the transition ID for the 'PASS' status
    for transition in transitions_data["transitions"]:
        if transition["to"]["name"] == new_status:
            transition_id = transition["id"]
            break
    else:
        print(f"Transition to '{new_status}' status not found.")
        exit()

    # Step 3: Perform the transition to update the status
    transition_data = {"transition": {"id": transition_id}}
    response = requests.post(transitions_url, headers=jira_options['headers'], json=transition_data, auth=auth)

    if response.status_code == 204:
        print(f"Test execution {execution_key} status updated to '{new_status}'.")
    else:
        print(f"Failed to update status. Status code: {response.status_code}")
0

There are 0 best solutions below