Skip to content

Commit

Permalink
Redirect sentry-cli info stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi committed May 14, 2024
1 parent 5b60adf commit e507c03
Showing 1 changed file with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.sentry.SentryLevel
import io.sentry.SpanStatus
import io.sentry.android.gradle.SentryCliProvider
import io.sentry.android.gradle.SentryPlugin
import io.sentry.android.gradle.SentryPlugin.Companion.logger
import io.sentry.android.gradle.SentryPropertiesFileProvider
import io.sentry.android.gradle.extensions.SentryPluginExtension
import io.sentry.android.gradle.telemetry.SentryCliInfoValueSource.InfoParams
Expand Down Expand Up @@ -263,28 +264,30 @@ abstract class SentryTelemetryService :

// if telemetry is disabled we don't even need to exec sentry-cli as telemetry service
// will be no-op
if (isExecAvailable() && isTelemetryEnabled && extension.authToken.orNull != null) {
return paramsWithExecAvailable(
if (isExecAvailable() && isTelemetryEnabled) {
paramsWithExecAvailable(
project,
cliExecutable,
extension,
variant,
org,
buildType,
tags
)
} else {
return SentryTelemetryServiceParams(
isTelemetryEnabled,
extension.telemetryDsn.get(),
org,
buildType,
tags,
extension.debug.get(),
saas = extension.url.orNull == null,
cliVersion = BuildConfig.CliVersion
)
)?.let {
return it
}
}
// fallback: sentry-cli is not available or e.g. auth token is not configured
return SentryTelemetryServiceParams(
isTelemetryEnabled,
extension.telemetryDsn.get(),
org,
buildType,
tags,
extension.debug.get(),
saas = extension.url.orNull == null,
cliVersion = BuildConfig.CliVersion
)
}

private fun paramsWithExecAvailable(
Expand All @@ -295,7 +298,7 @@ abstract class SentryTelemetryService :
sentryOrg: String?,
buildType: String,
tags: Map<String, String>
): SentryTelemetryServiceParams {
): SentryTelemetryServiceParams? {
var cliVersion: String? = BuildConfig.CliVersion
var defaultSentryOrganization: String? = null
val infoOutput = project.providers.of(SentryCliInfoValueSource::class.java) { cliVS ->
Expand All @@ -309,6 +312,10 @@ abstract class SentryTelemetryService :
)
}
}.get()

if (infoOutput.isEmpty()) {
return null
}
val isSaas = infoOutput.contains("(?m)Sentry Server: .*sentry.io$".toRegex())

orgRegex.find(infoOutput)?.let { matchResult ->
Expand Down Expand Up @@ -471,9 +478,11 @@ abstract class SentryCliInfoValueSource : ValueSource<String, InfoParams> {
@get:Inject
abstract val execOperations: ExecOperations

override fun obtain(): String {
val output = ByteArrayOutputStream()
execOperations.exec {
override fun obtain(): String? {
val stdOutput = ByteArrayOutputStream()
val errOutput = ByteArrayOutputStream()

val execResult = execOperations.exec {
it.isIgnoreExitValue = true
SentryCliProvider.maybeExtractFromResources(
parameters.buildDirectory.get(),
Expand All @@ -499,9 +508,19 @@ abstract class SentryCliInfoValueSource : ValueSource<String, InfoParams> {
}

it.commandLine(args)
it.standardOutput = output
it.standardOutput = stdOutput
it.errorOutput = errOutput
}

if (execResult.exitValue == 0) {
return String(stdOutput.toByteArray(), Charset.defaultCharset())
} else {
logger.info {
"Failed to execute sentry-cli info. Error Output: " +
String(errOutput.toByteArray(), Charset.defaultCharset())
}
return ""
}
return String(output.toByteArray(), Charset.defaultCharset())
}
}

Expand Down

0 comments on commit e507c03

Please sign in to comment.