-
Notifications
You must be signed in to change notification settings - Fork 0
feat(webhooks): include app_instance_uuid in app instance webhook payloads #257
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
database/migrations/2026_08_06_000001_backfill_uuid_on_polydock_app_instances_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| /** | ||
| * The uuid column was added nullable (2025_03_21) without backfilling rows | ||
| * that already existed, so their status transitions would emit webhook | ||
| * payloads with a null app_instance_uuid — the field consumers are told to | ||
| * key on. Assign uuids to those legacy rows, including soft-deleted ones. | ||
| */ | ||
| public function up(): void | ||
| { | ||
| if (in_array(DB::connection()->getDriverName(), ['mysql', 'mariadb'], true)) { | ||
| // Single atomic statement: no window between chunks where a | ||
| // concurrent status transition could still read a null uuid. | ||
| DB::statement('UPDATE polydock_app_instances SET uuid = UUID() WHERE uuid IS NULL'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Portable fallback for drivers without UUID() (sqlite in tests, where | ||
| // there is no concurrent traffic to race against). | ||
| DB::table('polydock_app_instances') | ||
| ->whereNull('uuid') | ||
| ->orderBy('id') | ||
| ->chunkById(100, function ($instances): void { | ||
| foreach ($instances as $instance) { | ||
| DB::table('polydock_app_instances') | ||
| ->where('id', $instance->id) | ||
| ->update(['uuid' => Str::uuid()->toString()]); | ||
| } | ||
| }); | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| // Intentionally a no-op: backfilled uuids are indistinguishable from | ||
| // boot-assigned ones, and consumers may already hold them. | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/Feature/Listeners/CreateWebhookCallForAppInstanceStatusChangedTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Feature\Listeners; | ||
|
|
||
| use App\Events\PolydockAppInstanceCreatedWithNewStatus; | ||
| use App\Events\PolydockAppInstanceStatusChanged; | ||
| use App\Listeners\CreateWebhookCallForAppInstanceStatusChanged; | ||
| use App\Models\PolydockAppInstance; | ||
| use App\Models\PolydockStore; | ||
| use App\Models\PolydockStoreApp; | ||
| use App\Models\PolydockStoreWebhook; | ||
| use App\Models\PolydockStoreWebhookCall; | ||
| use App\Polydock\Core\Enums\PolydockAppInstanceStatus; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\Queue; | ||
| use Illuminate\Support\Str; | ||
| use Tests\TestCase; | ||
|
|
||
| /** | ||
| * The app-instance webhook payload is a published contract: consumers hold the | ||
| * instance `uuid` (it is what the API returns on create and the only identifier | ||
| * they ever see), so a payload without it cannot be resolved back to an | ||
| * instance. The identifying fields are pinned here. | ||
| */ | ||
| class CreateWebhookCallForAppInstanceStatusChangedTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| private function makeInstance(PolydockAppInstanceStatus $status): PolydockAppInstance | ||
| { | ||
| $store = PolydockStore::factory()->create(); | ||
| $storeApp = PolydockStoreApp::factory()->create([ | ||
| 'polydock_store_id' => $store->id, | ||
| ]); | ||
|
|
||
| PolydockStoreWebhook::factory()->active()->create([ | ||
| 'polydock_store_id' => $store->id, | ||
| 'url' => 'https://example.com/webhooks/polydock', | ||
| ]); | ||
|
|
||
| $instance = new PolydockAppInstance; | ||
| $instance->polydock_store_app_id = $storeApp->id; | ||
| $instance->name = 'webhook-payload-test-'.Str::random(6); | ||
| $instance->app_type = 'test-app'; | ||
| $instance->status = $status; | ||
| $instance->data = []; | ||
| // saveQuietly() skips the model's creating hook, which is what normally | ||
| // fills the uuid — set it explicitly so this test exercises the payload | ||
| // rather than the model's boot sequence. | ||
| $instance->uuid = (string) Str::uuid(); | ||
| $instance->saveQuietly(); | ||
|
|
||
| return $instance; | ||
| } | ||
|
|
||
| public function test_created_event_payload_carries_the_app_instance_uuid(): void | ||
| { | ||
| Queue::fake(); | ||
|
|
||
| $instance = $this->makeInstance(PolydockAppInstanceStatus::NEW); | ||
|
|
||
| (new CreateWebhookCallForAppInstanceStatusChanged)->handle( | ||
| new PolydockAppInstanceCreatedWithNewStatus($instance), | ||
| ); | ||
|
|
||
| $call = PolydockStoreWebhookCall::query()->sole(); | ||
|
|
||
| self::assertSame('app_instance.created', $call->event); | ||
| self::assertSame($instance->uuid, $call->payload['app_instance_uuid']); | ||
| self::assertSame($instance->id, $call->payload['app_instance_id']); | ||
| self::assertNull($call->payload['previous_status']); | ||
| self::assertSame( | ||
| PolydockAppInstanceStatus::NEW->value, | ||
| $call->payload['current_status'], | ||
| ); | ||
| } | ||
|
|
||
| public function test_status_changed_event_payload_carries_the_app_instance_uuid(): void | ||
| { | ||
| Queue::fake(); | ||
|
|
||
| $instance = $this->makeInstance(PolydockAppInstanceStatus::PENDING_DEPLOY); | ||
|
|
||
| (new CreateWebhookCallForAppInstanceStatusChanged)->handle( | ||
| new PolydockAppInstanceStatusChanged( | ||
| $instance, | ||
| PolydockAppInstanceStatus::PENDING_PRE_DEPLOY, | ||
| ), | ||
| ); | ||
|
|
||
| $call = PolydockStoreWebhookCall::query()->sole(); | ||
|
|
||
| self::assertSame('app_instance.status_changed', $call->event); | ||
| self::assertSame($instance->uuid, $call->payload['app_instance_uuid']); | ||
| self::assertSame( | ||
| PolydockAppInstanceStatus::PENDING_PRE_DEPLOY->value, | ||
| $call->payload['previous_status'], | ||
| ); | ||
| self::assertSame( | ||
| PolydockAppInstanceStatus::PENDING_DEPLOY->value, | ||
| $call->payload['current_status'], | ||
| ); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
tests/Feature/Migrations/BackfillUuidOnPolydockAppInstancesTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Feature\Migrations; | ||
|
|
||
| use App\Models\PolydockStore; | ||
| use App\Models\PolydockStoreApp; | ||
| use App\Polydock\Core\Enums\PolydockAppInstanceStatus; | ||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Str; | ||
| use Tests\TestCase; | ||
|
|
||
| class BackfillUuidOnPolydockAppInstancesTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| public function test_backfill_assigns_uuids_to_legacy_null_rows_only(): void | ||
| { | ||
| $store = PolydockStore::factory()->create(); | ||
| $storeApp = PolydockStoreApp::factory()->create([ | ||
| 'polydock_store_id' => $store->id, | ||
| ]); | ||
|
|
||
| // Insert directly so no model boot hook fills the uuid, mirroring rows | ||
| // created before the uuid column existed. | ||
| $legacyId = DB::table('polydock_app_instances')->insertGetId([ | ||
| 'polydock_store_app_id' => $storeApp->id, | ||
| 'name' => 'legacy-null-uuid', | ||
| 'app_type' => 'test-app', | ||
| 'status' => PolydockAppInstanceStatus::NEW->value, | ||
| 'uuid' => null, | ||
| 'created_at' => now(), | ||
| 'updated_at' => now(), | ||
| ]); | ||
|
|
||
| $existingUuid = '0f1c9a3e-6d2b-4e1a-9b6f-2c4d8e7a5b31'; | ||
| $modernId = DB::table('polydock_app_instances')->insertGetId([ | ||
| 'polydock_store_app_id' => $storeApp->id, | ||
| 'name' => 'modern-with-uuid', | ||
| 'app_type' => 'test-app', | ||
| 'status' => PolydockAppInstanceStatus::NEW->value, | ||
| 'uuid' => $existingUuid, | ||
| 'created_at' => now(), | ||
| 'updated_at' => now(), | ||
| ]); | ||
|
|
||
| $migration = require database_path('migrations/2026_08_06_000001_backfill_uuid_on_polydock_app_instances_table.php'); | ||
| self::assertInstanceOf(Migration::class, $migration); | ||
| if (! method_exists($migration, 'up')) { | ||
| self::fail('Backfill migration does not define up()'); | ||
| } | ||
| $migration->up(); | ||
|
|
||
| $legacyUuid = DB::table('polydock_app_instances')->where('id', $legacyId)->value('uuid'); | ||
| self::assertNotNull($legacyUuid); | ||
| self::assertTrue(Str::isUuid($legacyUuid)); | ||
|
|
||
| // Rows that already had a uuid keep it untouched. | ||
| self::assertSame( | ||
| $existingUuid, | ||
| DB::table('polydock_app_instances')->where('id', $modernId)->value('uuid'), | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.