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

Add MAESTRO_CLI_NO_ANALYTICS flag #1848

Merged
merged 6 commits into from
Jul 30, 2024
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Changelog

## 1.37.4 - 2024-07-30

- Don't ask for analytics permission on CI + add `MAESTRO_CLI_NO_ANALYTICS` env var ([#1848](https://github.com/mobile-dev-inc/maestro/pull/1848))

## 1.37.3 - 2024-07-29

### Bug fixes

Fix `FileNotFoundException: ~.maestro/sessions` ([#1843](https://github.com/mobile-dev-inc/maestro/pull/1843))
- Fix `FileNotFoundException: ~.maestro/sessions` ([#1843](https://github.com/mobile-dev-inc/maestro/pull/1843))

## 1.37.2 - 2024-07-29

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ android.useAndroidX=true
android.enableJetifier=true
kotlin.code.style=official
GROUP=dev.mobile
VERSION_NAME=1.37.3
VERSION_NAME=1.37.4
POM_DESCRIPTION=Maestro is a server-driven platform-agnostic library that allows to drive tests for both iOS and Android using the same implementation through an intuitive API.
POM_URL=https://github.com/mobile-dev-inc/maestro
POM_SCM_URL=https://github.com/mobile-dev-inc/maestro
Expand Down
2 changes: 1 addition & 1 deletion maestro-cli/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CLI_VERSION=1.37.3
CLI_VERSION=1.37.4
25 changes: 24 additions & 1 deletion maestro-cli/src/main/java/maestro/cli/analytics/Analytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ object Analytics {
private val analyticsStatePath: Path = EnvUtils.xdgStateHome().resolve("analytics.json")
private val legacyUuidPath: Path = EnvUtils.legacyMaestroHome().resolve("uuid")

private const val DISABLE_ANALYTICS_ENV_VAR = "MAESTRO_CLI_NO_ANALYTICS"
private val analyticsDisabledWithEnvVar: Boolean
get() = System.getenv(DISABLE_ANALYTICS_ENV_VAR) != null

private val JSON = jacksonObjectMapper().apply {
registerModule(JavaTimeModule())
enable(SerializationFeature.INDENT_OUTPUT)
Expand Down Expand Up @@ -73,6 +77,21 @@ object Analytics {
fun maybeAskToEnableAnalytics() {
if (hasRunBefore) return

// Fix for https://github.com/mobile-dev-inc/maestro/issues/1846
if (CiUtils.getCiProvider() != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's maybe give DISABLE_ANALYTICS_ENV_VAR false by default if its CI?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need analytics if user is sourcing from CLI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? We set uuid to name of the CI service if we're running on CI:

private fun generateUUID(): String {
return CiUtils.getCiProvider() ?: UUID.randomUUID().toString()
}

which then gets uploaded to our backend:

I think it can be useful knowledge to know what CI service is most used*, so we would have some data if we will have to build integrations with CIs in the future.


*A problem here may be that we don't have UUIDs of specific CI installations. We would need to have separate "UUID" and "CI" values in the AnalyticsReport.

// AnalyticsReport must match equivalent monorepo model in:
// mobile.dev/api/models/src/main/java/models/maestro/AnalyticsReport.kt
@JsonIgnoreProperties(ignoreUnknown = true)
data class AnalyticsReport(
@JsonProperty("deviceUuid") val uuid: String,
@JsonProperty("freshInstall") val freshInstall: Boolean,
@JsonProperty("version") val cliVersion: String,
@JsonProperty("os") val os: String,
@JsonProperty("osArch") val osArch: String,
@JsonProperty("osVersion") val osVersion: String,
@JsonProperty("javaVersion") val javaVersion: String?,
@JsonProperty("xcodeVersion") val xcodeVersion: String?,
@JsonProperty("flutterVersion") val flutterVersion: String?,
@JsonProperty("flutterChannel") val flutterChannel: String?,
@JsonProperty("androidVersions") val androidVersions: List<String>,
@JsonProperty("iosVersions") val iosVersions: List<String>,
)

Otherwise analytics from various runs on CI get merged together.

if (!analyticsDisabledWithEnvVar) {
println("CI detected, analytics was automatically enabled.")
println("To opt out, set $DISABLE_ANALYTICS_ENV_VAR environment variable to any value before running Maestro.")
} else {
println("CI detected and $DISABLE_ANALYTICS_ENV_VAR environment variable set, analytics disabled.")
}
return
}

if (analyticsDisabledWithEnvVar) {
return
}

while (!Thread.interrupted()) {
println("Maestro CLI would like to collect anonymous usage data to improve the product.")
print("Enable analytics? [Y/n] ")
Expand All @@ -99,8 +118,12 @@ object Analytics {
return
}

if (analyticsDisabledWithEnvVar) {
logger.trace("Analytics disabled with env var, not uploading")
}

if (!analyticsState.enabled) {
logger.trace("Analytics disabled, not uploading")
logger.trace("Analytics disabled with config file, not uploading")
return
}

Expand Down
23 changes: 16 additions & 7 deletions maestro-cli/src/main/java/maestro/cli/util/CiUtils.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package maestro.cli.util

object CiUtils {

// When adding a new CI, also add the first version of Maestro that supports it.
private val ciEnvVarMap = mapOf(
"JENKINS_HOME" to "jenkins",
"APPVEYOR" to "appveyor", // since v1.37.4
"BITBUCKET_BUILD_NUMBER" to "bitbucket",
"BITRISE_IO" to "bitrise",
"BUILDKITE" to "buildkite", // since v1.37.4
"CIRCLECI" to "circleci",
"GITLAB_CI" to "gitlab",
"CIRRUS_CI" to "cirrusci", // since v1.37.4
"DRONE" to "drone", // since v1.37.4
"GITHUB_ACTIONS" to "github",
"BITBUCKET_BUILD_NUMBER" to "bitbucket",
"GITLAB_CI" to "gitlab",
"JENKINS_HOME" to "jenkins",
"TEAMCITY_VERSION" to "teamcity", // since v1.37.4
"CI" to "ci"
)

Expand All @@ -22,12 +29,14 @@ object CiUtils {
return mdevCiEnvVar
}

for (ciVar in ciEnvVarMap.entries) {
for (ciEnvVar in ciEnvVarMap.entries) {
try {
if (isTruthy(System.getenv(ciVar.key).lowercase())) return ciVar.value
} catch (e: Exception) {}
if (isTruthy(System.getenv(ciEnvVar.key).lowercase())) return ciEnvVar.value
} catch (e: Exception) {
// We don't care
}
}

return null
}
}
}