Unverified Commit e25d8810 authored by jcollins-g's avatar jcollins-g Committed by GitHub

Reduce the chances of bots reporting analytics (#13915)

* First version

* Prevent modification of .flutter during analytics test

* Pass in directory and override analyzer warning due to conditional import

* Review comments
parent ad898ad0
...@@ -27,6 +27,12 @@ bool get isRunningOnBot { ...@@ -27,6 +27,12 @@ bool get isRunningOnBot {
// https://www.appveyor.com/docs/environment-variables/ // https://www.appveyor.com/docs/environment-variables/
platform.environment.containsKey('APPVEYOR') || platform.environment.containsKey('APPVEYOR') ||
// https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
(platform.environment.containsKey('AWS_REGION') && platform.environment.containsKey('CODEBUILD_INITIATOR')) ||
// https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
platform.environment.containsKey('JENKINS_URL') ||
// Properties on Flutter's Chrome Infra bots. // Properties on Flutter's Chrome Infra bots.
platform.environment['CHROME_HEADLESS'] == '1' || platform.environment['CHROME_HEADLESS'] == '1' ||
platform.environment.containsKey('BUILDBOT_BUILDERNAME'); platform.environment.containsKey('BUILDBOT_BUILDERNAME');
......
...@@ -8,6 +8,7 @@ import 'package:meta/meta.dart'; ...@@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
import 'package:usage/usage_io.dart'; import 'package:usage/usage_io.dart';
import 'base/context.dart'; import 'base/context.dart';
import 'base/file_system.dart';
import 'base/os.dart'; import 'base/os.dart';
import 'base/platform.dart'; import 'base/platform.dart';
import 'base/utils.dart'; import 'base/utils.dart';
...@@ -19,11 +20,16 @@ const String _kFlutterUA = 'UA-67589403-6'; ...@@ -19,11 +20,16 @@ const String _kFlutterUA = 'UA-67589403-6';
Usage get flutterUsage => Usage.instance; Usage get flutterUsage => Usage.instance;
class Usage { class Usage {
/// Create a new Usage instance; [versionOverride] is used for testing. /// Create a new Usage instance; [versionOverride] and [configDirOverride] are
Usage({ String settingsName: 'flutter', String versionOverride }) { /// used for testing.
Usage({ String settingsName: 'flutter', String versionOverride, String configDirOverride}) {
final FlutterVersion flutterVersion = FlutterVersion.instance; final FlutterVersion flutterVersion = FlutterVersion.instance;
final String version = versionOverride ?? flutterVersion.getVersionString(whitelistBranchName: true); final String version = versionOverride ?? flutterVersion.getVersionString(whitelistBranchName: true);
_analytics = new AnalyticsIO(_kFlutterUA, settingsName, version); _analytics = new AnalyticsIO(_kFlutterUA, settingsName, version,
// Analyzer doesn't recognize that [Directory] objects match up due to a
// conditional import.
// ignore: argument_type_not_assignable
documentDirectory: configDirOverride != null ? fs.directory(configDirOverride) : null);
// Report a more detailed OS version string than package:usage does by default. // Report a more detailed OS version string than package:usage does by default.
_analytics.setSessionValue('cd1', os.name); _analytics.setSessionValue('cd1', os.name);
...@@ -33,19 +39,13 @@ class Usage { ...@@ -33,19 +39,13 @@ class Usage {
if (platform.environment.containsKey('FLUTTER_HOST')) { if (platform.environment.containsKey('FLUTTER_HOST')) {
_analytics.setSessionValue('aiid', platform.environment['FLUTTER_HOST']); _analytics.setSessionValue('aiid', platform.environment['FLUTTER_HOST']);
} }
_analytics.analyticsOpt = AnalyticsOpt.optOut;
bool runningOnCI = false;
// Many CI systems don't do a full git checkout. // Many CI systems don't do a full git checkout.
if (version.endsWith('/unknown')) if (version.endsWith('/unknown') || isRunningOnBot) {
runningOnCI = true; // If we think we're running on a CI system, suppress sending analytics.
suppressAnalytics = true;
// Check for common CI systems. }
if (isRunningOnBot)
runningOnCI = true;
// If we think we're running on a CI system, default to not sending analytics.
_analytics.analyticsOpt = runningOnCI ? AnalyticsOpt.optIn : AnalyticsOpt.optOut;
} }
/// Returns [Usage] active in the current app context. /// Returns [Usage] active in the current app context.
......
...@@ -55,7 +55,7 @@ void main() { ...@@ -55,7 +55,7 @@ void main() {
expect(count, 0); expect(count, 0);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => new FlutterVersion(const Clock()), FlutterVersion: () => new FlutterVersion(const Clock()),
Usage: () => new Usage(), Usage: () => new Usage(configDirOverride: temp.path),
}); });
// Ensure we don't send for the 'flutter config' command. // Ensure we don't send for the 'flutter config' command.
...@@ -74,7 +74,7 @@ void main() { ...@@ -74,7 +74,7 @@ void main() {
expect(count, 0); expect(count, 0);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => new FlutterVersion(const Clock()), FlutterVersion: () => new FlutterVersion(const Clock()),
Usage: () => new Usage(), Usage: () => new Usage(configDirOverride: temp.path),
}); });
}); });
...@@ -134,6 +134,11 @@ void main() { ...@@ -134,6 +134,11 @@ void main() {
}); });
group('analytics bots', () { group('analytics bots', () {
Directory temp;
setUp(() {
temp = fs.systemTempDirectory.createTempSync('flutter_tools');
});
testUsingContext('don\'t send on bots', () async { testUsingContext('don\'t send on bots', () async {
int count = 0; int count = 0;
flutterUsage.onSend.listen((Map<String, dynamic> data) => count++); flutterUsage.onSend.listen((Map<String, dynamic> data) => count++);
...@@ -144,6 +149,22 @@ void main() { ...@@ -144,6 +149,22 @@ void main() {
Usage: () => new Usage( Usage: () => new Usage(
settingsName: 'flutter_bot_test', settingsName: 'flutter_bot_test',
versionOverride: 'dev/unknown', versionOverride: 'dev/unknown',
configDirOverride: temp.path,
),
});
testUsingContext('don\'t send on bots even when opted in', () async {
int count = 0;
flutterUsage.onSend.listen((Map<String, dynamic> data) => count++);
flutterUsage.enabled = true;
await createTestCommandRunner().run(<String>['--version']);
expect(count, 0);
}, overrides: <Type, Generator>{
Usage: () => new Usage(
settingsName: 'flutter_bot_test',
versionOverride: 'dev/unknown',
configDirOverride: temp.path,
), ),
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment