init
This commit is contained in:
commit
ecb1fe0c69
4 changed files with 100 additions and 0 deletions
65
event.php
Normal file
65
event.php
Normal 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,
|
||||
]);
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue