Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear webhook cache when webhooks are deleted #695

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 13 additions & 4 deletions app/Models/WebhookConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@
...$webhookConfiguration->getOriginal('events', '[]'),
])->unique();

$changedEvents->each(function (string $event) {
cache()->forever("webhooks.$event", WebhookConfiguration::query()->whereJsonContains('events', $event)->get());
});
self::updateCache($changedEvents);
});

self::deleted(static function (self $webhookConfiguration): void {
self::updateCache(collect($webhookConfiguration->events));

Check failure on line 47 in app/Models/WebhookConfiguration.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $value of function collect expects Illuminate\Contracts\Support\Arrayable<(int|string), mixed>|iterable<(int|string), mixed>|null, string given.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self::updateCache(collect($webhookConfiguration->events));
self::updateCache(collect((array) $webhookConfiguration->events));

});
}

cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all());
private static function updateCache($eventList): void

Check failure on line 51 in app/Models/WebhookConfiguration.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method App\Models\WebhookConfiguration::updateCache() has parameter $eventList with no type specified.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static function updateCache($eventList): void
private static function updateCache(Collection $eventList): void

{
$eventList->each(function (string $event) {
cache()->forever("webhooks.$event", WebhookConfiguration::query()->whereJsonContains('events', $event)->get());
});

cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all());
}

public function webhooks(): HasMany
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Tests\Feature;
namespace App\Tests\Feature\Webhooks;

use App\Jobs\ProcessWebhook;
use App\Models\Server;
Expand Down Expand Up @@ -62,6 +62,32 @@ public function test_it_sends_some_webhooks()
Queue::assertPushed(ProcessWebhook::class, 1);
}

public function test_it_does_not_call_removed_events()
{
$webhookConfig = WebhookConfiguration::factory()->create([
'events' => ['eloquent.created: '.Server::class],
]);

$webhookConfig->update(['events' => 'eloquent.deleted: '.Server::class]);

$this->createServer();

Queue::assertNothingPushed();
}

public function test_it_does_not_call_deleted_webhooks()
{
$webhookConfig = WebhookConfiguration::factory()->create([
'events' => ['eloquent.created: '.Server::class],
]);

$webhookConfig->delete();

$this->createServer();

Queue::assertNothingPushed();
}

public function createServer(): Server
{
return Server::factory()->withNode()->create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Tests\Feature;
namespace App\Tests\Feature\Webhooks;

use App\Events\Server\Installed;
use App\Jobs\ProcessWebhook;
Expand Down
Loading