179 lines
5.1 KiB
PHP
179 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Supporto;
|
|
|
|
use BackedEnum;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Process;
|
|
use Throwable;
|
|
use UnitEnum;
|
|
|
|
class Modifiche extends Page
|
|
{
|
|
protected static ?string $navigationLabel = 'Modifiche';
|
|
|
|
protected static ?string $title = 'Modifiche e Changelog';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-document-text';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Supporto';
|
|
|
|
protected static ?int $navigationSort = 30;
|
|
|
|
protected static ?string $slug = 'supporto/modifiche';
|
|
|
|
protected string $view = 'filament.pages.supporto.modifiche';
|
|
|
|
public string $currentVersion = '0.0.0';
|
|
|
|
public string $versionSource = 'config';
|
|
|
|
public string $updateChannel = 'free';
|
|
|
|
public bool $updateForce = false;
|
|
|
|
public bool $updateDryRun = false;
|
|
|
|
public ?int $lastUpdateExitCode = null;
|
|
|
|
public ?string $lastUpdateOutput = null;
|
|
|
|
public ?string $lastUpdateAt = null;
|
|
|
|
/** @var array<int,array{hash:string,short_hash:string,author:string,date:string,message:string}> */
|
|
public array $latestCommits = [];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->reloadDashboardData();
|
|
}
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user) {
|
|
return false;
|
|
}
|
|
|
|
// Pagina informativa: accessibile a chi accede al pannello.
|
|
return true;
|
|
}
|
|
|
|
public function canRunUpdate(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user && method_exists($user, 'hasAnyRole')
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore']);
|
|
}
|
|
|
|
public function reloadDashboardData(): void
|
|
{
|
|
$this->loadVersionInfo();
|
|
$this->loadLatestCommits();
|
|
}
|
|
|
|
public function runUpdate(): void
|
|
{
|
|
if (! $this->canRunUpdate()) {
|
|
Notification::make()
|
|
->title('Permessi insufficienti')
|
|
->body('Solo super-admin/admin/amministratore possono lanciare gli aggiornamenti.')
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
$params = [
|
|
'--channel' => $this->updateChannel,
|
|
'--apply' => true,
|
|
];
|
|
|
|
if ($this->updateDryRun) {
|
|
$params['--dry-run'] = true;
|
|
}
|
|
|
|
if ($this->updateForce) {
|
|
$params['--force'] = true;
|
|
}
|
|
|
|
try {
|
|
$exitCode = Artisan::call('netgescon:update', $params);
|
|
$output = trim((string) Artisan::output());
|
|
|
|
$this->lastUpdateExitCode = $exitCode;
|
|
$this->lastUpdateOutput = $output;
|
|
$this->lastUpdateAt = now()->format('d/m/Y H:i:s');
|
|
|
|
$this->reloadDashboardData();
|
|
|
|
Notification::make()
|
|
->title($exitCode === 0 ? 'Aggiornamento completato' : 'Aggiornamento terminato con errori')
|
|
->body($exitCode === 0 ? 'Procedura conclusa. Versione e commit aggiornati.' : 'Controlla il log tecnico nella sezione output.')
|
|
->{$exitCode === 0 ? 'success' : 'danger'}()
|
|
->send();
|
|
} catch (Throwable $e) {
|
|
$this->lastUpdateExitCode = 1;
|
|
$this->lastUpdateAt = now()->format('d/m/Y H:i:s');
|
|
$this->lastUpdateOutput = 'Errore durante esecuzione update: ' . $e->getMessage();
|
|
|
|
Notification::make()
|
|
->title('Errore durante update')
|
|
->body($e->getMessage())
|
|
->danger()
|
|
->send();
|
|
}
|
|
}
|
|
|
|
private function loadVersionInfo(): void
|
|
{
|
|
$versionFile = base_path('VERSION');
|
|
if (File::exists($versionFile)) {
|
|
$fileVersion = trim((string) File::get($versionFile));
|
|
if ($fileVersion !== '') {
|
|
$this->currentVersion = $fileVersion;
|
|
$this->versionSource = 'VERSION';
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
$this->currentVersion = (string) config('netgescon.version', '0.0.0');
|
|
$this->versionSource = 'config';
|
|
}
|
|
|
|
private function loadLatestCommits(): void
|
|
{
|
|
$this->latestCommits = [];
|
|
|
|
$format = '%H|%h|%an|%ad|%s';
|
|
$command = 'git log -n 20 --date=format:"%d/%m/%Y %H:%M" --pretty=format:"' . $format . '"';
|
|
$result = Process::path(base_path())->run($command);
|
|
|
|
if (! $result->successful()) {
|
|
return;
|
|
}
|
|
|
|
$lines = preg_split('/\r\n|\r|\n/', trim($result->output())) ?: [];
|
|
foreach ($lines as $line) {
|
|
$parts = explode('|', $line, 5);
|
|
if (count($parts) !== 5) {
|
|
continue;
|
|
}
|
|
|
|
$this->latestCommits[] = [
|
|
'hash' => (string) $parts[0],
|
|
'short_hash' => (string) $parts[1],
|
|
'author' => (string) $parts[2],
|
|
'date' => (string) $parts[3],
|
|
'message' => (string) $parts[4],
|
|
];
|
|
}
|
|
}
|
|
}
|