Skip to content

Commit

Permalink
Fix misleading auth-token error message in case "sentry-cli info" fai…
Browse files Browse the repository at this point in the history
…ls (#708)

* Fix don't run cli if auth token is not present

* Redirect sentry-cli info stderr

* Update Changelog

* Add test

* Make ktlint happy

* Fix tests
  • Loading branch information
markushi committed May 16, 2024
1 parent 2da0bcc commit 0399750
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Fix misleading auth-token error message in case "sentry-cli info" fails ([#708](https://github.com/getsentry/sentry-android-gradle-plugin/pull/708))

### Dependencies

- Bump CLI from v2.31.1 to v2.31.2 ([#702](https://github.com/getsentry/sentry-android-gradle-plugin/pull/702))
Expand Down
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 @@ -264,27 +265,29 @@ 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) {
return paramsWithExecAvailable(
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.sentry.android.gradle.telemetry

import io.sentry.android.gradle.SentryCliProvider
import kotlin.test.assertEquals
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

class SentryTelemetryServiceTest {

@get:Rule
val testProjectDir = TemporaryFolder()

@Suppress("UnstableApiUsage")
@Test
fun `SentryCliInfoValueSource returns empty string when no auth token is present`() {
val project = ProjectBuilder
.builder()
.withProjectDir(testProjectDir.root)
.build()

val cliPath = SentryCliProvider
.getCliResourcesExtractionPath(project.layout.buildDirectory.asFile.get())

val infoOutput = project.providers.of(SentryCliInfoValueSource::class.java) { cliVS ->
cliVS.parameters.buildDirectory.set(project.buildDir)
cliVS.parameters.cliExecutable.set(cliPath.absolutePath)
// sets an empty/invalid auth token
cliVS.parameters.authToken.set("")
}.get()

assertEquals("", infoOutput)
}
}

0 comments on commit 0399750

Please sign in to comment.