-
-
Save dkmonaghan/c480432eefcf6bcd0027b1c1d6410e79 to your computer and use it in GitHub Desktop.
<?php | |
$json = json_decode(file_get_contents('php://input'), true); | |
if ($json['action'] !== 'created'){ die('Only create actions are alerted'); } | |
$error = $json['data']['issue']; | |
if ($error['project']['id'] == 1){ die('Ignore internal errors.'); } | |
if (empty($error['shortId'])){ die('No shortId passed'); } | |
// Set channel and username | |
$response['username'] = 'Sentry'; | |
$response['response_type'] = 'in_channel'; | |
$attachment = []; | |
$attachment['fallback'] = 'Error reported by Sentry: ' . $error['id']; | |
$attachment['author_name'] = strtoupper($error['project']['name']) . ' - ' . $error['shortId']; | |
$attachment['author_icon'] = 'ICON_HERE'; | |
$attachment['author_link'] = 'PATH_TO_YOUR_ORG_HERE' . $error['id']; | |
$attachment['text'] = $error['title'] . ' @ ' . $error['culprit']; | |
$fields = [ | |
'Function' => $error['metadata']['function'], | |
'File' => $error['metadata']['filename'] | |
]; | |
// Generate fields | |
foreach ($fields as $label => $value){ | |
$field = [ | |
'short' => true, | |
'title' => $label, | |
'value' => $value | |
]; | |
$attachment['fields'][] = $field; | |
} | |
$response['attachments'][] = $attachment; | |
$response = json_encode($response); | |
$ch = curl_init('YOUR_MATTERMOST_WEBHOOK'); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $response); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); | |
echo curl_exec($ch); |
Hi @pmsudhi - Line 12 needs to remain as in_channel
- this is a Mattermost identifier telling it you want the message to be posted 'in a channel'.
When you create your incoming webhook URL within Mattermost, this is where you define the channel for the webhook to post messages into.
Lines 17 and 18 are where you enable a quick hyperlink within the Mattermost message back to your Sentry dashboard. Your org URL might be something like:
https://sentry.example.com/organizations/something/
Hope this helps! My guess is the reason this isn't posting is because of your issue on line 12.
Thanks, @dkmonaghan for the headstart. Unfortunately still not able to make progress. If you can please go through the details below. I suspect, some basic mistake I may be making. When an issue is created in Sentry, if I go and check the integration dashboard I can see 2 entries there. first one return with success while the second one fails with internal error. Appreciate it if you can help me out with this
My Webhook script looks likes as below
- My Script is accessible from outside
- My Mattermost incoming webhook URL is OK and tested by providing same to my GitLab instance and it is able to successfully able to communicate with matter most
My Sentry Integration looks like this
My Alert Configuration in Sentry is as below
If the server is returning a 500 error that means there's a problem with the script. Can you provide log output from your webserver showing PHP error logs?
Thanks @dkmonaghan . php curl was missing in the system hence the issue. this is resolved now. Thanks once again for the nice neat solution for Sentry - Mattermost integration
@dkmonaghan Thanks for the idea and the inspiration! Based on this, I created a Sentry - Google Chat webhook connection: https://gist.github.com/Benjaminhu/90471b8f4fc51305a8212223996c4994
I was not able to get it running successfully on my machine. can you help me on this. i have made following things
Are the line numbers 17 and 18 are compulsory? if yes, can you please explain what is to put at PATH_TO_YOUR_ORG_HERE?
In my sentry i can see that event alert triggered to this script hosted on my server with 200 status. so i assume that sentry was successfully able to call this script. but iam not able to see the message posted in my mattermost channel