Skip to content

Instantly share code, notes, and snippets.

@jsor
Created March 2, 2012 10:59
Show Gist options
  • Save jsor/1957719 to your computer and use it in GitHub Desktop.
Save jsor/1957719 to your computer and use it in GitHub Desktop.
TravisCI/HipChat Webhook
notifications:
webhooks: http://example.com/travisci-hipchat-webhook.php
<?php
$travisToken = '<token>'; // Get from http://travis-ci.org/profile
$hipchatParams = array(
'auth_token' => '<auth_token>', // Create at https://<your-subdomain>.hipchat.com/admin/api
'room_id' => '<room_name>',
'notify' => 1,
'from' => 'TravisCI'
);
$hipchatEndpoint = 'http://api.hipchat.com/v1/rooms/message';
function secure_compare($a, $b)
{
if (strlen($a) !== strlen($b)) {
return false;
}
$result = 0;
for ($i = 0; $i < strlen($a); $i++) {
$result |= ord($a[$i]) ^ ord($b[$i]);
}
return $result == 0;
}
if ('POST' === $_SERVER['REQUEST_METHOD']) {
if (!isset($_POST['payload'])) {
header('HTTP/1.1 400 Bad Request');
exit;
}
$payload = json_decode($_POST['payload'], true);
if (!isset($payload['repository']) || !isset($payload['repository']['owner_name']) || !isset($payload['repository']['name'])) {
header('HTTP/1.1 400 Bad Request');
exit;
}
$headers = apache_request_headers();
if (!isset($headers['Authorization'])) {
header('HTTP/1.1 401 Unauthorized');
exit;
}
$repoSlug = $payload['repository']['owner_name'] . '/' . $payload['repository']['name'];
$authorization = hash('sha256', $repoSlug . $travisToken);
if (!secure_compare($headers['Authorization'], $authorization)) {
header('HTTP/1.1 401 Unauthorized');
exit;
}
$baseUrl = 'http://travis-ci.org/' . $repoSlug;
$statusMsg = 1 === $payload['status'] ? '<span style="color:red">failed</span>' : '<span style="color:green">passed</span>';
$message = 'Build <b><a href="' . $baseUrl . '/builds/' . $payload['id'] . '">#' . $payload['number'] . '</a></b> of <a href="' . $baseUrl . '">' . $repoSlug . '</a> has <b>' . $statusMsg . '</b>.<br/>' .
'&nbsp;- Commit: <a href="http://github.com/' . $repoSlug . '/commit/' . $payload['commit'] . '">' . substr($payload['commit'], 0, 7) . ' (' . $payload['branch'] . ')</a><br/>' .
'&nbsp;- Compare: <a href="' . str_replace(' ', '', $payload['compare_url']) . '">' . basename($payload['compare_url']) . '</a><br/>' .
'&nbsp;- Author: <a href="http://github.com/' . $payload['author_name'] . '">' . $payload['author_name'] . '</a><br/>' .
'&nbsp;- Committer: <a href="http://github.com/' . $payload['committer_name'] . '">' . $payload['committer_name'] . '</a><br/>';
$params = $hipchatParams;
$params['format'] = 'json';
$params['message'] = $message;
$url = $hipchatEndpoint . '?' . http_build_query($params, '', '&');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
$code = (integer) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$json = json_decode($response, true);
// do something with $json
} else {
header('HTTP/1.1 405 Method Not Allowed');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment