Add Comment in JIRA when we get reply on AWS support Case

simi jain April 26, 2024

problem statement : When someone create case on AWS Support, Jira ticket should be created and when someone reply to the same case, JIRA comment should be added in same JIRA Ticket. 

My approach : JIRA ticket is creating when AWS Support case is getting created , but when I am replying to the case, I am fetching that message Support API using describe_cases method but unable to make api put request in JIRA to add comment in same JIRA Ticket

below is my code:

I am triggering my lambda using Eventbridge (contains the Eventpattern which fetch logs from Cloudtrail for events Create case and AddCommunicationToCase)

Create Case is working fine and  addcommunication is also working but in JIRA ticket comment is not being made 

error : Failed to add comments to Jira ticket 

Response 405

I want to know: how I can fetch issue key based on summary or case ID so that when my lambda will run 2nd time in the case of AddCommunicationToCase , it can add comment in same ticket of JIRA 

 

 

import json
import requests
import boto3
import base64

def lambda_handler(event, context):
# Extract relevant data from CloudWatch event
cloudtrail_data = event['detail']

# Process the CloudTrail data
print(cloudtrail_data)

support_client = boto3.client('support')
event_name = cloudtrail_data["eventName"]

if event_name == "CreateCase":

case_id = cloudtrail_data['responseElements']['caseId']
severity = cloudtrail_data['requestParameters']['severityCode']
issue_type = cloudtrail_data['requestParameters']['issueType']
description = cloudtrail_data['requestParameters'].get('description', 'Check CaseId')

# Create Jira issue payload
payload = {
"fields": {
"project": {
"key": "TEST" # Replace with your Jira project key
},
"summary": f"AWS Support Case:{case_id}",
"description": {
"content": [
{
"content": [
{
"text": f"{description} {severity} {issue_type}",
"type": "text"
}
],
"type": "paragraph"
}
],
"type": "doc",
"version": 1
},
"issuetype": {
"name": "Task"
}
}
}


# call Create Jira issue function
jira_url = "https://simijain650.atlassian.net/rest/api/3/issue"
jira_username = "<email of jira>"
jira_api_token = "<adding api token>"

"""
# Jira API request headers
headers = {
"Content-Type": "application/json",
"Authorization": "Basic " + base64.b64encode(f"{jira_username}:{jira_api_token}".encode()).decode()
}
"""

# Send request to create Jira issue
response = requests.post(jira_url, json=payload, auth=jira_auth)

# Check if request was successful
if response.status_code == 201:
print("Jira ticket created successfully")
issue_key = response.json()["key"] # fetching issue key
print(issue_key)
else:
print("Failed to create Jira ticket")
print(response.text)


elif event_name == "AddCommunicationToCase":
cloudtrail_data = event['detail']
caseId = cloudtrail_data['requestParameters']['caseId']
print("caseId", caseId)
# Retrieve communications/messages associated with the case


response = support_client.describe_cases(
caseIdList=[caseId]
)

print(response)


# Extract messages from the response
communications = response["cases"][0]["recentCommunications"]["communications"]
messages = [communication["body"] for communication in communications]

print(f"Case ID: {caseId}")

# Add comments to the Jira issue
add_comment_to_jira(caseId, messages)
else:
print("Event is not relevant for this Lambda function.")

return {
'statusCode': 200,
'body': json.dumps('Process completed')
}

def add_comment_to_jira(case_id, messages):
# Jira API endpoint
jira_url = "https://simijain650.atlassian.net/rest/api/3/issue"

# Jira authentication credentials
jira_username = "<email of jira>"
jira_api_token = "<adding api token>  "

# Create payload to add comments to the Jira issue
add_comment_payload = {
"update": {
"comment": [
{
"add": {
"body": "AWS Support Messages:\n" + "\n".join(messages)
}
}
]
}
}
print("add_comment_payload",add_comment_payload)

# Jira API request headers
headers = {
"Content-Type": "application/json",
"Authorization": "Basic " + base64.b64encode(f"{jira_username}:{jira_api_token}".encode()).decode()
}
print(headers)

# Send request to add comments to the Jira issue
response = requests.put(f"{jira_url}?case_id={case_id}", headers=headers,json = add_comment_payload )
#response = requests.post(jira_url, headers=headers, json=add_comment_payload)
print("Status of Put method: ",response.status_code)
print("response",response)
# Check if request was successful
if response.status_code == 200:
print("Comments added to Jira ticket successfully")
else:
print("Failed to add comments to Jira ticket")
print(response.text)

 

 

 

1 answer

0 votes
Steffen Opel _Utoolity_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2024

Hi @simi jain ´, and welcome to the Atlassian Community!

Without checking into you code, have you considered the readily available AWS Service Management Connector for JSM app that is published and supported by AWS?

While it's not mentioned on the Marketplace listing, the app gained a related feature in version 5.0.0 a while ago already:

AWS Support integration

  • Configure dual synchronization of AWS Support cases as Jira issues
  • View, create, resolve, and add correspondences to AWS Support tickets directly from Jira issues

Note that I haven't tried that yet, but given you can add correspondences to AWS Support tickets directly from Jira issues, I'd expect you'll receive replies as well :)

You can learn more in Integrating AWS Support in Jira Service Management Cloud.

Good luck,
Steffen

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events