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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"require": {
"php": "^8.3",
"ext-simplexml": "*",
"cachethq/badger": "^5.0",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Obtain approval before adding Badger

This introduces cachethq/badger as a new runtime dependency, but the repository explicitly prohibits adding dependencies without prior approval, and the reviewed change provides no evidence of that approval. Obtain and document approval or use an existing approved dependency before merging.

AGENTS.md reference: AGENTS.md:L20-L24

Useful? React with 👍 / 👎.

"doctrine/dbal": "^3.6",
"filament/filament": "^5.0",
"filament/spatie-laravel-settings-plugin": "^5.0",
Expand Down
2 changes: 2 additions & 0 deletions src/CachetCoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cachet;

use BladeUI\Icons\Factory;
use Cachet\Badger\BadgerServiceProvider;
use Cachet\Commands\AssetsCommand;
use Cachet\Commands\CheckComponentsCommand;
use Cachet\Commands\MakeUserCommand;
Expand Down Expand Up @@ -64,6 +65,7 @@ public function register(): void
$this->app->singleton(Cachet::class);
$this->app->singleton(ViewManager::class);
$this->app->scoped(Status::class);
$this->app->register(BadgerServiceProvider::class);

$this->configureSettingsCache();
}
Expand Down
12 changes: 12 additions & 0 deletions src/Enums/ComponentStatusEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ public function getTextColorClasses(): string
};
}

public function getBadgeColor(): string
{
return match ($this) {
self::operational => 'brightgreen',
self::performance_issues => 'yellow',
self::partial_outage => 'orange',
self::major_outage => 'red',
self::under_maintenance => 'orange',
self::unknown => 'lightgray',
};
}

public function getColor(): array
{
return match ($this) {
Expand Down
10 changes: 10 additions & 0 deletions src/Enums/SystemStatusEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function getColor(): array
};
}

public function getBadgeColor(): string
{
return match ($this) {
self::operational => 'brightgreen',
self::partial_outage => 'orange',
self::major_outage => 'red',
self::under_maintenance => 'orange',
};
}

public function getIcon(): string
{
return match ($this) {
Expand Down
33 changes: 33 additions & 0 deletions src/Http/BadgeResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Cachet\Http;

use Cachet\Badger\BadgeImage;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class BadgeResponseFactory
{
/**
* Build a cacheable SVG response.
*/
public function make(BadgeImage $badge): Response
{
return response((string) $badge, 200, [
'Cache-Control' => 'public, max-age=60, s-maxage=60',
'Content-Type' => 'image/svg+xml; charset=UTF-8',
]);
}

/**
* Return the requested style, falling back to the default for unsupported values.
*/
public function style(Request $request): string
{
$style = $request->string('style', 'flat-square')->value();

return in_array($style, ['flat-square', 'plastic-flat', 'flat', 'plastic', 'social', 'svg'], true)
? $style
: 'flat-square';
}
}
32 changes: 32 additions & 0 deletions src/Http/Controllers/StatusPage/ComponentBadgeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Cachet\Http\Controllers\StatusPage;

use Cachet\Badger\Badger;
use Cachet\Enums\ResourceVisibilityEnum;
use Cachet\Http\BadgeResponseFactory;
use Cachet\Models\Component;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ComponentBadgeController
{
/**
* Render a badge for a publicly visible component.
*/
public function __invoke(Request $request, Component $component, Badger $badger, BadgeResponseFactory $badgeResponseFactory): Response
{
$component->loadMissing('group');

abort_if(! $component->enabled || ($component->group?->visible !== null && $component->group->visible !== ResourceVisibilityEnum::guest), 404);

return $badgeResponseFactory->make(
$badger->generate(
$component->name,
$component->latest_status->getLabel(),
$component->latest_status->getBadgeColor(),
$badgeResponseFactory->style($request),
),
);
}
}
24 changes: 24 additions & 0 deletions src/Http/Controllers/StatusPage/StatusBadgeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Cachet\Http\Controllers\StatusPage;

use Cachet\Badger\Badger;
use Cachet\Http\BadgeResponseFactory;
use Cachet\Status;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class StatusBadgeController
{
/**
* Render a badge for the status page's current system status.
*/
public function __invoke(Request $request, Status $status, Badger $badger, BadgeResponseFactory $badgeResponseFactory): Response
{
$systemStatus = $status->current();

return $badgeResponseFactory->make(
$badger->generate('status', $systemStatus->getLabel(), $systemStatus->getBadgeColor(), $badgeResponseFactory->style($request)),
);
}
}
4 changes: 4 additions & 0 deletions src/PendingRouteRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Cachet\Http\Controllers\HealthController;
use Cachet\Http\Controllers\RssController;
use Cachet\Http\Controllers\Setup\SetupController;
use Cachet\Http\Controllers\StatusPage\ComponentBadgeController;
use Cachet\Http\Controllers\StatusPage\StatusBadgeController;
use Cachet\Http\Controllers\StatusPage\StatusPageController;
use Cachet\Http\Controllers\Subscribers\SubscriberController;
use Cachet\Http\Controllers\Subscribers\UnsubscribeSubscriberController;
Expand Down Expand Up @@ -36,6 +38,8 @@ public function register(): void
$router->get('/', [StatusPageController::class, 'index'])->name('status-page');
$router->get('/incidents/{incident:guid}', [StatusPageController::class, 'show'])->name('status-page.incident');
$router->get('/schedules/{schedule}', [StatusPageController::class, 'schedule'])->name('status-page.schedule');
$router->get('/badge.svg', StatusBadgeController::class)->name('status-page.badge');
$router->get('/components/{component}/badge.svg', ComponentBadgeController::class)->name('status-page.component.badge');

$router->get('/setup', [SetupController::class, 'index'])->name('setup.index');
$router->post('/setup', [SetupController::class, 'store'])->name('setup.store');
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/StatusPage/StatusPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,50 @@
->assertOk();
});

it('renders an SVG badge for the overall status page status', function () {
Component::factory()->create(['status' => ComponentStatusEnum::operational]);

$response = $this->get(route('cachet.status-page.badge'))
->assertOk()
->assertHeader('content-type', 'image/svg+xml; charset=UTF-8')
->assertSee('<svg', escape: false)
->assertSee('All systems are operational.');

expect($response->headers->get('cache-control'))
->toContain('public')
->toContain('max-age=60')
->toContain('s-maxage=60');
});

it('renders an SVG badge for a public component', function () {
$component = Component::factory()->create([
'name' => 'Public API',
'status' => ComponentStatusEnum::major_outage,
]);

$this->get(route('cachet.status-page.component.badge', $component))
->assertOk()
->assertHeader('content-type', 'image/svg+xml; charset=UTF-8')
->assertSee('<svg', escape: false)
->assertSee('Public API')
->assertSee('Major outage');
});

it('does not render a badge for a component in a private group', function () {
$group = ComponentGroup::factory()->create(['visible' => 0]);
$component = Component::factory()->create(['component_group_id' => $group->id]);

$this->get(route('cachet.status-page.component.badge', $component))
->assertNotFound();
});

it('does not render a badge for a disabled component', function () {
$component = Component::factory()->create(['enabled' => false]);

$this->get(route('cachet.status-page.component.badge', $component))
->assertNotFound();
});

it('can hide the site name and about content without changing status page metadata', function () {
$settings = app(AppSettings::class);
$settings->name = 'Acme Status';
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/StatusBadgeColorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Cachet\Enums\ComponentStatusEnum;
use Cachet\Enums\SystemStatusEnum;

it('maps component statuses to badge colors', function (ComponentStatusEnum $status, string $color) {
expect($status->getBadgeColor())->toBe($color);
})->with([
[ComponentStatusEnum::operational, 'brightgreen'],
[ComponentStatusEnum::performance_issues, 'yellow'],
[ComponentStatusEnum::partial_outage, 'orange'],
[ComponentStatusEnum::major_outage, 'red'],
[ComponentStatusEnum::unknown, 'lightgray'],
[ComponentStatusEnum::under_maintenance, 'orange'],
]);

it('maps system statuses to badge colors', function (SystemStatusEnum $status, string $color) {
expect($status->getBadgeColor())->toBe($color);
})->with([
[SystemStatusEnum::operational, 'brightgreen'],
[SystemStatusEnum::partial_outage, 'orange'],
[SystemStatusEnum::major_outage, 'red'],
[SystemStatusEnum::under_maintenance, 'orange'],
]);
Loading