From b06fb57ef74f5a7965adbcfa1a80b98bd17adf44 Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Sun, 25 Oct 2015 12:51:01 -0400 Subject: [PATCH] working deploy + sass compile test --- .gitignore | 2 +- controller/Actions.class.php | 32 ++++++++++++++++++++++---- lib/exception/ErrorException.class.php | 11 +++++++++ update.sh | 4 ++++ web/scss/_basic.scss | 4 ++-- web/scss/_content.scss | 7 ++---- 6 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 lib/exception/ErrorException.class.php create mode 100755 update.sh diff --git a/.gitignore b/.gitignore index e9f20eb4..17cdce53 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/web/.sass-cache +.sass-cache /web/css /log /data/secret diff --git a/controller/Actions.class.php b/controller/Actions.class.php index 1535d48f..07b49964 100644 --- a/controller/Actions.class.php +++ b/controller/Actions.class.php @@ -35,11 +35,21 @@ class Actions public function executePostCommit() { - $payload = json_decode($_REQUEST['payload']); - $secret = file_get_contents($_SERVER['ROOT_DIR'] . '/data/secret/github-secret'); - $isToMaster = $payload->ref === 'refs/heads/master'; + $payload = json_decode($_REQUEST['payload'], true); + $rawPost = file_get_contents('php://input'); + $secret = trim(file_get_contents($_SERVER['ROOT_DIR'] . '/data/secret/github-secret')); - file_put_contents($_SERVER['ROOT_DIR'] . '/log/github.txt', ($isToMaster ? 'master' : 'apprentince') . "\n$secret\n" . print_r($payload, TRUE) . print_r($_REQUEST, true)); + $this->returnErrorIf(!isset($_SERVER['HTTP_X_HUB_SIGNATURE']), "HTTP header 'X-Hub-Signature' is missing."); + + list($algo, $hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2) + array('', ''); + $this->returnErrorIf(!in_array($algo, hash_algos(), TRUE), 'Invalid hash algorithm "' . $algo . '"'); + $this->returnErrorIf($hash !== hash_hmac($algo, $rawPost, $secret), 'Hash does not match. "' . $secret . '"' . ' algo: ' . $algo . '$'); + + if ($payload['ref'] === 'refs/heads/master') + { + shell_exec($_SERVER['ROOT_DIR'] . '/deploy.sh > /dev/null 2>/dev/null &'); + echo "Successful post commit."; + } return [null, []]; } @@ -79,4 +89,18 @@ class Actions return false; } + + //this is dumb + protected function returnError($error) + { + throw new ErrorException($error); + } + + protected function returnErrorIf($condition, $error) + { + if ($condition) + { + $this->returnError($error); + } + } } \ No newline at end of file diff --git a/lib/exception/ErrorException.class.php b/lib/exception/ErrorException.class.php new file mode 100644 index 00000000..81e2d9d1 --- /dev/null +++ b/lib/exception/ErrorException.class.php @@ -0,0 +1,11 @@ +