From 4490bcd3f12816f1ce16feadabc4975d4dd11375 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Wed, 18 May 2022 16:26:40 +0100 Subject: [PATCH 1/4] Update bugsnag-php version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d07d79ef..6f003dc4 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "php": ">=5.5", - "bugsnag/bugsnag": "^3.27.0", + "bugsnag/bugsnag": "^3.28.0", "bugsnag/bugsnag-psr-logger": "^1.4|^2.0", "illuminate/contracts": "^5.0|^6.0|^7.0|^8.0|^9.0", "illuminate/support": "^5.0|^6.0|^7.0|^8.0|^9.0", From 02ce809cb5cba8b8ee5dde90c8e79c93593fe6d8 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Tue, 17 May 2022 15:11:00 +0100 Subject: [PATCH 2/4] Allow adding feature flags in config --- config/bugsnag.php | 19 +++++++++++++ src/BugsnagServiceProvider.php | 19 +++++++++++++ tests/ServiceProviderTest.php | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/config/bugsnag.php b/config/bugsnag.php index d4016be6..20997a3c 100644 --- a/config/bugsnag.php +++ b/config/bugsnag.php @@ -330,4 +330,23 @@ */ 'redacted_keys' => empty(env('BUGSNAG_REDACTED_KEYS')) ? null : explode(',', env('BUGSNAG_REDACTED_KEYS')), + + /* + |-------------------------------------------------------------------------- + | Feature flags + |-------------------------------------------------------------------------- + | + | An array of feature flags to add to all reports. + | + | Each element in the array must have a "name" key and can optionally have a + | "variant" key, for example: + | + | [ + | ['name' => 'example without a variant'], + | ['name' => 'example with a variant', 'variant' => 'example of a variant'], + | ] + | + */ + + 'feature_flags' => [], ]; diff --git a/src/BugsnagServiceProvider.php b/src/BugsnagServiceProvider.php index c9b7c196..7431d0cb 100644 --- a/src/BugsnagServiceProvider.php +++ b/src/BugsnagServiceProvider.php @@ -9,6 +9,7 @@ use Bugsnag\Callbacks\CustomUser; use Bugsnag\Client; use Bugsnag\Configuration; +use Bugsnag\FeatureFlag; use Bugsnag\PsrLogger\BugsnagLogger; use Bugsnag\PsrLogger\MultiLogger as BaseMultiLogger; use Bugsnag\Report; @@ -245,6 +246,24 @@ public function register() $client->setRedactedKeys($config['redacted_keys']); } + if (isset($config['feature_flags']) && is_array($config['feature_flags']) && $config['feature_flags'] !== []) { + $featureFlags = []; + + foreach ($config['feature_flags'] as $flag) { + if (!is_array($flag) || !array_key_exists('name', $flag)) { + continue; + } + + if (array_key_exists('variant', $flag)) { + $featureFlags[] = new FeatureFlag($flag['name'], $flag['variant']); + } else { + $featureFlags[] = new FeatureFlag($flag['name']); + } + } + + $client->addFeatureFlags($featureFlags); + } + return $client; }); diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 0a41b306..58d17da8 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -465,4 +465,53 @@ public function memoryLimitIncreaseProvider() [1024 * 1024 * 20], ]; } + + public function testFeatureFlagsCanBeSetFromConfig() + { + /** @var \Illuminate\Config\Repository $laravelConfig */ + $laravelConfig = $this->app->config; + $bugsnagConfig = $laravelConfig->get('bugsnag'); + $bugsnagConfig['feature_flags'] = [ + ['name' => 'flag1'], + ['name' => 'flag2', 'variant' => 'yes'], + ['not name' => 'flag3'], + ['name' => 'flag4', 'not variant' => 'xyz'], + ]; + + $laravelConfig->set('bugsnag', $bugsnagConfig); + + /** @var Client $client */ + $client = $this->app->make(Client::class); + + $this->assertInstanceOf(Client::class, $client); + + $expected = [ + ['featureFlag' => 'flag1'], + ['featureFlag' => 'flag2', 'variant' => 'yes'], + ['featureFlag' => 'flag4'], + ]; + + $actual = $client->getConfig()->getFeatureFlagsCopy()->toArray(); + + $this->assertSame($expected, $actual); + } + + public function testFeatureFlagsAreNotSetWhenNotAnArray() + { + /** @var \Illuminate\Config\Repository $laravelConfig */ + $laravelConfig = $this->app->config; + $bugsnagConfig = $laravelConfig->get('bugsnag'); + $bugsnagConfig['feature_flags'] = new \stdClass(); + + $laravelConfig->set('bugsnag', $bugsnagConfig); + + /** @var Client $client */ + $client = $this->app->make(Client::class); + + $this->assertInstanceOf(Client::class, $client); + + $actual = $client->getConfig()->getFeatureFlagsCopy()->toArray(); + + $this->assertSame([], $actual); + } } From bf7f81ce3ac462f60e96ed79071335a01fb09673 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Wed, 18 May 2022 16:27:33 +0100 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 633f81fd..44d3ed22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Changelog ========= +## TBD + + ### Enhancements + + * New APIs to support feature flag and experiment functionality. For more information, please see https://docs.bugsnag.com/product/features-experiments. + [#153](https://github.com/bugsnag/bugsnag-laravel/pull/487) + ## 2.23.0 (2022-02-09) ### Enhancements From 7276a35266dd8c074f4bc70f05b02cd5789982df Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Thu, 19 May 2022 10:25:32 +0100 Subject: [PATCH 4/4] Bump version --- CHANGELOG.md | 4 ++-- src/BugsnagServiceProvider.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44d3ed22..4626200b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ Changelog ========= -## TBD +## 2.24.0 (2022-05-20) - ### Enhancements +### Enhancements * New APIs to support feature flag and experiment functionality. For more information, please see https://docs.bugsnag.com/product/features-experiments. [#153](https://github.com/bugsnag/bugsnag-laravel/pull/487) diff --git a/src/BugsnagServiceProvider.php b/src/BugsnagServiceProvider.php index 7431d0cb..fe7373f5 100644 --- a/src/BugsnagServiceProvider.php +++ b/src/BugsnagServiceProvider.php @@ -37,7 +37,7 @@ class BugsnagServiceProvider extends ServiceProvider * * @var string */ - const VERSION = '2.23.0'; + const VERSION = '2.24.0'; /** * Boot the service provider.