Skip to content

Prefer the SENTRY_RELEASE environment variable over the package root version #753

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 3 commits into from
Sep 5, 2023
Merged
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
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ parameters:
count: 1
path: tests/DependencyInjection/ConfigurationTest.php

-
message: "#^Function Symfony\\\\Component\\\\DependencyInjection\\\\Loader\\\\Configurator\\\\ref not found\\.$#"
count: 1
path: tests/DependencyInjection/Fixtures/php/release_option_fallback_to_env_var.php

-
message: "#^Used function Symfony\\\\Component\\\\DependencyInjection\\\\Loader\\\\Configurator\\\\ref not found\\.$#"
count: 1
path: tests/DependencyInjection/Fixtures/php/release_option_fallback_to_env_var.php

-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 1
Expand All @@ -305,6 +315,11 @@ parameters:
count: 1
path: tests/DependencyInjection/SentryExtensionTest.php

-
message: "#^Cannot access offset 'release' on mixed\\.$#"
count: 1
path: tests/DependencyInjection/SentryExtensionTest.php

-
message: "#^Class Symfony\\\\Component\\\\Debug\\\\Exception\\\\FatalErrorException not found\\.$#"
count: 1
Expand Down
3 changes: 1 addition & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Sentry\SentryBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Jean85\PrettyVersions;
use Sentry\Options;
use Sentry\SentryBundle\ErrorTypesParser;
use Sentry\Transport\TransportFactoryInterface;
Expand Down Expand Up @@ -102,7 +101,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('logger')->end()
->scalarNode('release')
->cannotBeEmpty()
->defaultValue(PrettyVersions::getRootPackageVersion()->getPrettyVersion())
->defaultValue('%env(default::SENTRY_RELEASE)%')
->end()
->scalarNode('server_name')->end()
->scalarNode('before_send')->end()
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sentry\SentryBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Jean85\PrettyVersions;
use Psr\Log\NullLogger;
use Sentry\Client;
use Sentry\ClientBuilder;
Expand Down Expand Up @@ -67,6 +68,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');

if (!$container->hasParameter('env(SENTRY_RELEASE)')) {
$container->setParameter('env(SENTRY_RELEASE)', PrettyVersions::getRootPackageVersion()->getPrettyVersion());
}

$this->registerConfiguration($container, $mergedConfig);
$this->registerErrorListenerConfiguration($container, $mergedConfig);
$this->registerMessengerListenerConfiguration($container, $mergedConfig['messenger']);
Expand Down
3 changes: 1 addition & 2 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Sentry\SentryBundle\Tests\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Jean85\PrettyVersions;
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\DependencyInjection\Configuration;
use Symfony\Bundle\TwigBundle\TwigBundle;
Expand All @@ -29,7 +28,7 @@ public function testProcessConfigurationWithDefaultConfiguration(): void
'integrations' => [],
'prefixes' => array_merge(['%kernel.project_dir%'], array_filter(explode(\PATH_SEPARATOR, get_include_path() ?: ''))),
'environment' => '%kernel.environment%',
'release' => PrettyVersions::getRootPackageVersion()->getPrettyVersion(),
'release' => '%env(default::SENTRY_RELEASE)%',
'tags' => [],
'in_app_exclude' => [
'%kernel.cache_dir%',
Expand Down
28 changes: 28 additions & 0 deletions tests/DependencyInjection/Fixtures/StubEnvVarLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\DependencyInjection\Fixtures;

use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;

final class StubEnvVarLoader implements EnvVarLoaderInterface
{
/**
* @var array<string, string>
*/
private $envs = [];

/**
* @param array<string, string> $envs
*/
public function __construct(array $envs)
{
$this->envs = $envs;
}

public function loadEnvVars(): array
{
return $this->envs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\ContainerBuilder;

/** @var ContainerBuilder $container */
$container->loadFromExtension('sentry', []);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Sentry\SentryBundle\Tests\DependencyInjection\Fixtures\StubEnvVarLoader;
use Symfony\Component\DependencyInjection\EnvVarProcessor;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

return static function (ContainerConfigurator $container): void {
$container->extension('sentry', []);

$container->services()
->set(StubEnvVarLoader::class)
->tag('container.env_var_loader')
->args([['SENTRY_RELEASE' => '1.0.x-dev']])

->set(EnvVarProcessor::class)
->tag('container.env_var_processor')
->args([
function_exists('Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\service') ? service('service_container') : ref('service_container'),
tagged_iterator('container.env_var_loader'),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\ContainerBuilder;

/** @var ContainerBuilder $container */
$container->loadFromExtension('sentry', [
'options' => [
'release' => '1.0.x-dev',
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\ContainerBuilder;

/** @var ContainerBuilder $container */
$container->setParameter('env(APP_RELEASE)', '1.0.x-dev');
$container->loadFromExtension('sentry', [
'options' => [
'release' => '%env(APP_RELEASE)%',
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sentry="https://sentry.io/schema/dic/sentry-symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
https://sentry.io/schema/dic/sentry-symfony https://sentry.io/schema/dic/sentry-symfony/sentry-1.0.xsd">

<sentry:config />
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sentry="https://sentry.io/schema/dic/sentry-symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
https://sentry.io/schema/dic/sentry-symfony https://sentry.io/schema/dic/sentry-symfony/sentry-1.0.xsd">

<services>
<service id="Sentry\SentryBundle\Tests\DependencyInjection\Fixtures\StubEnvVarLoader" class="Sentry\SentryBundle\Tests\DependencyInjection\Fixtures\StubEnvVarLoader">
<argument type="collection">
<argument type="string" key="SENTRY_RELEASE">1.0.x-dev</argument>
</argument>

<tag name="container.env_var_loader" />
</service>

<service id="Symfony\Component\DependencyInjection\EnvVarProcessor" class="Symfony\Component\DependencyInjection\EnvVarProcessor">
<argument type="service" id="service_container" />
<argument type="tagged_iterator" tag="container.env_var_loader" />

<tag name="container.env_var_processor" />
</service>
</services>

<sentry:config />
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sentry="https://sentry.io/schema/dic/sentry-symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
https://sentry.io/schema/dic/sentry-symfony https://sentry.io/schema/dic/sentry-symfony/sentry-1.0.xsd">

<sentry:config>
<sentry:options release="1.0.x-dev" />
</sentry:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sentry="https://sentry.io/schema/dic/sentry-symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
https://sentry.io/schema/dic/sentry-symfony https://sentry.io/schema/dic/sentry-symfony/sentry-1.0.xsd">

<parameters>
<parameter key="env(APP_RELEASE)">1.0.x-dev</parameter>
</parameters>

<sentry:config>
<sentry:options release="%env(APP_RELEASE)%" />
</sentry:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sentry:
options: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
Sentry\SentryBundle\Tests\DependencyInjection\Fixtures\StubEnvVarLoader:
arguments:
- { SENTRY_RELEASE: 1.0.x-dev }
tags:
- { name: container.env_var_loader }

Symfony\Component\DependencyInjection\EnvVarProcessor:
arguments:
- '@service_container'
- !tagged_iterator container.env_var_loader
tags:
- { name: container.env_var_processor }

sentry:
options: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sentry:
options:
release: 1.0.x-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
env(APP_RELEASE): 1.0.x-dev

sentry:
options:
release: '%env(APP_RELEASE)%'
46 changes: 45 additions & 1 deletion tests/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sentry\SentryBundle\Tests\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Jean85\PrettyVersions;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Sentry\ClientInterface;
Expand All @@ -30,6 +31,9 @@
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveTaggedIteratorArgumentPass;
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
Expand Down Expand Up @@ -448,20 +452,60 @@ public function testLoggerOptionFallbackToNullLoggerIfNotSet(): void
$this->assertDefinitionMethodCallAt($methodCalls[5], 'setLogger', [new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]);
}

/**
* @dataProvider releaseOptionDataProvider
*/
public function testReleaseOption(string $fixtureFile, string $expectedRelease): void
{
$container = $this->createContainerFromFixture($fixtureFile);
$optionsDefinition = $container->getDefinition('sentry.client.options');

$this->assertSame(Options::class, $optionsDefinition->getClass());
$this->assertSame($expectedRelease, $container->resolveEnvPlaceholders($optionsDefinition->getArgument(0)['release'], true));
}

public function releaseOptionDataProvider(): \Generator
{
yield 'If the release option is set to a concrete value, then no fallback occurs' => [
'release_option_from_config',
'1.0.x-dev',
];

yield 'If the release option is set and references an environment variable, then no fallback occurs' => [
'release_option_from_env_var',
'1.0.x-dev',
];

yield 'If the release option is unset and the SENTRY_RELEASE environment variable is set, then the latter is used as fallback' => [
'release_option_fallback_to_env_var',
'1.0.x-dev',
];

yield 'If both the release option and the SENTRY_RELEASE environment variable are unset, then the root package version is used as fallback' => [
'release_option_fallback_to_composer_version',
PrettyVersions::getRootPackageVersion()->getPrettyVersion(),
];
}

private function createContainerFromFixture(string $fixtureFile): ContainerBuilder
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
'kernel.cache_dir' => __DIR__,
'kernel.build_dir' => __DIR__,
'kernel.project_dir' => __DIR__,
'kernel.environment' => 'dev',
'doctrine.default_connection' => 'default',
'doctrine.connections' => ['default'],
]));

$container->registerExtension(new SentryExtension());
$container->getCompilerPassConfig()->setOptimizationPasses([]);
$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
$container->getCompilerPassConfig()->setOptimizationPasses([
new ValidateEnvPlaceholdersPass(),
new ResolveParameterPlaceHoldersPass(),
new ResolveTaggedIteratorArgumentPass(),
]);

$this->loadFixture($container, $fixtureFile);

Expand Down