From 87f9144641887a468ec73462ffabc60185f3dc14 Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 18 Apr 2023 15:40:28 -0700 Subject: [PATCH 1/6] id is needed now --- tests/Unit/WebhookTest.php | 1 + 1 file changed, 1 insertion(+) 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', ], From 29f69482b8042f567244f8394ddbba414f16abc8 Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 18 Apr 2023 15:40:53 -0700 Subject: [PATCH 2/6] get tests going again --- src/ServiceProvider.php | 11 +++++++++++ tests/TestCase.php | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) 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 From ed59351fa704a3fa330271f713a3428143fd38aa Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 18 Apr 2023 15:41:18 -0700 Subject: [PATCH 3/6] use new front notification structure --- resources/views/alert_cleared.antlers.html | 158 ++++++++++++++++++++ resources/views/alert_raised.antlers.html | 166 +++++++++++++++++++++ src/Notifications/AbstractAlert.php | 50 +++---- src/Notifications/AlertCleared.php | 26 ++-- src/Notifications/AlertRaised.php | 22 ++- 5 files changed, 378 insertions(+), 44 deletions(-) create mode 100644 resources/views/alert_cleared.antlers.html create mode 100644 resources/views/alert_raised.antlers.html 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 }}
  • +
+ + + + +
+
+ + +
+ +
+ Transform Studios +
+
+ +
+ +  + 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... + +

+ + +
+
+ + +
+ +
+ Transform Studios +
+
+ +
+ +  + \ No newline at end of file diff --git a/src/Notifications/AbstractAlert.php b/src/Notifications/AbstractAlert.php index 12945bf..70411c6 100644 --- a/src/Notifications/AbstractAlert.php +++ b/src/Notifications/AbstractAlert.php @@ -2,20 +2,21 @@ 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) + public function __construct(string $subject, array $payload, string $template, Collection $users) { + parent::__construct( + Arr::get($payload, 'service.id'), + $subject, + view("uptime::$template", $this->data($payload))->render(), + $users, + ); + } /** @@ -29,24 +30,17 @@ public function via($notifiable) return ['front']; } - /** - * 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', []))] ); } } From 81058748d25f52c3a35ce112f7e43abdc9f38ea9 Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 18 Apr 2023 15:41:22 -0700 Subject: [PATCH 4/6] tidy --- src/Http/Controllers/WebhookController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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(); From fe4d1fe34575ed0b82915454b29a898ff3660806 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 19 Apr 2023 11:12:50 -0700 Subject: [PATCH 5/6] tidy --- src/Notifications/AbstractAlert.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Notifications/AbstractAlert.php b/src/Notifications/AbstractAlert.php index 70411c6..6e37756 100644 --- a/src/Notifications/AbstractAlert.php +++ b/src/Notifications/AbstractAlert.php @@ -16,18 +16,6 @@ public function __construct(string $subject, array $payload, string $template, C view("uptime::$template", $this->data($payload))->render(), $users, ); - - } - - /** - * Get the notification's delivery channels. - * - * @param mixed $notifiable - * @return array - */ - public function via($notifiable) - { - return ['front']; } protected function data(array $payload, array $additionalData = []): array From 89204cce220f0585c66745462e03af877c19e2c9 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 19 Apr 2023 11:13:00 -0700 Subject: [PATCH 6/6] update deps --- composer.json | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) 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" + } + ] }