Created
February 1, 2018 09:44
-
-
Save Lachee/2de4343b117630dda6eb050324161a4b to your computer and use it in GitHub Desktop.
GitLab to Discord Webhook
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 | |
error_reporting(E_ALL | E_STRICT); | |
ini_set('display_errors', 'On'); | |
DEFINE("COMMIT_BASED_COLOURS", True); | |
$json = file_get_contents('php://input'); | |
$action = json_decode($json, true); | |
//Create the hook | |
$webhook = array ( | |
'content' => "A push has been made to the project [{$action['project']['name']}]({$action['project']['web_url']}) on the branch `{$action['ref']}`", | |
'embeds' => array() | |
); | |
//Create the embeds | |
foreach ($action['commits'] as $commit) | |
$webhook['embeds'][] = CreateEmbed($commit); | |
//Prepare the webhook | |
$DiscordWebHook = "https://ptb.discordapp.com/api/webhooks/" . $_SERVER['HTTP_X_GITLAB_TOKEN']; | |
//Send the hook away | |
$payload = json_encode($webhook); | |
file_put_contents("payload.json", $payload); | |
$ch = curl_init(); | |
$options = array( | |
CURLOPT_URL => $DiscordWebHook, | |
CURLOPT_POST => true, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POSTFIELDS => $payload, | |
CURLOPT_HTTPHEADER => array('Content-type: application/json'), | |
); | |
curl_setopt_array($ch, $options); | |
$result = curl_exec($ch); | |
$httpCodeReturned = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
$result_str = json_decode($result); | |
/* | |
* print_r($result_str); | |
* print($payload); | |
* */ | |
print($httpCodeReturned); | |
die(); | |
/* | |
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", | |
"message": "Update Catalan translation to e38cb41.", | |
"timestamp": "2011-12-12T14:27:31+02:00", | |
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", | |
"author": { | |
"name": "Jordi Mallach", | |
"email": "[email protected]" | |
}, | |
"added": ["CHANGELOG"], | |
"modified": ["app/controller/application.rb"], | |
"removed": [] | |
}, | |
*/ | |
function CreateEmbed($commit) | |
{ | |
$commit_id = substr($commit['id'], 0, 7); | |
$avatar = md5(strtolower(trim($commid['author']['email']))); | |
$embed = array( | |
'description' => $commit['message'], | |
'url' => $commit['url'], | |
'color' => GetColour($commit), | |
'timestamp' => substr($commit['timestamp'], 0, -6), | |
'author' => array( | |
'name' => "Commit #{$commit_id}", | |
'url' => $commit['url'], | |
'icon_url' => "https://emojipedia-us.s3.amazonaws.com/thumbs/120/microsoft/106/fox-face_1f98a.png" | |
), | |
'footer' => array( | |
'icon_url' => "https://www.gravatar.com/avatar/" . md5(strtolower(trim($commid['author']['email']))), | |
'text' => $commit['author']['name'] | |
), | |
'fields' => array() | |
); | |
$added = CreateField($commit['added'], "Added"); | |
if ($added != null) $embed['fields'][] = $added; | |
$modified = CreateField($commit['modified'], "Modified"); | |
if ($modified != null) $embed['fields'][] = $modified; | |
$removed = CreateField($commit['removed'], "Removed"); | |
if ($removed != null) $embed['fields'][] = $removed; | |
return $embed; | |
} | |
function CreateField($files, $name) | |
{ | |
$count = count($files); | |
if ($count > 0) | |
{ | |
$value = ""; | |
if ($count > 5) | |
{ | |
$value = "+{$c} files"; | |
} | |
else | |
{ | |
foreach($files as $v) $value = $v . "\n"; | |
} | |
return array( | |
'name' => $name, | |
'value' => $value | |
); | |
} | |
return null; | |
} | |
function GetColour($commit) | |
{ | |
$colour_added = hexdec("#00FF89"); | |
$colour_modified = hexdec("#FFEE00"); | |
$colour_removed = hexdec("#E4271A"); | |
$colour_normal = hexdec("#E4271A"); | |
if (COMMIT_BASED_COLOURS) | |
{ | |
$added = count($commit['added']); | |
$removed = count($commit['removed']); | |
$modified = count($commit['modified']); | |
if ($added > $removed && $added > $modified) $colour_normal = $colour_added; | |
if ($removed > $added && $removed > $modified) $colour_normal = $colour_normal; | |
if ($modified > $added && $modified > $removed) $colour_normal = $colour_modified; | |
} | |
return $colour_normal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Know Issue is that new commits say "Tomorrow" and a hour late. Could just be a timezone thing or it is trimming a little bit too much of the timestamp from gitlab.