Create jira issue based on custom action
Asked by Oalimerko:

Dear all,

since 1 year i use the opsgenie and OEC for managing my alerts,OEC is running in a linux VM and it is used for creating jira issues with some custom fields.Untill now all jira tickets are created automatically based on some rules.However from now and later i want that a human decides if an alert is an indication for opening a ticket or not,that means that upon alert is triggered someone will check it and decide based on the impact of alert.For this reason i created a custom action "createIssue" and every time an alert is triggered every user has the possibility to create the issues on-demand.Steps which i did:

1. Create a custom action under Teams--\>Policies--\>Alert policies
2. Create a custom channel under Teams--\>Actions--\>Action channels
3. The new generated API Key i copied in my config.json file in opsgenie installation folder

Now every time an alert is triggered i click on "create issue" but unfortunately ticket is not created.It seems to be something wrong with 'alertId' in py script.Here the screenshot from alert:  
![alertId_error.png](https://community.atlassian.com/t5/image/serverpage/image-id/173126i09A0688065BB2CD2/image-size/large?v=v2&px=999 "alertId_error.png")  

I don't understand why the automatically ticket creation based on rules works and based on custom action does not?  

Here you can find the py script--\>[actionExecutor.py](https://nextcloud.gk-software.com/index.php/s/Q3Ds64QnFEq6otQ)   

BR  
Oltion Alimerko


Answers:
author: Ronnie Kingston
body: Hi Oltion,

Thanks for reaching out to us.

You mentioned that the OEC script executes without any issues when it's automatically called from the integration actions. But the script fails when it's called via the custom actions.

I'm assuming that you have created the custom action and it is linked to the respective action channel.

When the custom action is executed from a user-based trigger, the JSON payload sent from Opsgenie to the OEC process has a different structure. Due to this, when the OEC script attempts to parse the queue payload, it fails. This is why you are getting that error shown in your screenshot.

I am mentioning the link to the OEC script that is getting executed so that I can explain it further. <https://github.com/opsgenie/oec-scripts/blob/master/Jira/scripts/actionExecutor.py>

To view the payload, you can attempt to dump the contents of the "queue_message" variable into the logs and examine the JSON format. For example, you can add a line as below just after line 74 in the script that I linked above.  

    print(json.dumps(queue_message, indent=4, sort_keys=True))

For your reference, I'm also mentioning the format in which the payload is sent from Opsgenie to OEC when a custom action is triggered.

```
{
"customerId": "0d03b9ff-bfab-4cb8-a008-8b6156bbb775",
"action": "createIssue",
"source": {
"userId": "4b39816a-7427-44bd-beac-2d913b008850",
"userName": "my_email@atlassian.com",
"type": "user"
},
"ownerId": "b5c7f639-c0e6-4a69-bf60-6ec6a75f826e",
"params": {
"customerId": "0d03b9ff-bfab-4cb8-a008-8b6156bbb775",
"action": "createIssue",
"source": {
"userId": "4b39816a-7427-44bd-beac-2d913b008850",
"userName": "my_email@atlassian.com",
"type": "user"
},
"ownerId": "b5c7f639-c0e6-4a69-bf60-6ec6a75f826e",
"param1": "my_test",
"entity": {
"count": "1",
"description": "my test description",
"extraProperties": {},
"source": "my_email@atlassian.com",
"message": "test again",
"priority": "P3",
"type": "alert",
"tags": [],
"tinyId": "818",
"alias": "de96c18d-1637-4ba8-8135-96c54660a2ce-1581592282889",
"id": "de96c18d-1637-4ba8-8135-96c54660a2ce-1581592282889",
"actions": [
"createIssue"
],
"entity": "",
"status": "open"
}
},
"param1": "my_test",
"entity": {
"count": "1",
"description": "my test description",
"extraProperties": {},
"source": "my_email@atlassian.com",
"message": "test again",
"priority": "P3",
"type": "alert",
"tags": [],
"tinyId": "818",
"alias": "de96c18d-1637-4ba8-8135-96c54660a2ce-1581592282889",
"id": "de96c18d-1637-4ba8-8135-96c54660a2ce-1581592282889",
"actions": [
"createIssue"
],
"entity": "",
"status": "open"
}
}
```

Since the script is failing in getting the alertID, you may want to replace the line 76 with

alert_id = queue_message\["params"\]\["entity"\]\["id"\]

Also replace the line 77 with

mapped_action = queue_message\["action"\]

I am mentioning this based on the above payload that I received while testing a manually created alert.

Please reach out to us if you need any further assistance.

Regards,  
Ronnie

comments: author: Oalimerko
body: Hello Ronnie,  

thanks for your support.Exactly the payload was the key because the custom action use a different payload than regular action.  


Tags: cloud

python