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

RUMM-2346 Add more environment variables #109

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class DdAndroidGradlePlugin @Inject constructor(
val environmentKey = System.getenv(DD_API_KEY)
if (!environmentKey.isNullOrBlank()) return ApiKey(environmentKey, ApiKeySource.ENVIRONMENT)

val alternativeEnvironmentKey = System.getenv(DATADOG_API_KEY)
if (!alternativeEnvironmentKey.isNullOrBlank()) {
return ApiKey(alternativeEnvironmentKey, ApiKeySource.ENVIRONMENT)
}
Comment on lines 62 to +68
Copy link
Member

Choose a reason for hiding this comment

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

can be also written like:

val envKey = listOf(System.getenv(DD_API_KEY), System.getenv(DATADOG_API_KEY))
                          .firstOrNull { !it.isNullOrBlank() }
if (envKey != null) ...

but I'm also fine with introducing another variable, it is a minor thing anyway

return ApiKey.NONE
}

Expand Down Expand Up @@ -277,6 +281,8 @@ class DdAndroidGradlePlugin @Inject constructor(

internal const val DD_API_KEY = "DD_API_KEY"

internal const val DATADOG_API_KEY = "DATADOG_API_KEY"

internal val LOGGER = LoggerFactory.getLogger("DdAndroidGradlePlugin")

private const val EXT_NAME = "datadog"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ open class DdMappingFileUploadTask
datadogCiFile?.let {
applyDatadogCiConfig(it)
}
applySiteFromEnvironment()
validateConfiguration()

var mappingFile = File(mappingFilePath)
Expand Down Expand Up @@ -163,6 +164,26 @@ open class DdMappingFileUploadTask
)
}

private fun applySiteFromEnvironment() {
val environmentSite = System.getenv(DATADOG_SITE)
if (!environmentSite.isNullOrEmpty()) {
if (this.site.isNotEmpty()) {
LOGGER.info(
"Site property found as DATADOG_SITE env variable, but it will be ignored," +
" because also an explicit one was provided in extension."
)
return
}
val site = DatadogSite.fromDomain(environmentSite)
if (site == null) {
LOGGER.warn("Unknown Datadog domain provided: $environmentSite, ignoring it.")
} else {
LOGGER.info("Site property found in Datadog CI config file, using it.")
this.site = site.name
}
}
}

private fun applyDatadogCiConfig(datadogCiFile: File) {
try {
val config = JSONObject(datadogCiFile.readText())
Expand Down Expand Up @@ -217,8 +238,8 @@ open class DdMappingFileUploadTask
private fun validateConfiguration() {
check(apiKey.isNotBlank()) {
"Make sure you define an API KEY to upload your mapping files to Datadog. " +
"Create a DD_API_KEY environment variable, gradle property or define it" +
" in datadog-ci.json file."
"Create a DD_API_KEY or DATADOG_API_KEY environment variable, gradle" +
" property or define it in datadog-ci.json file."
}

if (site.isBlank()) {
Expand Down Expand Up @@ -337,5 +358,6 @@ open class DdMappingFileUploadTask

private const val DATADOG_CI_API_KEY_PROPERTY = "apiKey"
private const val DATADOG_CI_SITE_PROPERTY = "datadogSite"
const val DATADOG_SITE = "DATADOG_SITE"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ internal class DdAndroidGradlePluginTest {
fakeFlavorNames = fakeFlavorNames.take(5) // A D F G A♭ A A♭ G F
fakeProject = ProjectBuilder.builder().build()
testedPlugin = DdAndroidGradlePlugin(mock())
setEnv(DdAndroidGradlePlugin.DD_API_KEY, "")
setEnv(DdAndroidGradlePlugin.DATADOG_API_KEY, "")
Comment on lines +81 to +82
Copy link
Member

Choose a reason for hiding this comment

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

is it mostly for cleanup? per code logic having an empty value is not different from not having env variable at all, so it seems these 2 lines don't make any change compared to the current setup (only if to clean up values set by some tests).

If it is for the cleanup, maybe it is better to remove env variables for the clean state?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it's for cleanup, the test for DATADOG_API_KEY wasn't passing when executed after the DD_API_KEY test.
How do you remove env variables in kotlin?

Copy link
Member

Choose a reason for hiding this comment

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

interesting, it seems there is no easy way to delete them

}

// region configureVariant()
Expand Down Expand Up @@ -486,6 +488,19 @@ internal class DdAndroidGradlePluginTest {
assertThat(apiKey.source).isEqualTo(ApiKeySource.ENVIRONMENT)
}

@Test
fun `𝕄 resolve API KEY from alternative environment variable 𝕎 resolveApiKey()`() {
// Given
setEnv(DdAndroidGradlePlugin.DATADOG_API_KEY, fakeApiKey.value)

// When
val apiKey = testedPlugin.resolveApiKey(fakeProject)

// Then
assertThat(apiKey.value).isEqualTo(fakeApiKey.value)
assertThat(apiKey.source).isEqualTo(ApiKeySource.ENVIRONMENT)
}

@Test
fun `𝕄 returns empty String 𝕎 resolveApiKey() {key not defined anywhere}`() {
// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ internal class DdMappingFileUploadTaskTest {
testedTask.versionName = fakeVersion
testedTask.serviceName = fakeService
testedTask.site = fakeSite.name
setEnv(DdMappingFileUploadTask.DATADOG_SITE, "")
}

@Test
Expand Down Expand Up @@ -575,6 +576,39 @@ internal class DdMappingFileUploadTaskTest {
assertThat(testedTask.site).isEqualTo(fakeSite.name)
}

@Test
fun `𝕄 read site from environment variable 𝕎 applyTask() {site is not set}`(forge: Forge) {

// Given
val fakeDatadogEnvDomain = forge.aValueFrom(DatadogSite::class.java).domain
setEnv(DdMappingFileUploadTask.DATADOG_SITE, fakeDatadogEnvDomain)
testedTask.site = ""

// When
testedTask.applyTask()

// Then
assertThat(testedTask.apiKey).isEqualTo(fakeApiKey.value)
assertThat(testedTask.apiKeySource).isEqualTo(fakeApiKey.source)
assertThat(testedTask.site).isEqualTo(DatadogSite.fromDomain(fakeDatadogEnvDomain)?.name)
}

@Test
fun `𝕄 read site from environment variable 𝕎 applyTask() {site is set}`(forge: Forge) {

// Given
val fakeDatadogEnvDomain = forge.aValueFrom(DatadogSite::class.java).domain
setEnv(DdAndroidGradlePlugin.DATADOG_API_KEY, fakeDatadogEnvDomain)

// When
testedTask.applyTask()

// Then
assertThat(testedTask.apiKey).isEqualTo(fakeApiKey.value)
assertThat(testedTask.apiKeySource).isEqualTo(fakeApiKey.source)
assertThat(testedTask.site).isEqualTo(fakeSite.name)
}

@Test
fun `𝕄 not apply datadog CI config if exists 𝕎 applyTask() { malformed json }`(forge: Forge) {

Expand Down
4 changes: 2 additions & 2 deletions docs/upload_mapping_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For any given error, you can access the file path, line number, and a code snipp
}
```

2. [Create a dedicated Datadog API key][2] and export it as an environment variable named `DD_API_KEY` (alternatively, pass it as a task property or if you have `datadog-ci.json` file in the root of your project, it can be taken from `apiKey` property there).
2. [Create a dedicated Datadog API key][2] and export it as an environment variable named `DD_API_KEY` or `DATADOG_API_KEY`. Alternatively, pass it as a task property, or if you have `datadog-ci.json` file in the root of your project, it can be taken from an `apiKey` property there.
3. Optionally, configure the plugin to upload files to the EU region by configuring the plugin in your `build.gradle` script:

```
Expand Down Expand Up @@ -65,7 +65,7 @@ For any given error, you can access the file path, line number, and a code snipp
}
```

2. [Create a dedicated Datadog API key][2] and export it as an environment variable named `DD_API_KEY` (alternatively, pass it as a task property or if you have `datadog-ci.json` file in the root of your project, it can be taken from `apiKey` property there).
2. [Create a dedicated Datadog API key][2] and export it as an environment variable named `DD_API_KEY` or `DATADOG_API_KEY`. Alternatively, pass it as a task property, or if you have `datadog-ci.json` file in the root of your project, it can be taken from an `apiKey` property there.
3. Configure the plugin to use the EU region by adding the following snippet in your app's `build.gradle` script file:

```groovy
Expand Down