gitea_webhooks/www/pr.php

73 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2021-01-12 21:53:49 +00:00
<?php
2021-01-12 23:19:11 +00:00
include('config.php');
2021-01-12 21:53:49 +00:00
// check for POST request
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
header("HTTP/1.1 405 Method Not Allowed");
error_log('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']);
exit();
2021-01-12 21:53:49 +00:00
}
// get content type
$content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
if ($content_type != 'application/json') {
header("HTTP/1.1 403 Forbidden");
error_log('FAILED - not application/json - '. $content_type);
exit();
2021-01-12 21:53:49 +00:00
}
// get payload
$payload = trim(file_get_contents("php://input"));
if (empty($payload)) {
header("HTTP/1.1 403 Forbidden");
error_log('FAILED - no payload');
exit();
2021-01-12 21:53:49 +00:00
}
// convert json to array
$decoded = json_decode($payload, true);
// check for json decode errors
if (json_last_error() !== JSON_ERROR_NONE) {
header("HTTP/1.1 403 Forbidden");
error_log('FAILED - json decode - '. json_last_error());
exit();
2021-01-12 21:53:49 +00:00
}
if($decoded['secret'] != $secret_key['pr']) {
header("HTTP/1.1 403 Forbidden");
echo("WRONG SECRET");
error_log('FAILED - wrong secret key');
exit();
2021-01-12 21:53:49 +00:00
}
2022-10-31 20:52:02 +00:00
//file_put_contents("log.txt", print_r($decoded, true));
2021-01-12 21:53:49 +00:00
2021-01-13 19:25:34 +00:00
$subject = $decoded['repository']['full_name']
. " - PR #"
. $decoded['pull_request']['number']
. " ("
. $decoded['pull_request']['title']
. ") by "
. $decoded['pull_request']['user']['login'];
2021-01-12 21:53:49 +00:00
$message = $decoded['sender']['login']
. " has " .
($decoded['pull_request']['merged'] ? "merged" : $decoded['action'])
2021-01-13 19:29:31 +00:00
. " the pull request #" .
2021-01-12 21:53:49 +00:00
$decoded['pull_request']['number']
2021-01-13 19:25:34 +00:00
. " (" . $decoded['pull_request']['title'] . ") for " .
2021-01-12 21:53:49 +00:00
$decoded['repository']['full_name']
. ".\n" .
2021-01-13 19:25:34 +00:00
$decoded['pull_request']['html_url']
2021-01-13 19:29:31 +00:00
. "\n\n---\n\nSent to you by gitea_webhooks (https://git.chaospott.de/Chaospott/gitea_webhooks)";
2021-01-12 21:53:49 +00:00
2021-01-12 23:08:44 +00:00
foreach($recipients['pr'] as $recp) {
2021-01-12 23:19:11 +00:00
mail($recp, $subject, $message, $from);
2021-01-12 23:08:44 +00:00
}
2021-01-12 23:27:53 +00:00
echo("SENT");
2021-01-12 21:53:49 +00:00
?>