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 4 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
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:

https://github.com/mobile-dev-inc/maestro/blob/21ae6eb6eae669a397174dc69bedef203384413e/maestro-cli/src/main/java/maestro/cli/analytics/Analytics.kt#L191-L193

which then gets uploaded to our backend:

https://github.com/mobile-dev-inc/maestro/blob/21ae6eb6eae669a397174dc69bedef203384413e/maestro-cli/src/main/java/maestro/cli/analytics/Analytics.kt#L167

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.

https://github.com/mobile-dev-inc/maestro/blob/21ae6eb6eae669a397174dc69bedef203384413e/maestro-cli/src/main/java/maestro/cli/analytics/Analytics.kt#L204-L220

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
21 changes: 14 additions & 7 deletions maestro-cli/src/main/java/maestro/cli/util/CiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package maestro.cli.util

object CiUtils {
private val ciEnvVarMap = mapOf(
"JENKINS_HOME" to "jenkins",
"APPVEYOR" to "appveyor",
"BITBUCKET_BUILD_NUMBER" to "bitbucket",
"BITRISE_IO" to "bitrise",
"BUILDKITE" to "buildkite",
"CIRCLECI" to "circleci",
"GITLAB_CI" to "gitlab",
"CIRRUS_CI" to "cirrusci",
"DRONE" to "drone",
"GITHUB_ACTIONS" to "github",
"BITBUCKET_BUILD_NUMBER" to "bitbucket",
"GITLAB_CI" to "gitlab",
"JENKINS_HOME" to "jenkins",
"TEAMCITY_VERSION" to "teamcity",
"CI" to "ci"
)

Expand All @@ -22,12 +27,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
}
}
}