Skip to content

Commit 67211e5

Browse files
authored
Rename env var for API URL (#13)
The current name conflicts with the one used in https://github.com/gradle/common-custom-user-data-gradle-plugin. While they could remain the same, it's more flexible to use a different variable.
1 parent 7ad783b commit 67211e5

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Options.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ class Options internal constructor(
3535
) {
3636

3737
/**
38-
* Provides the URL of a Gradle Enterprise API instance (without `/api`). By default, uses
39-
* environment variable `GRADLE_ENTERPRISE_URL`.
38+
* Provides the URL of a Gradle Enterprise API instance REST API. By default, uses
39+
* environment variable `GRADLE_ENTERPRISE_API_URL`. Must end with `/api/`.
4040
*/
4141
var url: () -> String = {
42-
env["GRADLE_ENTERPRISE_URL"]
43-
?: error("GE instance URL is required")
42+
env["GRADLE_ENTERPRISE_API_URL"]
43+
?: error("GRADLE_ENTERPRISE_API_URL is required")
4444
}
4545

4646
/**
4747
* Provides the access token for a Gradle Enterprise API instance. By default, uses keychain entry
48-
* `gradle-enterprise-api-token` or environment variable `GRADLE_ENTERPRISE_URL`.
48+
* `gradle-enterprise-api-token` or environment variable `GRADLE_ENTERPRISE_API_TOKEN`.
4949
*/
5050
var token: () -> String = {
5151
keychain["gradle-enterprise-api-token"]
5252
?: env["GRADLE_ENTERPRISE_API_TOKEN"]
53-
?: error("GE token is required")
53+
?: error("GRADLE_ENTERPRISE_API_TOKEN is required")
5454
}
5555
}
5656

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/Retrofit.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ internal fun buildRetrofit(
2222
client: OkHttpClient,
2323
moshi: Moshi,
2424
) = with(Retrofit.Builder()) {
25-
val url = options.gradleEnterpriseInstance.url()
26-
check("/api" !in url) { "Instance URL must be the plain instance URL, without /api" }
27-
baseUrl(url)
25+
val apiUrl = options.gradleEnterpriseInstance.url()
26+
check("/api/" in apiUrl) { "A valid API URL must end in /api/" }
27+
val instanceUrl = apiUrl.substringBefore("api/")
28+
baseUrl(instanceUrl)
2829
addConverterFactory(ScalarsConverterFactory.create())
2930
addConverterFactory(MoshiConverterFactory.create(moshi))
3031
client(client)

src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OptionsTest.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ import kotlin.test.assertTrue
1212
class OptionsTest {
1313

1414
@Test
15-
fun `URL from env is required`() {
15+
fun `Given no URL set in env, url() fails`() {
1616
val options = Options(FakeEnv(), FakeKeychain())
1717
assertFails {
1818
options.gradleEnterpriseInstance.url()
1919
}
2020
}
2121

22+
@Test
23+
fun `Given URL set in env, url() returns env URL`() {
24+
val options = Options(
25+
FakeEnv("GRADLE_ENTERPRISE_API_URL" to "https://example.com/api/"),
26+
FakeKeychain(),
27+
)
28+
assertEquals("https://example.com/api/", options.gradleEnterpriseInstance.url())
29+
}
30+
2231
@Test
2332
fun `Token from keychain is preferred`() {
2433
val options = Options(

src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/RetrofitTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import kotlin.test.*
1515
class RetrofitTest {
1616

1717
@Test
18-
fun `Sets URL from options`() {
18+
fun `Sets instance URL from options, stripping api segment`() {
1919
val retrofit = buildRetrofit(
20-
"GRADLE_ENTERPRISE_URL" to "https://example.com/",
20+
"GRADLE_ENTERPRISE_API_URL" to "https://example.com/api/",
2121
)
22+
// That's what generated classes expect
2223
assertEquals("https://example.com/", retrofit.baseUrl().toString())
2324
}
2425

2526
@Test
2627
fun `Rejects invalid URL`() {
2728
assertFails {
2829
buildRetrofit(
29-
"GRADLE_ENTERPRISE_URL" to "https://example.com/api/",
30+
"GRADLE_ENTERPRISE_API_URL" to "https://example.com/",
3031
)
3132
}
3233
}
@@ -37,8 +38,6 @@ class RetrofitTest {
3738
val env = FakeEnv(*envVars)
3839
if ("GRADLE_ENTERPRISE_API_TOKEN" !in env)
3940
env["GRADLE_ENTERPRISE_API_TOKEN"] = "example-token"
40-
if ("GRADLE_ENTERPRISE_URL" !in env)
41-
env["GRADLE_ENTERPRISE_URL"] = "example-url"
4241
val options = Options(env, FakeKeychain())
4342
return buildRetrofit(
4443
options = options,

0 commit comments

Comments
 (0)