Created
October 28, 2020 17:49
-
-
Save dkmonaghan/c480432eefcf6bcd0027b1c1d6410e79 to your computer and use it in GitHub Desktop.
This script can act as a middleman between Sentry's webhook system and Mattermost's Incoming Webhooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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