|
1 | 1 | package com.gabrielfeo.develocity.api
|
2 | 2 |
|
3 |
| -import com.gabrielfeo.develocity.api.Config.CacheConfig |
4 |
| -import com.gabrielfeo.develocity.api.internal.* |
| 3 | +import com.gabrielfeo.develocity.api.internal.auth.accessKeyResolver |
| 4 | +import com.gabrielfeo.develocity.api.internal.basicOkHttpClient |
| 5 | +import com.gabrielfeo.develocity.api.internal.env |
| 6 | +import com.gabrielfeo.develocity.api.internal.systemProperties |
5 | 7 | import okhttp3.Dispatcher
|
6 | 8 | import okhttp3.OkHttpClient
|
7 | 9 | import java.io.File
|
| 10 | +import java.net.URI |
8 | 11 | import kotlin.time.Duration.Companion.days
|
9 | 12 |
|
10 | 13 | /**
|
@@ -46,15 +49,32 @@ data class Config(
|
46 | 49 | */
|
47 | 50 | val apiUrl: String =
|
48 | 51 | env["DEVELOCITY_API_URL"]
|
| 52 | + ?.also { requireValidUrl(it) } |
49 | 53 | ?: error(ERROR_NULL_API_URL),
|
50 | 54 |
|
51 | 55 | /**
|
52 |
| - * Provides the access token for a Develocity API instance. By default, uses environment |
53 |
| - * variable `DEVELOCITY_API_TOKEN`. |
| 56 | + * Provides the access key for a Develocity API instance. By default, resolves to the first |
| 57 | + * key from these sources that matches the host of [apiUrl]: |
| 58 | + * |
| 59 | + * - variable `DEVELOCITY_ACCESS_KEY` |
| 60 | + * - variable `GRADLE_ENTERPRISE_ACCESS_KEY` |
| 61 | + * - file `$GRADLE_USER_HOME/.gradle/develocity/keys.properties` or, if `GRADLE_USER_HOME` is |
| 62 | + * not set, `~/.gradle/develocity/keys.properties` |
| 63 | + * - file `~/.m2/.develocity/keys.properties` |
| 64 | + * |
| 65 | + * Refer to Develocity documentation for details on the format of such variables and files: |
| 66 | + * |
| 67 | + * - [Develocity Gradle Plugin User Manual][1] |
| 68 | + * - [Develocity Maven Extension User Manual][2] |
| 69 | + * |
| 70 | + * [1]: https://docs.gradle.com/develocity/gradle-plugin/current/#manual_access_key_configuration |
| 71 | + * [2]: https://docs.gradle.com/develocity/maven-extension/current/#manual_access_key_configuration |
| 72 | + * |
| 73 | + * @throws IllegalArgumentException if no matching key is found. |
54 | 74 | */
|
55 |
| - val apiToken: () -> String = { |
56 |
| - env["DEVELOCITY_API_TOKEN"] |
57 |
| - ?: error(ERROR_NULL_API_TOKEN) |
| 75 | + val accessKey: () -> String = { |
| 76 | + val host = URI(apiUrl).host |
| 77 | + requireNotNull(accessKeyResolver.resolve(host)) { ERROR_NULL_ACCESS_KEY } |
58 | 78 | },
|
59 | 79 |
|
60 | 80 | /**
|
@@ -216,6 +236,15 @@ data class Config(
|
216 | 236 | )
|
217 | 237 | }
|
218 | 238 |
|
| 239 | +private fun requireValidUrl(string: String) { |
| 240 | + requireNotNull(runCatching { URI(string) }.getOrNull()) { |
| 241 | + ERROR_MALFORMED_API_URL.format(string) |
| 242 | + } |
| 243 | +} |
| 244 | + |
219 | 245 | private const val ERROR_NULL_API_URL = "DEVELOCITY_API_URL is required"
|
220 |
| -private const val ERROR_NULL_API_TOKEN = "DEVELOCITY_API_TOKEN is required" |
| 246 | +private const val ERROR_MALFORMED_API_URL = "DEVELOCITY_API_URL contains a malformed URL: %s" |
| 247 | +private const val ERROR_NULL_ACCESS_KEY = "Develocity access key not found. " + |
| 248 | + "Please set DEVELOCITY_ACCESS_KEY='[host]=[accessKey]' or see Config.accessKey javadoc for " + |
| 249 | + "other supported options." |
221 | 250 | private const val ERROR_NULL_USER_HOME = "'user.home' system property must not be null"
|
0 commit comments