This commit is contained in:
Simon Vieille 2016-01-30 21:21:40 +01:00
commit ecb1fe0c69
4 changed files with 100 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
accounts.php
ctags
*.swp

26
README.md Normal file
View File

@ -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 ...

6
accounts.php-dist Normal file
View File

@ -0,0 +1,6 @@
<?php
return [
'tdGeYjvAZPqb8bikwIlTRaHMVumfjitG6cXyGC9meOZ2zthmyTwxSoAtW6WABTv4,
'V8U6WwmbyWCPH0vdFX4MZYvUbVlwWCctij6X7FIAHoEXOHiTXmVXllMiyHF90RcO',
];

65
event.php Normal file
View File

@ -0,0 +1,65 @@
<?php
function httpCode($code) {
http_response_code($code);
headers_sent();
}
function response(array $content, $code = 200) {
httpCode($code);
header('Content-type: application/json');
echo json_encode($content);
exit;
}
function badSecret() {
httpCode(403);
response([
'error' => '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,
]);