Commit 3b1730a5 authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Added Webhook

parent 655fe0a0
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Log;
use Carbon;
class ServiceDesk extends Controller
{
private $accessToken = null;
private $apiUrl = "https://gitlab.metager.de/api/v4/projects";
public function webhook(Request $request)
{
$token = $request->header("X-Gitlab-Token HTTP");
if ($token !== env("gitlab_webhook_token", "")) {
Log::info("Webhook Action not taken. Token mismatch: " . $token);
return;
}
$this->accessToken = env("gitlab_access_token", "");
$issues = [];
$getParameter = [
"confidential" => "true",
"author_username" => "support-bot",
"state" => "closed"
];
// Gather all relevant issues
// Issue:
// - closed
// - from support-bot
// - confidential
// - updated after it was closed
$currentUrl = $this->apiUrl . "/2/issues?" . http_build_query($getParameter);
while (true) {
if ($currentUrl === null) {
break;
}
$response = file_get_contents($currentUrl, false, stream_context_create([
"http" => [
"header" => "Authorization: Bearer " . $this->accessToken . "\r\n"
]
]));
$currentUrl = null;
$response = \json_decode($response, true);
if ($response === null) {
break;
}
foreach ($response as $issue) {
$updatedAt = new Carbon($issue["updated_at"]);
$closedAt = new Carbon($issue["closed_at"]);
if ($updatedAt->isAfter($closedAt)) {
$issues[] = $issue;
}
}
// Check if there is a next page
foreach ($http_response_header as $header) {
if (stripos($header, "Link") === 0) {
if (preg_match("/<([^>]+)>; rel=\"next\"/", $header, $matches) && !empty($matches[1])) {
$currentUrl = $matches[1];
break;
}
$matches = null;
}
}
}
if (sizeof($issues) === 0) {
Log::info("Webhook: No closed and updated issues found");
}
foreach ($issues as $issue) {
// Reopen the issues
$getParameter = [
"state_event" => "reopen"
];
$url = $this->apiUrl . "/2/issues/" . $issue["iid"] . "?" . http_build_query($getParameter);
$response = file_get_contents($url, false, stream_context_create([
"http" => [
"method" => "PUT",
"header" => "Authorization: Bearer " . $this->accessToken . "\r\n"
]
]));
Log::info("reopened issue " . $issue["iid"]);
}
return response("");
}
}
......@@ -191,6 +191,7 @@ Route::group(
Route::post('queryregexp', 'AdminSpamController@queryregexp');
Route::post('deleteRegexp', 'AdminSpamController@deleteRegexp');
});
Route::get('service-desk', 'ServiceDesk@webhook');
});
Route::get('settings', function () {
......@@ -256,7 +257,7 @@ Route::group(
try {
$fh = fopen("https://gitlab.metager.de/open-source/app-en/-/raw/latest/app/release_manual/app-release_manual.apk", "r");
while (!feof($fh)) {
echo (fread($fh, 1024));
echo(fread($fh, 1024));
}
} catch (\Exception $e) {
abort(404);
......@@ -273,7 +274,7 @@ Route::group(
try {
$fh = fopen("https://gitlab.metager.de/open-source/metager-maps-android/raw/latest/app/release/app-release.apk?inline=false", "r");
while (!feof($fh)) {
echo (fread($fh, 1024));
echo(fread($fh, 1024));
}
} catch (\Exception $e) {
abort(404);
......@@ -302,4 +303,5 @@ Route::group(
return response($result, 200)
->header('Content-Type', RenderTextFormat::MIME_TYPE);
});
});
}
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment