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 | |
/* | |
GitLab to (HipChat and DeployHQ) | |
Set GitLab commit web hook to http://server/script.php?url={deployhq_deploy_url} | |
Uses https://github.com/hipchat/hipchat-php | |
*/ | |
require_once('hipchat.php'); | |
$branch_to_test = "staging"; | |
$hipchat_token = ""; | |
$hipchat = new HipChat\HipChat($hipchat_token); | |
$url = $_GET['url']; | |
$git = file_get_contents("php://input"); | |
$git = json_decode($git); | |
if(isset($git->commits) && !empty($git->commits)) | |
{ | |
$branch = explode("/", $git->ref); | |
$branch = $branch[count($branch) - 1]; | |
if($branch == $branch_to_test) | |
{ | |
foreach ($git->commits as $commit) { | |
$commit_auth = $commit->author->name; | |
$commit_msg = $commit->message; | |
$commit_url = $commit->url; | |
$commit_msg = trim(preg_replace('/\s\s+/', ' ', $commit_msg)); | |
$message = "New commit: by " . (string)$commit_auth . " " . "<a href=" . $commit_url . ">" . $commit_msg . "</a>"; | |
file_put_contents('log', $message); | |
$hipchat->message_room('Coders', 'GitLab', $message, true); | |
} | |
$jsonData = array( | |
"new_ref" => $git->after, | |
"branch" => $branch, | |
"email" => (string)$git->commits[0]->author->email, | |
"clone_url" => $git->repository->url | |
); | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "payload=" . urlencode(json_encode($jsonData))); | |
$result = curl_exec($ch); | |
if ($result == " ") { | |
// Ok | |
} else { | |
error_log($result); | |
} | |
} | |
else | |
{ | |
foreach ($git->commits as $commit) { | |
$commit_auth = $commit->author->name; | |
$commit_msg = $commit->message; | |
$commit_url = $commit->url; | |
$commit_msg = trim(preg_replace('/\s\s+/', ' ', $commit_msg)); | |
$message = "New commit: by " . (string)$commit_auth . " " . "<a href=" . $commit_url . ">" . $commit_msg . "</a>"; | |
file_put_contents('log', $message); | |
$hipchat->message_room('Coders', 'GitLab', $message, true); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment