Skip to content

Instantly share code, notes, and snippets.

@gomasy
Last active August 26, 2016 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gomasy/eb28b5a0a4efb4bc707e to your computer and use it in GitHub Desktop.
Save gomasy/eb28b5a0a4efb4bc707e to your computer and use it in GitHub Desktop.
github webhook を利用した、push すると自動で git pull する感じのアレ
<?php
header('Content-Type: application/json');
$secret = 'hogehoge';
$repo = 'hoge';
function generate_response($code) {
$response = array(
'200' => array(
'status' => 'success',
'errors' => null
),
'400' => array(
'status' => 'fail',
'errors' => array(
'code' => '400',
'message' => 'Invalid request.'
)),
'401' => array(
'status' => 'fail',
'errors' => array(
'code' => '401',
'message' => 'Could not authenticate.'
))
);
return json_encode($response[$code]);
}
if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
header('HTTP/1.1 400 Bad Request');
print generate_response(400);
die();
}
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$signature = substr($_SERVER['HTTP_X_HUB_SIGNATURE'], 5);
$hash = hash_hmac('sha1', $json_str, $secret);
if (($hash !== $signature) || ($json_obj->repository->name !== $repo)) {
header('HTTP/1.1 401 Unauthorized');
print generate_response(401);
die();
} else {
`cd ./repository; git pull`;
print generate_response(200);
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment