Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

REST API for Cloud returning 404 Error

Stephen Byrd August 3, 2023 edited

I am able to add a comment when using a specified ticket number, but when I pull the ticket number from getIssues with a jquery, the I am not able to add a comment, it throws a 404 error. I'm using the same credentials for it all.

I've tried the following:
Stripping any excess space away, using the IssueID instead of Issue Key. 

When I print out the ticket information, I get the correct key in string format.

I have the correct permissions for this



Code: 

def add_comment(ticket_info_dict, jira_url, api_token, jira_username):
"""
Add a comment to a Jira ticket with documentation links based on the research type.

Args:
ticket_info_dict (dict): A dictionary containing ticket numbers as keys and research types as values.
jira_url (str): The URL of the Jira instance.
api_token (str): The API token for authentication with Jira.
jira_username (str): The username for authentication with Jira.

Returns:
bool: True if all comments were added successfully, False otherwise.
"""
# Iterate through the ticket_info_dict to process each ticket and its research type
for ticket_number, research_type in ticket_info_dict.items():
# Set up the API endpoint for adding comments to the ticket
print(ticket_number)
add_comment_endpoint = f"{jira_url}/rest/api/3/issue/{ticket_number}/comment"

# Encode credentials for authentication
credentials = f"{jira_username}:{api_token}"
base64_credentials = base64.b64encode(credentials.encode()).decode()

headers = {
"Authorization": f"Basic {base64_credentials}",
"Content-Type": "application/json",
}

# Check if the research type exists in the documentation dictionary
if research_type in documentation_dict:
# If the research type is found, include the corresponding documentation links in the comment
documentation_links = documentation_dict[research_type]

# Create the comment body including the comment_text and documentation links
comment_body = {
"body": {
"content": [
{
"type": "paragraph",
"content": [
{"text": "This documentation relates to this research type:\n", "type": "text"},
],
},
{
"type": "paragraph",
"content": [{"text": "Documentation Links:\n", "type": "text"}],
},
{
"type": "paragraph",
"content": [
{
"text": f"-{doc_text}\n",
"type": "text",
"marks": [{"type": "link", "attrs": {"href": doc_link}}],
}
for doc_text, doc_link in documentation_links.items()
],
},
],
"type": "doc",
"version": 1
},
"properties": [
{
"key": "sd.public.comment",
"value": {
"internal": "true"
}
}
],
"visibility": {
"type": "role",
"value": "Service Desk Team"
}
}

try:
# Make the POST request to add the comment
comment_response = requests.post(add_comment_endpoint, headers=headers, json=comment_body)

# Check if the request was successful (HTTP status code 201)
if comment_response.status_code == 201:
print(f"Comment added successfully to ticket {ticket_number}.")
else:
print(f"Failed to add comment to ticket {ticket_number}. Status code: {comment_response.status_code}, Response: {comment_response.text}")

except requests.exceptions.RequestException as e:
# Handle exceptions related to the HTTP request
print(f"Error: {e}")
return False

else:
# If the research type is not found in the documentation dictionary, add a generic comment
generic_comment = f"This ticket has a research type of '{research_type}' but no documentation links are available for this type."
print(generic_comment)

# Return True if all comments were added successfully
return True


Getting the ticket info with this function:

def pull_ticket_info(jql_query, jira_url, api_token, jira_username):
"""
Retrieve ticket information from Jira using a JQL query.

Args:
jql_query (str): The JQL query to search for tickets in Jira.
jira_url (str): The URL of the Jira instance.
api_token (str): The API token for authentication with Jira.
jira_username (str): The username for authentication with Jira.

Returns:
str: The ticket number of research tickets. Returns None if no ticket is found.
"""

ticket_info_dict = {} # Dictionary to store ticket number and research type

# Set up the API endpoint for JQL search
api_endpoint = f"{jira_url}/rest/api/3/search"

# Encode credentials for authentication
credentials = f"{jira_username}:{api_token}"
base64_credentials = base64.b64encode(credentials.encode()).decode()

# Set up the authentication headers with base64-encoded credentials
headers = {
"Authorization": f"Basic {base64_credentials}"
}

# Set up the JQL query parameters and specify the fields you want to retrieve
params = {
"jql": jql_query,
# "fields": "key, summary, customfield_13810, customfield_13427"
}

try:
# Make the GET request to perform the JQL search
jql_response = requests.get(api_endpoint, headers=headers, params=params)
api_response = jql_response.json()
# print(api_response)

if jql_response.status_code == 200:
for issue in api_response["issues"]:
ticket_id = issue["id"]
ticket_number = issue["key"]
research_type = issue["fields"].get("customfield_13427", {})
research_type_value = research_type.get("value", "N/A")

# Add ticket number and research type to the dictionary
# ticket_info_dict[ticket_number] = research_type_value
ticket_info_dict[ticket_number] = research_type_value


# Return the dictionary with ticket numbers and research types as key-value pairs
return ticket_info_dict

else:
# Handle errors if the JQL search request is unsuccessful
print(f"JQL Error: {jql_response.status_code}, {api_response.get('errorMessages', 'Unknown error')}")
return {}

except requests.exceptions.RequestException as e:
# Handle exceptions related to the HTTP request
print(f"Request Error: {e}")
return {}

 
Each time i'm getting this error:

Failed to add comment to ticket GCPS-9679. Status code: 404, Response: {"errorMessages":["Issue does not exist or you do not have permission to see it."],"errors":{}}


2 answers

1 accepted

1 vote
Answer accepted
Stephen Byrd August 4, 2023

What I ended up doing was using the python-jira library. I used the add_comment method from that library. 

Charlie Misonne
Community Champion
August 7, 2023

ok good to know!

0 votes
Charlie Misonne
Community Champion
August 4, 2023

I don't immediately see what could be wrong. Your request seems OK to me.

Have you tried performing the post request yourself with postman or curl without all the scriptring around it? Let's see if you get the 404 too.

Stephen Byrd August 4, 2023

I haven't. I'll go ahead and give it a try!

Like • Charlie Misonne likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events