Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions config/filament-deploy-indicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@
'local' => ['label' => 'LOCAL', 'color' => 'gray'],
],

/*
|--------------------------------------------------------------------------
| Deploy history
|--------------------------------------------------------------------------
|
| Append-only JSONL log of past deploys. Each new deploy is recorded
| (deduplicated by commit hash). Useful to see who/what/when in the
| Filament topbar without leaving the admin.
|
*/
'history' => [
'enabled' => true,
'path' => storage_path('app/private/deploy-history.jsonl'),
'max_entries' => 100,
'show_in_dropdown' => 5,
],

/*
|--------------------------------------------------------------------------
| Topbar hint
Expand Down
1 change: 1 addition & 0 deletions resources/lang/bg/deploy-indicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
'click_to_view' => 'Кликни, за да видиш информация за деплой',
'copy' => 'Копирай commit хеш',
'copied' => 'Копирано!',
'recent_deploys' => 'Последни деплойменти',
];
1 change: 1 addition & 0 deletions resources/lang/en/deploy-indicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
'click_to_view' => 'Click to view deployment info',
'copy' => 'Copy commit hash',
'copied' => 'Copied!',
'recent_deploys' => 'Recent deploys',
];
29 changes: 29 additions & 0 deletions resources/views/indicator.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,34 @@ class="mt-0.5 flex-shrink-0 rounded p-1 text-gray-400 transition-colors hover:bg
</x-filament::dropdown.list.item>
@endif

@if (!empty($history))
<x-filament::dropdown.list.item>
<div class="text-xs font-semibold uppercase tracking-wide text-gray-500">
{{ __('filament-deploy-indicator::deploy-indicator.recent_deploys') }}
</div>
<div class="mt-2 space-y-1.5">
@foreach ($history as $entry)
@php
$entryCommit = data_get($entry, 'commit');
$entryShort = $entryCommit ? \Illuminate\Support\Str::limit($entryCommit, 7, '') : null;
$entryAuthor = data_get($entry, 'author');
$entryDate = data_get($entry, 'deployed_at') ?? data_get($entry, 'recorded_at');
@endphp
<div class="flex items-baseline gap-2 text-xs">
@if ($entryShort)
<span class="font-mono text-gray-700 dark:text-gray-300">{{ $entryShort }}</span>
@endif
@if ($entryAuthor)
<span class="truncate text-gray-500">{{ $entryAuthor }}</span>
@endif
@if ($entryDate)
<span class="ml-auto whitespace-nowrap text-gray-400">{{ $entryDate }}</span>
@endif
</div>
@endforeach
</div>
</x-filament::dropdown.list.item>
@endif

</x-filament::dropdown.list>
</x-filament::dropdown>
9 changes: 7 additions & 2 deletions src/Commands/WriteDeployInfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Arnautdev\FilamentDeployIndicator\Commands;

use Arnautdev\FilamentDeployIndicator\Services\DeployHistoryService;
use Arnautdev\FilamentDeployIndicator\Services\GitDeployInfoGenerator;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
Expand All @@ -20,8 +21,10 @@ class WriteDeployInfoCommand extends Command

protected $description = 'Write deploy-info.json for Filament Deploy Indicator';

public function __construct(protected GitDeployInfoGenerator $generator)
{
public function __construct(
protected GitDeployInfoGenerator $generator,
protected DeployHistoryService $history,
) {
parent::__construct();
}

Expand Down Expand Up @@ -52,6 +55,8 @@ public function handle(): int
File::ensureDirectoryExists(dirname((string) $path));
File::put($path, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

$this->history->record($data);

$this->info("Deploy info written to: {$path}");

return self::SUCCESS;
Expand Down
5 changes: 4 additions & 1 deletion src/FilamentDeployIndicatorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ function (): string {
return '';
}

$service = app(DeployInfoService::class);

return View::make('filament-deploy-indicator::indicator', [
'deploy' => app(DeployInfoService::class)->get(),
'deploy' => $service->get(),
'history' => $service->recentHistory(),
])->render();
}
);
Expand Down
131 changes: 131 additions & 0 deletions src/Services/DeployHistoryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Arnautdev\FilamentDeployIndicator\Services;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class DeployHistoryService
{
public function record(array $entry): void
{
if (! $this->enabled()) {
return;
}

$commit = $entry['commit'] ?? null;

if (! is_string($commit) || $commit === '') {
return;
}

$path = $this->path();

if ($path === '') {
return;
}

$maxEntries = max(1, (int) config('filament-deploy-indicator.history.max_entries', 100));

$existing = $this->readAll($path);

$lastCommit = $existing !== [] ? ($existing[array_key_last($existing)]['commit'] ?? null) : null;

if ($lastCommit === $commit) {
return;
}

$entry['recorded_at'] ??= now()->toDateTimeString();

$existing[] = $entry;

if (count($existing) > $maxEntries) {
$existing = array_slice($existing, -$maxEntries);
}

$this->writeAll($path, $existing);
}

public function recent(?int $limit = null): array
{
if (! $this->enabled()) {
return [];
}

$path = $this->path();

if ($path === '' || ! File::exists($path)) {
return [];
}

$limit ??= (int) config('filament-deploy-indicator.history.show_in_dropdown', 5);
$limit = max(0, $limit);

if ($limit === 0) {
return [];
}

$entries = $this->readAll($path);

return array_reverse(array_slice($entries, -$limit));
}

public function path(): string
{
return (string) config('filament-deploy-indicator.history.path', '');
}

public function enabled(): bool
{
return (bool) config('filament-deploy-indicator.history.enabled', true);
}

private function readAll(string $path): array
{
if (! File::exists($path)) {
return [];
}

$contents = File::get($path);

if ($contents === '') {
return [];
}

$entries = [];

foreach (preg_split("/\r\n|\n|\r/", $contents) ?: [] as $line) {
$line = trim($line);

if ($line === '') {
continue;
}

$decoded = json_decode($line, true);

if (is_array($decoded)) {
$entries[] = $decoded;
}
}

return $entries;
}

private function writeAll(string $path, array $entries): void
{
File::ensureDirectoryExists(dirname($path));

$lines = array_map(
static fn (array $entry): string => json_encode($entry, JSON_UNESCAPED_SLASHES) ?: '',
$entries,
);

$lines = array_filter($lines, static fn (string $line): bool => $line !== '');

try {
File::put($path, implode(PHP_EOL, $lines) . ($lines === [] ? '' : PHP_EOL));
} catch (\Throwable $e) {
Log::warning("filament-deploy-indicator: failed to write deploy history to [{$path}]: {$e->getMessage()}");
}
}
}
8 changes: 8 additions & 0 deletions src/Services/DeployInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DeployInfoService
{
public function __construct(
protected GitDeployInfoGenerator $generator,
protected DeployHistoryService $history,
) {}

public function get(): array
Expand Down Expand Up @@ -39,6 +40,8 @@ public function get(): array
json_encode($generated, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);

$this->history->record($generated);

$path = $writePath;
}

Expand All @@ -54,4 +57,9 @@ public function get(): array
return $data;
});
}

public function recentHistory(?int $limit = null): array
{
return $this->history->recent($limit);
}
}
104 changes: 104 additions & 0 deletions tests/Unit/DeployHistoryServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

use Arnautdev\FilamentDeployIndicator\Services\DeployHistoryService;
use Illuminate\Support\Facades\File;

beforeEach(function () {
$path = storage_path('app/private/deploy-history.jsonl');

if (File::exists($path)) {
File::delete($path);
}

config()->set('filament-deploy-indicator.history.enabled', true);
config()->set('filament-deploy-indicator.history.path', $path);
config()->set('filament-deploy-indicator.history.max_entries', 100);
config()->set('filament-deploy-indicator.history.show_in_dropdown', 5);
});

it('appends a deploy entry to the history file', function () {
$service = app(DeployHistoryService::class);

$service->record([
'commit' => 'abc123',
'author' => 'Dmitry',
'deployed_at' => '2026-04-27 10:00:00',
]);

$recent = $service->recent();

expect($recent)->toHaveCount(1)
->and($recent[0]['commit'])->toBe('abc123')
->and($recent[0]['author'])->toBe('Dmitry')
->and($recent[0]['recorded_at'])->not->toBeEmpty();
});

it('skips appending when commit hash matches the last entry', function () {
$service = app(DeployHistoryService::class);

$service->record(['commit' => 'abc123', 'author' => 'Dmitry']);
$service->record(['commit' => 'abc123', 'author' => 'Dmitry']);
$service->record(['commit' => 'abc123', 'author' => 'Other']);

expect($service->recent())->toHaveCount(1);
});

it('appends when commit hash differs from the last entry', function () {
$service = app(DeployHistoryService::class);

$service->record(['commit' => 'aaa']);
$service->record(['commit' => 'bbb']);
$service->record(['commit' => 'ccc']);

$recent = $service->recent();

expect($recent)->toHaveCount(3)
->and(array_column($recent, 'commit'))->toBe(['ccc', 'bbb', 'aaa']);
});

it('trims history to max_entries', function () {
config()->set('filament-deploy-indicator.history.max_entries', 3);

$service = app(DeployHistoryService::class);

foreach (['a', 'b', 'c', 'd', 'e'] as $commit) {
$service->record(['commit' => $commit]);
}

$recent = $service->recent(10);

expect($recent)->toHaveCount(3)
->and(array_column($recent, 'commit'))->toBe(['e', 'd', 'c']);
});

it('returns empty array when history is disabled', function () {
config()->set('filament-deploy-indicator.history.enabled', false);

$service = app(DeployHistoryService::class);

$service->record(['commit' => 'abc123']);

expect($service->recent())->toBe([]);
});

it('respects the show_in_dropdown limit', function () {
config()->set('filament-deploy-indicator.history.show_in_dropdown', 2);

$service = app(DeployHistoryService::class);

foreach (['a', 'b', 'c', 'd'] as $commit) {
$service->record(['commit' => $commit]);
}

expect($service->recent())->toHaveCount(2)
->and(array_column($service->recent(), 'commit'))->toBe(['d', 'c']);
});

it('ignores entries without a commit hash', function () {
$service = app(DeployHistoryService::class);

$service->record(['author' => 'Dmitry']);
$service->record(['commit' => '', 'author' => 'Dmitry']);

expect($service->recent())->toBe([]);
});
Loading