From ecb1fe0c69be0a1f7484a8f39ae567d5f9007d0e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 30 Jan 2016 21:21:40 +0100 Subject: [PATCH] init --- .gitignore | 3 +++ README.md | 26 +++++++++++++++++++ accounts.php-dist | 6 +++++ event.php | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 accounts.php-dist create mode 100644 event.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb8e05b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +accounts.php +ctags +*.swp diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e9dc2c --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +Webhook API +=========== + +Gogs webhook api system writed for [gitnet] project. + +Installation +------------ + +### Requirements + +* PHP >= 5.4 + +### With GIT + +$ git clone https://gitnet.fr/deblan/webhook-api.git + +### Without GIT + +$ + +### Access keys + +$ cp accounts.php-dist accounts.php +$ ... Add API keys in accounts.php ... + + diff --git a/accounts.php-dist b/accounts.php-dist new file mode 100644 index 0000000..375af9b --- /dev/null +++ b/accounts.php-dist @@ -0,0 +1,6 @@ + 'Invalid secret', + ]); +} + +function badRequest(array $content = []) { + httpCode(400); + response($content); +} + +function requiredBagVar($bag, $index) { + if (!array_key_exists($index, $bag)) { + badRequest([ + 'error' => 'Bad request', + 'missing_param' => [$index, $bag], + ]); + } +} + +function isValidSecret($secret, $accounts) { + return in_array( + $secret, + $accounts + ); +} + +requiredBagVar($_POST, 'payload'); +$payload = json_decode($_POST['payload'], true); + +requiredBagVar($payload, 'secret'); + +$accounts = require 'accounts.php'; + +if (!isValidSecret($payload['secret'], $accounts)) { + badSecret(); +} + +$repository = preg_replace('/-project$/', '', $payload['repository']['name']); +$repository = str_replace(['/', '.'], '', $repository); + +$command = 'sudo -u git $HOME/bin/webhook-project '.escapeshellarg($repository); +$execute = shell_exec($command); + +response([ + 'data' => $repository, + 'exec' => $command, +]); +