Skip to content

Commit 7f8f96c

Browse files
committed
Allow configuring cacheable URLs from env
1 parent e6cbfb6 commit 7f8f96c

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("unused")
2+
13
package com.gabrielfeo.gradle.enterprise.api
24

35
import com.gabrielfeo.gradle.enterprise.api.internal.*
@@ -40,17 +42,19 @@ fun shutdown() {
4042
}
4143

4244
/**
43-
* List of patterns matching API URLs that are OK to store in the HTTP cache. Matches by default:
45+
* Regex pattern to match API URLs that are OK to store in the HTTP cache. Matches by default:
4446
* - {host}/api/builds/{id}/gradle-attributes
4547
* - {host}/api/builds/{id}/maven-attributes
4648
*
47-
* By default, the Gradle Enterprise disallows HTTP caching via response headers. This library
49+
* By default, the Gradle Enterprise API disallows HTTP caching via response headers. This library
4850
* removes such headers to forcefully allow caching, if the path is matched by any of these
4951
* patterns.
52+
*
53+
* Use `|` to define multiple patterns in one, e.g. `.*gradle-attributes|.*test-distribution`.
5054
*/
51-
val cacheablePaths: MutableList<Regex> = mutableListOf(
52-
""".*/api/builds/[\d\w]+/(?:gradle|maven)-attributes""".toRegex(),
53-
)
55+
val cacheableUrlPattern: Regex = System.getenv("GRADLE_ENTERPRISE_API_CACHEABLE_URL_PATTERN")
56+
?.toRegex()
57+
?: """.*/api/builds/[\d\w]+/(?:gradle|maven)-attributes""".toRegex()
5458

5559
/**
5660
* Maximum amount of concurrent requests allowed. Further requests will be queued. By default,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.gabrielfeo.gradle.enterprise.api.internal
22

33
import com.gabrielfeo.gradle.enterprise.api.accessToken
44
import com.gabrielfeo.gradle.enterprise.api.auth.HttpBearerAuth
5-
import com.gabrielfeo.gradle.enterprise.api.cacheablePaths
5+
import com.gabrielfeo.gradle.enterprise.api.cacheableUrlPattern
66
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheEnforcingInterceptor
77
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheHitLoggingInterceptor
88
import com.gabrielfeo.gradle.enterprise.api.internal.caching.cache
@@ -14,7 +14,7 @@ val okHttpClient: OkHttpClient by lazy {
1414
.cache(cache)
1515
.addInterceptor(HttpBearerAuth("bearer", accessToken()))
1616
.addInterceptor(CacheHitLoggingInterceptor())
17-
.addNetworkInterceptor(CacheEnforcingInterceptor(cacheablePaths))
17+
.addNetworkInterceptor(CacheEnforcingInterceptor(cacheableUrlPattern))
1818
.build()
1919
.apply {
2020
dispatcher.maxRequests = maxConcurrentRequests

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import okhttp3.Response
66
import kotlin.time.Duration.Companion.days
77

88
internal class CacheEnforcingInterceptor(
9-
private val cacheablePaths: List<Regex>,
9+
private val cacheableUrlPattern: Regex,
1010
) : Interceptor {
1111

1212
private val maxAge = 365.days.inWholeMilliseconds
@@ -25,5 +25,5 @@ internal class CacheEnforcingInterceptor(
2525
}
2626

2727
private fun isCacheable(request: Request) =
28-
cacheablePaths.any { it.matches(request.url.toString()) }
28+
cacheableUrlPattern.matches(request.url.toString())
2929
}

0 commit comments

Comments
 (0)