diff --git a/bootstrap.php b/bootstrap.php index 9fc98f4..3463f8e 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -83,6 +83,97 @@ if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) { 'build' => $build ]; }, + 'fetchLogs' => function ($build) { + $settings = $this->app->storage->findOne('drone/settings') ?? FALSE; + + if (!$settings || !isset($settings['url'], + $settings['owner'], + $settings['project'], + $settings['target'], + $settings['token']) || !$settings['active']) { + return []; + } + + $url = trim($settings['url'], "/") . '/api/repos/' . $settings['owner'] . '/' . $settings['project'] . '/builds/'. $build; + + $headers = [ + 'Content-Type: application/json', + 'Accept: application/json', + 'Authorization: Bearer ' . $settings['token'] + ]; + + $logstr = ""; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + $b = curl_exec($ch); + curl_close($ch); + + $b = json_decode($b); + + $colors = [ + '/\\033?\[(?:0;)?30m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[(?:0;)?31m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[(?:0;)?32m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[(?:0;)?33m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[(?:0;)?34m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[(?:0;)?35m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[(?:0;)?36m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[(?:0;)?37m(.*?)\\033?\[0m/s' => '$1', + + '/\\033?\[1;30m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[1;31m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[1;32m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[1;33m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[1;34m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[1;35m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[1;36m(.*?)\\033?\[0m/s' => '$1', + '/\\033?\[1;37m(.*?)\\033?\[0m/s' => '$1', + + '/\\033?\[0m/s' => '', + ]; + + foreach ($b->stages as $stage) { + foreach ($stage->steps as $step) { + + $logstr .= '