diff --git a/composer.json b/composer.json index be4529b..e53b3ef 100644 --- a/composer.json +++ b/composer.json @@ -12,14 +12,15 @@ }, "require": { "php": "^8.0", - "laravel/framework": "^8.53 || ^9.9", - "statamic/cms": "^3.3" + "laravel/framework": "^9.9", + "statamic/cms": "^3.4", + "transformstudios/front": "^1.12.1" }, "require-dev": { "mockery/mockery": "^1.3.1", - "nunomaduro/collision": "^5.0", + "nunomaduro/collision": "^6.0", "phpunit/phpunit": "^9.0", - "orchestra/testbench": "^6.0", + "orchestra/testbench": "^7.0", "spatie/laravel-ray": "^1.24" }, "extra": { @@ -33,5 +34,16 @@ ] }, "patches": {} - } + }, + "config": { + "allow-plugins": { + "pixelfear/composer-dist-plugin": true + } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/transformstudios/statamic-front" + } + ] } diff --git a/resources/views/alert_cleared.antlers.html b/resources/views/alert_cleared.antlers.html new file mode 100644 index 0000000..c796443 --- /dev/null +++ b/resources/views/alert_cleared.antlers.html @@ -0,0 +1,158 @@ + + + +  + +    Daily Checkout Report +    + +    +    +    +  + + +  + + + + + + + + +
+ + +
+ + +
+ Green Checkmark +
+ +

+ Error Resolved +

+
+ + +
+ +
+ + + + +

+ Hello again. +

+

+ The previously reported error is now resolved. The details for the resolved check are below: +

+ +
    +
  • Name of Check: {{ test }}
  • +
  • Monitor Type: {{ type }}
  • +
  • Alert Output: {{ output }}
  • +
  • Error Duration: {{ duration }}
  • +
+ + + + +
+
+ + + + +
+ +  + diff --git a/resources/views/alert_raised.antlers.html b/resources/views/alert_raised.antlers.html new file mode 100644 index 0000000..ebd8ccb --- /dev/null +++ b/resources/views/alert_raised.antlers.html @@ -0,0 +1,166 @@ + + + +  + +    Daily Checkout Report +    + +    +    +    +  + + +  + + + + + + + + +
+ + +
+ + +
+ Error Detected + +
+ +

+ Error Detected +

+
+ + +
+ +
+ + + + +

+ Hello {{ name or 'there' }}. 👋 +

+

+ Our global network of monitoring servers has reported an error on your web property. Our 1st priority is prompt resolution when any downtime or error is detected by our systems all while providing you full transparency of our progress on resolution.

This message is to confirm we have started to investigate and will keep you updated on the progress until final resolution. +

+ +
    +
  • Name of Check: {{ test }}
  • +
  • Monitor Type: {{ type }}
  • +
  • Location(s): {{ locations }}
  • +
  • Alert Output: {{ output }}
  • +
+ + + + +

+ + Feel free to reply to this email with any questions, but rest assured we are working on resolution. Stay tuned for further updates... + +

+ + +
+
+ + + + +
+ +  + \ No newline at end of file diff --git a/src/Http/Controllers/WebhookController.php b/src/Http/Controllers/WebhookController.php index 105f675..d5eb888 100644 --- a/src/Http/Controllers/WebhookController.php +++ b/src/Http/Controllers/WebhookController.php @@ -20,7 +20,7 @@ class WebhookController extends Controller public function __invoke(Request $request) { - $method = 'handle' . Str::studly(str_replace('.', '_', $request->input('event'))); + $method = 'handle'.Str::studly(str_replace('.', '_', $request->input('event'))); if (method_exists($this, $method)) { return $this->{$method}($request->input('data')); @@ -39,7 +39,7 @@ private function handleAlertRaised(array $payload) return $this->handleAlert(AlertRaised::class, $payload); } - private function handleAlert($notificationClass, $payload) + private function handleAlert(string $notificationClass, array $payload) { if (! $tag = Arr::get($payload, 'service.tags.0')) { return response()->noContent(); diff --git a/src/Notifications/AbstractAlert.php b/src/Notifications/AbstractAlert.php index 12945bf..6e37756 100644 --- a/src/Notifications/AbstractAlert.php +++ b/src/Notifications/AbstractAlert.php @@ -2,51 +2,33 @@ namespace TransformStudios\Uptime\Notifications; -use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Notifications\Notification; +use Illuminate\Support\Collection; use Statamic\Support\Arr; +use TransformStudios\Front\Notifications\BaseNotification; -abstract class AbstractAlert extends Notification implements ShouldQueue +abstract class AbstractAlert extends BaseNotification { - use Queueable; - - protected string $event; - protected string $subject; - - public function __construct(protected $alert, protected $users) - { - } - - /** - * Get the notification's delivery channels. - * - * @param mixed $notifiable - * @return array - */ - public function via($notifiable) + public function __construct(string $subject, array $payload, string $template, Collection $users) { - return ['front']; + parent::__construct( + Arr::get($payload, 'service.id'), + $subject, + view("uptime::$template", $this->data($payload))->render(), + $users, + ); } - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray(): array + protected function data(array $payload, array $additionalData = []): array { - return [ - 'event' => $this->event, - 'date' => Arr::get($this->alert, 'date'), - 'id' => Arr::get($this->alert, 'service.id'), - 'is_up' => Arr::get($this->alert, 'alert.is_up'), - 'output' => Arr::get($this->alert, 'alert.short_output'), - 'subject' => $this->subject, - 'test' => Arr::get($this->alert, 'service.name'), - 'type' => Arr::get($this->alert, 'service.monitoring_service_type'), - 'users' => $this->users, - ]; + return array_merge( + [ + 'date' => Arr::get($payload, 'date'), + 'is_up' => Arr::get($payload, 'alert.is_up'), + 'output' => Arr::get($payload, 'alert.short_output'), + 'test' => Arr::get($payload, 'service.name'), + 'type' => Arr::get($payload, 'service.monitoring_service_type'), + ], + $additionalData, + ); } } diff --git a/src/Notifications/AlertCleared.php b/src/Notifications/AlertCleared.php index f788f2a..c28ad8b 100644 --- a/src/Notifications/AlertCleared.php +++ b/src/Notifications/AlertCleared.php @@ -3,23 +3,29 @@ namespace TransformStudios\Uptime\Notifications; use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; use Statamic\Support\Arr; class AlertCleared extends AbstractAlert { - protected string $event = 'alert_cleared'; - protected string $subject = 'Monitor Alert: Alert Cleared'; + public function __construct(array $payload, Collection $users) + { + parent::__construct( + subject: 'Monitor Alert: Alert Cleared', + payload: $payload, + template: 'alert_cleared', + users: $users + ); + } - public function toArray(): array + protected function data(array $payload, array $additionalData = []): array { - $startedAt = Carbon::parse(Arr::get($this->alert, 'downtime.started_at')); - $endedAt = Carbon::parse(Arr::get($this->alert, 'downtime.ended_at')); + $startedAt = Carbon::parse(Arr::get($payload, 'downtime.started_at')); + $endedAt = Carbon::parse(Arr::get($payload, 'downtime.ended_at')); - return array_merge( - parent::toArray(), - [ - 'duration' => $endedAt->diffForHumans($startedAt, Carbon::DIFF_ABSOLUTE, false, 3), - ] + return parent::data( + $payload, + ['duration' => $endedAt->diffForHumans($startedAt, Carbon::DIFF_ABSOLUTE, false, 3)] ); } } diff --git a/src/Notifications/AlertRaised.php b/src/Notifications/AlertRaised.php index 72f50a7..bf9ade9 100644 --- a/src/Notifications/AlertRaised.php +++ b/src/Notifications/AlertRaised.php @@ -2,20 +2,30 @@ namespace TransformStudios\Uptime\Notifications; +use Illuminate\Support\Collection; use Statamic\Support\Arr; class AlertRaised extends AbstractAlert { protected string $event = 'alert_raised'; + protected string $subject = 'Monitor Alert: Error Detected'; - public function toArray(): array + public function __construct(array $payload, Collection $users) + { + parent::__construct( + subject: 'Monitor Alert: Error Detected', + payload: $payload, + template: 'alert_raised', + users: $users + ); + } + + protected function data(array $payload, array $additionalData = []): array { - return array_merge( - parent::toArray(), - [ - 'locations' => implode(',', Arr::get($this->alert, 'locations', [])), - ] + return parent::data( + $payload, + ['locations' => implode(',', Arr::get($payload, 'locations', []))] ); } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 83c4ccf..bf9e808 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -14,4 +14,15 @@ class ServiceProvider extends AddonServiceProvider protected $fieldtypes = [ Tag::class, ]; + + public function bootAddon() + { + // needed for testing but not production + if (app()->environment('testing')) { + $this->loadViewsFrom( + __DIR__.'/../resources/views', + 'uptime' + ); + } + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index cb6b1c3..7114c3d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -45,7 +45,7 @@ protected function getEnvironmentSetUp($app) $app->make(Manifest::class)->manifest = [ 'transformstudios/uptime' => [ 'id' => 'transformstudios/uptime', - 'namespace' => 'TransformStudios\\Uptime\\', + 'namespace' => 'TransformStudios\\Uptime', ], ]; } @@ -57,7 +57,7 @@ protected function resolveApplicationConfiguration($app) $configs = ['assets', 'cp', 'forms', 'routes', 'static_caching', 'sites', 'stache', 'system', 'users']; foreach ($configs as $config) { - $app['config']->set("statamic.$config", require __DIR__ . "/../vendor/statamic/cms/config/{$config}.php"); + $app['config']->set("statamic.$config", require __DIR__."/../vendor/statamic/cms/config/{$config}.php"); } // Setting the user repository to the default flat file system diff --git a/tests/Unit/WebhookTest.php b/tests/Unit/WebhookTest.php index 96b696c..eee32ea 100644 --- a/tests/Unit/WebhookTest.php +++ b/tests/Unit/WebhookTest.php @@ -61,6 +61,7 @@ public function sends_alert_cleared_notification() $data = [ 'data' => [ 'service' => [ + 'id' => 123, 'tags' => [ 'foo', ],