Created
June 20, 2016 22:12
-
-
Save andyfoster/d482963daff5b0c103876069040bcfef to your computer and use it in GitHub Desktop.
This file contains 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 | |
// Check this request has a GitLab header | |
function checkHeader($gitlab_header) { | |
if (!isset($gitlab_header)) { | |
throw new Exception("Can't be accessed directly."); | |
} | |
} | |
try { checkHeader($_SERVER['HTTP_X_GITLAB_EVENT']); } | |
catch(Exception $e) { | |
echo $e->getMessage(); | |
exit; | |
} | |
// Get the webhook integration token from the URL or just use the default WebHook Testing room | |
$glip_token = isset($_GET['glip-channel-token']) ? $_GET['glip-channel-token'] : '001010-your-deafult-room-token-001010'; | |
// Generic class to post to any Glip channel from a PHP script | |
class GlipPost { | |
public function post($webhook_url, $icon_url, $activity, $message){ | |
$glip_activity = array( | |
'icon' => $icon_url, | |
'activity' => $activity, | |
'body' => $message, | |
); | |
$json_message = json_encode($glip_activity); | |
//Send the JSON data to the right server | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $webhook_url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8")); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_message); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
echo "Script run."; | |
} | |
} | |
// Get the body of the request sent to this PHP script | |
$request_body = file_get_contents('php://input'); | |
$json = json_decode($request_body); | |
/* | |
// TODO: | |
// Make this more extensible so it can handle more events | |
switch ($json->object_kind) { | |
case "merge_request": | |
// code here | |
break; | |
case "push": | |
// code | |
break; | |
default: | |
// code here | |
} | |
*/ | |
if ($json->object_kind == "merge_request") { | |
$username = $json->user->name; | |
$avatar = $json->user->avatar_url; | |
$output = <<<EOT | |
New **merge request** on __**{$json->project->name}**__. | |
Source branch: **{$json->object_attributes->source_branch}** | |
Target branch: **{$json->object_attributes->target_branch}** | |
Last commit message: {$json->object_attributes->last_commit->message} | |
URL: {$json->object_attributes->url} | |
EOT; | |
} else { | |
// Non merge requests | |
$username = $json->user_name; | |
$avatar = $json->user_avatar; | |
// Creating a new branch | |
/* | |
if ($json->before = "0000000000000000000000000000000000000000") { | |
$output = <<<EOT | |
I just created a new branch on **{$json->project->name}** (*{$json->ref}*) | |
EOT; | |
} else { | |
*/ | |
$plural_suffix = $json->total_commits_count != 1 ? "s" : ""; | |
$branch = str_replace('refs/heads/', '', $json->ref); | |
$output = <<<EOT | |
I just {$json->object_kind}ed {$json->total_commits_count} commit{$plural_suffix} to branch **[{$branch}]({$json->project->homepage}/commits/{$branch})** at **[{$json->project->path_with_namespace}]({$json->project->homepage})**. | |
EOT; | |
$output .= "\n"; | |
for($i = 0; $i < $json->total_commits_count; $i++){ | |
$output .= "* Commit " . ($i + 1) . ": [" . substr($json->commits[$i]->id, 0, 8) . "](" . $json->commits[$i]->url . ") - " . trim($json->commits[$i]->message) . ".\n"; | |
} | |
// } | |
} | |
GlipPost::post( | |
'https://hooks.glip.com/webhook/' . $glip_token, | |
$avatar, | |
$username . " ( via GitLab )", | |
$output | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you guide how to use it?