From 71b1b2d895af7cd567eaec78b67346b1cb45993e Mon Sep 17 00:00:00 2001 From: Braunson Yager Date: Fri, 8 Dec 2023 15:03:52 -0500 Subject: [PATCH] feat: Added sentryMonitor macro support for specifying environments to run in --- CONTRIBUTING.md | 4 +- .../Laravel/Features/ConsoleIntegration.php | 12 +++- .../Features/ConsoleIntegrationTest.php | 63 +++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7f289c33..c13b1cb9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -67,9 +67,11 @@ composer phpstan The code is automatically formatted through [php-cs-fixer](https://cs.symfony.com). ```bash -composer phpcs +composer cs-fix ``` +**Note**: there is a dry run option `composer cs-check` + ## Releasing a new version (only relevant for Sentry employees) diff --git a/src/Sentry/Laravel/Features/ConsoleIntegration.php b/src/Sentry/Laravel/Features/ConsoleIntegration.php index 49ab9826..fce4c170 100644 --- a/src/Sentry/Laravel/Features/ConsoleIntegration.php +++ b/src/Sentry/Laravel/Features/ConsoleIntegration.php @@ -19,7 +19,7 @@ class ConsoleIntegration extends Feature { /** - * @var array The list of checkins that are currently in progress. + * @var array The list of check-ins that are currently in progress. */ private $checkInStore = []; @@ -48,8 +48,13 @@ public function onBoot(Cache $cache): void ?string $monitorSlug = null, ?int $checkInMargin = null, ?int $maxRuntime = null, - bool $updateMonitorConfig = true + bool $updateMonitorConfig = true, + array $runOnEnvironments = [] ) use ($startCheckIn, $finishCheckIn) { + if (! empty($runOnEnvironments) && ! in_array(config('app.env'), $runOnEnvironments)) { + return $this; + } + /** @var SchedulingEvent $this */ if ($monitorSlug === null && $this->command === null) { throw new RuntimeException('The command string is null, please set a slug manually for this scheduled command using the `sentryMonitor(\'your-monitor-slug\')` macro.'); @@ -78,7 +83,8 @@ public function onBootInactive(): void ?string $monitorSlug = null, ?int $checkInMargin = null, ?int $maxRuntime = null, - bool $updateMonitorConfig = true + bool $updateMonitorConfig = true, + array $runOnEnvironments = [] ) { return $this; }); diff --git a/test/Sentry/Features/ConsoleIntegrationTest.php b/test/Sentry/Features/ConsoleIntegrationTest.php index 110c6b99..27ad0b09 100644 --- a/test/Sentry/Features/ConsoleIntegrationTest.php +++ b/test/Sentry/Features/ConsoleIntegrationTest.php @@ -83,6 +83,69 @@ public function testScheduleMacroWithoutSlugOrCommandName(): void $this->getScheduler()->call(function () {})->sentryMonitor(); } + public function testScheduledMacroWithSpecifiedEnvironmentsMatching(): void + { + $this->app['config']->set('app.env', 'production'); + + /** @var Event $scheduledEvent */ + $scheduledEvent = $this->getScheduler() + ->call(function () {}) + ->sentryMonitor('test-monitor', null, null, true, ['production']); + + $scheduledEvent->run($this->app); + + // We expect a total of 2 events to be sent to Sentry: + // 1. The start check-in event + // 2. The finish check-in event + $this->assertSentryCheckInCount(2); + + $finishCheckInEvent = $this->getLastSentryEvent(); + + $this->assertNotNull($finishCheckInEvent->getCheckIn()); + $this->assertEquals('test-monitor', $finishCheckInEvent->getCheckIn()->getMonitorSlug()); + } + + public function testScheduledMacroWithSpecifiedEnvironmentsNotMatching(): void + { + $this->app['config']->set('app.env', 'production'); + + /** @var Event $scheduledEvent */ + $scheduledEvent = $this->getScheduler() + ->call(function () {}) + ->sentryMonitor('test-monitor', null, null, true, ['staging']); + + $scheduledEvent->run($this->app); + + // We expect a total of 0 events to be sent to Sentry + $this->assertSentryCheckInCount(0); + + $finishCheckInEvent = $this->getLastSentryEvent(); + + $this->assertNull($finishCheckInEvent); + } + + public function testScheduledMacroWithSpecifiedEnvironmentsEmpty(): void + { + $this->app['config']->set('app.env', 'production'); + + /** @var Event $scheduledEvent */ + $scheduledEvent = $this->getScheduler() + ->call(function () {}) + ->sentryMonitor('test-monitor', null, null, true, []); + + $scheduledEvent->run($this->app); + + // We expect a total of 2 events to be sent to Sentry: + // 1. The start check-in event + // 2. The finish check-in event + $this->assertSentryCheckInCount(2); + + $finishCheckInEvent = $this->getLastSentryEvent(); + + $this->assertNotNull($finishCheckInEvent->getCheckIn()); + $this->assertEquals('test-monitor', $finishCheckInEvent->getCheckIn()->getMonitorSlug()); + } + /** @define-env envWithoutDsnSet */ public function testScheduleMacroWithoutDsnSet(): void {