Skip to content

Commit db15ee6

Browse files
danepowellCopilot
andauthored
Add build date, exclude old bugsnag reports (#1954)
* Add build date, exclude old bugsnag reports * Update src/Helpers/TelemetryHelper.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * extract build date * fix tests * add test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5dec28b commit db15ee6

File tree

8 files changed

+78
-5
lines changed

8 files changed

+78
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ jobs:
9595
echo BUGSNAG_KEY=${{ secrets.BUGSNAG_KEY }} >> .env
9696
echo AMPLITUDE_KEY=${{ secrets.AMPLITUDE_KEY }} >> .env
9797
echo ACLI_VERSION=${{ steps.acli-version.outputs.ACLI_VERSION }} >> .env
98+
echo ACLI_BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") >> .env
9899
- name: Build
99100
run: |
100101
composer install --no-dev --optimize-autoloader

bin/acli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ if (in_array($input->getFirstArgument(), ['clear-kernel-cache', 'ckc'])) {
8181
putenv('BUGSNAG_KEY=');
8282
putenv('AMPLITUDE_KEY=');
8383
putenv('ACLI_VERSION=dev-unknown');
84+
putenv('ACLI_BUILD_DATE=1970-01-01T00:00:00Z');
8485
$dotenv = Dotenv::createUnsafeMutable(__DIR__ . '/..');
8586
$dotenv->safeLoad();
8687

src/Application.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Application extends \Symfony\Component\Console\Application
1717
*/
1818
protected array $helpMessages = [];
1919

20+
protected ?string $buildDate = null;
21+
2022
/**
2123
* @return string[]
2224
*/
@@ -43,4 +45,13 @@ public function renderThrowable(
4345
$io->block($this->getHelpMessages(), 'help', 'help', ' ', true, false);
4446
}
4547
}
48+
49+
public function getBuildDate(): ?string
50+
{
51+
if ($this->buildDate !== null) {
52+
return $this->buildDate;
53+
}
54+
$this->buildDate = getenv('ACLI_BUILD_DATE') ?: null;
55+
return $this->buildDate;
56+
}
4657
}

src/Command/CommandBase.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,11 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
235235
);
236236
$this->formatter = $this->getHelper('formatter');
237237

238-
$this->output->writeln('Acquia CLI version: ' . $this->getApplication()
239-
->getVersion(), OutputInterface::VERBOSITY_DEBUG);
238+
$application = $this->getApplication();
239+
$version = ($application && method_exists($application, 'getVersion')) ? $application->getVersion() : null;
240+
$this->output->writeln('Acquia CLI version: ' . ($version ?? 'unknown'), OutputInterface::VERBOSITY_DEBUG);
241+
$buildDate = ($application && method_exists($application, 'getBuildDate')) ? $application->getBuildDate() : null;
242+
$this->output->writeln('Build date: ' . ($buildDate ?? 'unknown'), OutputInterface::VERBOSITY_DEBUG);
240243
$this->output->writeln('PHP runtime: ' . PHP_SAPI, OutputInterface::VERBOSITY_DEBUG);
241244
if (getenv('ACLI_NO_TELEMETRY') !== 'true') {
242245
$this->checkAndPromptTelemetryPreference();

src/Command/Self/SelfInfoCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ final class SelfInfoCommand extends CommandBase
1717
protected function execute(InputInterface $input, OutputInterface $output): int
1818
{
1919
$table = $this->createTable($output, 'Acquia CLI information', ['Property', 'Value']);
20-
$table->addRow(['Version', $this->getApplication()->getVersion()]);
20+
$application = $this->getApplication();
21+
$version = ($application && method_exists($application, 'getVersion')) ? $application->getVersion() : null;
22+
$table->addRow(['Version', $version ?? 'unknown']);
23+
$buildDate = ($application && method_exists($application, 'getBuildDate')) ? $application->getBuildDate() : null;
24+
$table->addRow(['Build date', $buildDate ?? 'unknown']);
2125
$table->addRow(['Cloud datastore', $this->datastoreCloud->filepath]);
2226
$table->addRow(['ACLI datastore', $this->datastoreAcli->filepath]);
2327
$table->addRow(['Telemetry enabled', var_export($this->telemetryHelper->telemetryEnabled(), true)]);

src/Helpers/TelemetryHelper.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ public function initialize(): void
3434
$this->initializeBugsnag();
3535
}
3636

37+
/**
38+
* Checks if the build date is older than the given number of months.
39+
*
40+
* @param string|null $buildDate The build date string (any strtotime-compatible format).
41+
* @param int $months Number of months to compare.
42+
* @param int|null $now Optional timestamp to use as 'now' (for testing).
43+
* @return bool True if build date is older than given months, false otherwise.
44+
*/
45+
public static function isBuildDateOlderThanMonths(?string $buildDate, int $months, ?int $now = null): bool
46+
{
47+
if (!$buildDate) {
48+
return false;
49+
}
50+
$buildTimestamp = strtotime($buildDate);
51+
if ($buildTimestamp === false) {
52+
return false;
53+
}
54+
$now = $now ?? time();
55+
$interval = 60 * 60 * 24 * 30 * $months;
56+
return ($now - $buildTimestamp) > $interval;
57+
}
58+
3759
public function initializeBugsnag(): void
3860
{
3961
if (empty($this->bugSnagKey)) {
@@ -42,6 +64,12 @@ public function initializeBugsnag(): void
4264
if (!$this->telemetryEnabled()) {
4365
return;
4466
}
67+
// Check build date: suppress Bugsnag if more than 3 months old.
68+
$buildDate = $this->application->getBuildDate();
69+
if (self::isBuildDateOlderThanMonths($buildDate, 3)) {
70+
// Too old, do not send Bugsnag reports.
71+
return;
72+
}
4573
// It's safe-ish to make this key public.
4674
// @see https://github.com/bugsnag/bugsnag-js/issues/595
4775
$bugsnag = Client::make($this->bugSnagKey);
@@ -151,12 +179,12 @@ public function getTelemetryUserData(): array
151179
{
152180
$data = [
153181
'ah_app_uuid' => getenv('AH_APPLICATION_UUID'),
154-
'ah_env' => $this->normalizeAhEnv(AcquiaDrupalEnvironmentDetector::getAhEnv()),
182+
'ah_env' => self::normalizeAhEnv(AcquiaDrupalEnvironmentDetector::getAhEnv()),
155183
'ah_group' => AcquiaDrupalEnvironmentDetector::getAhGroup(),
156184
'ah_non_production' => getenv('AH_NON_PRODUCTION'),
157185
'ah_realm' => getenv('AH_REALM'),
158186
'CI' => getenv('CI'),
159-
'env_provider' => $this->getEnvironmentProvider(),
187+
'env_provider' => self::getEnvironmentProvider(),
160188
'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
161189
];
162190
try {

tests/phpunit/src/Commands/Self/SelfInfoCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testSelfInfoCommand(): void
2929
$this->assertStringContainsString('Property', $output);
3030
$this->assertStringContainsString('--------', $output);
3131
$this->assertStringContainsString('Version', $output);
32+
$this->assertStringContainsString('Build date', $output);
3233
$this->assertStringContainsString('Cloud datastore', $output);
3334
$this->assertStringContainsString('ACLI datastore', $output);
3435
$this->assertStringContainsString('Telemetry enabled', $output);

tests/phpunit/src/Misc/TelemetryHelperTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,28 @@ public function testAhEnvNormalization(string $ah_env, string $expected): void
108108
$normalized_ah_env = TelemetryHelper::normalizeAhEnv($ah_env);
109109
$this->assertEquals($expected, $normalized_ah_env);
110110
}
111+
112+
public function testIsBuildDateOlderThanMonthsNullDate(): void
113+
{
114+
$this->assertFalse(TelemetryHelper::isBuildDateOlderThanMonths(null, 3));
115+
}
116+
117+
public function testIsBuildDateOlderThanMonthsInvalidDate(): void
118+
{
119+
$this->assertFalse(TelemetryHelper::isBuildDateOlderThanMonths('not-a-date', 3));
120+
}
121+
122+
public function testIsBuildDateOlderThanMonthsRecentDate(): void
123+
{
124+
$now = strtotime('2026-01-12');
125+
$buildDate = date('Y-m-d', strtotime('-2 months', $now));
126+
$this->assertFalse(TelemetryHelper::isBuildDateOlderThanMonths($buildDate, 3, $now));
127+
}
128+
129+
public function testIsBuildDateOlderThanMonthsOldDate(): void
130+
{
131+
$now = strtotime('2026-01-12');
132+
$buildDate = date('Y-m-d', strtotime('-4 months', $now));
133+
$this->assertTrue(TelemetryHelper::isBuildDateOlderThanMonths($buildDate, 3, $now));
134+
}
111135
}

0 commit comments

Comments
 (0)