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

Track environment provider in telemetry. #1741

Merged
merged 17 commits into from
May 23, 2024
51 changes: 51 additions & 0 deletions src/Helpers/TelemetryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
'ah_non_production' => getenv('AH_NON_PRODUCTION'),
'ah_realm' => getenv('AH_REALM'),
'CI' => getenv('CI'),
'env_provider' => $this->getEnvironmentProvider(),

Check warning on line 123 in src/Helpers/TelemetryHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers/TelemetryHelper.php#L123

Added line #L123 was not covered by tests
'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
];
try {
Expand All @@ -134,6 +135,20 @@
return $data;
}

public static function getEnvironmentProvider(): ?string {
$providers = self::getProviders();

// Check for environment variables.
foreach ($providers as $provider => $vars) {
foreach ($vars as $var) {
if (getenv($var) !== FALSE)
return $provider;
}
}

return NULL;
}

private function getUserId(): ?string {
$user = $this->getUserData();
if ($user && isset($user['uuid'])) {
Expand Down Expand Up @@ -180,4 +195,40 @@
];
}

/**
* @infection-ignore-all
* Skipping infection testing for this because, it most cases, we expect that when a row from this array is changed
* it won't affect the return value.
* @return array<mixed>
* An array of providers and their associated environment variables.
*/
public static function getProviders(): array {
// Define the environment variables associated with each provider.
// phpcs:ignore SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
return [
'lando' => ['LANDO'],
'ddev' => ['IS_DDEV_PROJECT'],
// Check Lando and DDEV first because the hijack AH_SITE_ENVIRONMENT.
'acquia' => ['AH_SITE_ENVIRONMENT'],
'bamboo' => ['BAMBOO_BUILDNUMBER'],
'beanstalk' => ['BEANSTALK_ENVIRONMENT'],
'bitbucket' => ['BITBUCKET_BUILD_NUMBER'],
'bitrise' => ['BITRISE_IO'],
'buddy' => ['BUDDY_WORKSPACE_ID'],
'circleci' => ['CIRCLECI'],
'codebuild' => ['CODEBUILD_BUILD_ID'],
'docksal' => ['DOCKSAL_VERSION'],
'drone' => ['DRONE'],
'github' => ['GITHUB_ACTIONS'],
'gitlab' => ['GITLAB_CI'],
'heroku' => ['HEROKU_TEST_RUN_ID'],
'jenkins' => ['JENKINS_URL'],
'pantheon' => ['PANTHEON_ENVIRONMENT'],
'pipelines' => ['PIPELINE_ENV'],
'platformsh' => ['PLATFORM_ENVIRONMENT'],
'teamcity' => ['TEAMCITY_VERSION'],
'travis' => ['TRAVIS'],
];
}

}
71 changes: 71 additions & 0 deletions tests/phpunit/src/Misc/TelemetryHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Misc;

use Acquia\Cli\Helpers\TelemetryHelper;
use Acquia\Cli\Tests\TestBase;

class TelemetryHelperTest extends TestBase {

const ENV_VAR_DEFAULT_VALUE = 'test';

public function tearDown(): void {
parent::tearDown();
$envVars = [];
foreach ($this->providerTestEnvironmentProvider() as $args) {
$envVars = array_merge($envVars, $args[1]);
}

TestBase::unsetEnvVars($envVars);
}

public function unsetGitHubEnvVars(): void {
$providers = TelemetryHelper::getProviders();

// Since we actually run our own tests on GitHub, getEnvironmentProvider() will return 'github' unless we unset it.
$github_env_vars = [];
foreach ($providers['github'] as $var) {
$github_env_vars[$var] = self::ENV_VAR_DEFAULT_VALUE;
}
TestBase::unsetEnvVars($github_env_vars);
}

/**
* @return array<mixed>
*/
public function providerTestEnvironmentProvider(): array {
$providersList = TelemetryHelper::getProviders();
$providersArray = [];
foreach ($providersList as $provider => $envVars) {
$env_vars_with_values = [];
foreach ($envVars as $var_name) {
$env_vars_with_values[$var_name] = self::ENV_VAR_DEFAULT_VALUE;
}
$providersArray[] = [$provider, $env_vars_with_values];
}

return $providersArray;
}

/**
* @dataProvider providerTestEnvironmentProvider()
*/
public function testEnvironmentProvider(string $provider, array $envVars): void {
$this->unsetGitHubEnvVars();
TestBase::setEnvVars($envVars);
$this->assertEquals($provider, TelemetryHelper::getEnvironmentProvider());
}

/**
* Test the getEnvironmentProvider method when no environment provider is detected.
*/
public function testGetEnvironmentProviderWithoutAnyEnvSet(): void {
$this->unsetGitHubEnvVars();

// Expect null since no provider environment variables are set.
$this->assertNull(TelemetryHelper::getEnvironmentProvider());
}

}