Skip to content
Draft
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 @@ -18,9 +18,14 @@ import org.eclipse.aether.transport.http.HttpTransporterFactory
import org.eclipse.aether.version.Version
import org.gradle.api.GradleException
import org.gradle.api.logging.Logging
import java.net.URI
import java.nio.file.Files

internal object MuzzleMavenRepoUtils {
private const val DEPOT_JAVA_HOST = "depot-read-api-java.us1.ddbuild.io"
private const val DEPOT_JAVA_FABRIC_URL =
"https://depot-read-api-java.rapid-dependency-management-depot.all-clusters.local-dc.fabric.dog:8443/" +
"magicmirror/magicmirror/@current/"
Comment on lines +25 to +28

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: I'd rather not have depot URL here, in constants, they should be configurable, I'll look into that in the mirror PR.

private val log = Logging.getLogger(MuzzleMavenRepoUtils::class.java)
private val backoffDelaysSeconds = listOf(5L, 10L, 30L)

Expand All @@ -31,18 +36,32 @@ internal object MuzzleMavenRepoUtils {
* be reused across builds with different MAVEN_REPOSITORY_PROXY values.
*/
@JvmStatic
fun defaultMuzzleRepos(): List<RemoteRepository> {
fun defaultMuzzleRepos(): List<RemoteRepository> =
defaultMuzzleRepos(System.getenv("MAVEN_REPOSITORY_PROXY"))

internal fun defaultMuzzleRepos(mavenProxyUrl: String?): List<RemoteRepository> {
val central = RemoteRepository.Builder("central", "default", "https://repo1.maven.org/maven2/").build()
val mavenProxyUrl = System.getenv("MAVEN_REPOSITORY_PROXY")
return if (mavenProxyUrl == null) {
listOf(central)
} else {
val proxy = RemoteRepository.Builder("central-proxy", "default", mavenProxyUrl).build()
// TODO: temporary hack for Maven Central rate limiting
listOf(proxy /*, central*/)
if (isDepotJavaProxy(mavenProxyUrl)) {
val fabricProxy = RemoteRepository.Builder(
"central-proxy-fabric",
"default",
DEPOT_JAVA_FABRIC_URL
).build()
listOf(fabricProxy, proxy /*, central*/)
} else {
listOf(proxy /*, central*/)
}
}
}

private fun isDepotJavaProxy(url: String): Boolean =
runCatching { URI(url).host.equals(DEPOT_JAVA_HOST, ignoreCase = true) }.getOrDefault(false)

/**
* Create new RepositorySystem for muzzle's Maven/Aether resolutions.
* Supports both HTTP/HTTPS and file:// repositories.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import org.eclipse.aether.util.version.GenericVersionScheme
import org.gradle.api.GradleException
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
Expand All @@ -24,6 +22,11 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy

private const val MAVEN_CENTRAL_URL = "https://repo1.maven.org/maven2/"
private const val DEPOT_JAVA_URL =
"https://depot-read-api-java.us1.ddbuild.io/magicmirror/magicmirror/@current/"
private const val DEPOT_JAVA_FABRIC_URL =
"https://depot-read-api-java.rapid-dependency-management-depot.all-clusters.local-dc.fabric.dog:8443/" +
"magicmirror/magicmirror/@current/"
Comment on lines +25 to +29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: I'd rather not have depot URL here, in constants, they should be configurable, I'll look into that in the mirror PR.


class MuzzleMavenRepoUtilsTest {

Expand Down Expand Up @@ -115,31 +118,27 @@ class MuzzleMavenRepoUtilsTest {
.hasMessageContaining("Backoff:\n disabled")
}

// The two tests below are mutually exclusive: MAVEN_REPOSITORY_PROXY is read from the real
// environment (defaultMuzzleRepos deliberately does not take it as a parameter), so each of
// them covers the branch its environment can reach -- unset locally, set in CI.

@Test
@DisabledIfEnvironmentVariable(
named = "MAVEN_REPOSITORY_PROXY",
matches = ".*",
disabledReason = "A mirror is configured; the proxy variant of this test covers that case"
)
fun `defaultMuzzleRepos is Maven Central alone when no proxy is configured`() {
assertThat(MuzzleMavenRepoUtils.defaultMuzzleRepos().map { it.id to it.url })
assertThat(MuzzleMavenRepoUtils.defaultMuzzleRepos(null).map { it.id to it.url })
.containsExactly("central" to MAVEN_CENTRAL_URL)
}

// TODO: Re-enable after removing the temporary Maven Central rate limiting workaround.
@Test
@Disabled("Temporarily using the configured proxy without a Maven Central fallback")
@EnabledIfEnvironmentVariable(named = "MAVEN_REPOSITORY_PROXY", matches = ".*")
fun `defaultMuzzleRepos queries the configured proxy before Maven Central`() {
val proxyUrl = System.getenv("MAVEN_REPOSITORY_PROXY")

// Central stays in the list as a fallback, but the proxy is consulted first.
assertThat(MuzzleMavenRepoUtils.defaultMuzzleRepos().map { it.id to it.url })
.containsExactly("central-proxy" to proxyUrl, "central" to MAVEN_CENTRAL_URL)
fun `defaultMuzzleRepos queries Depot through Fabric before the public endpoint`() {
assertThat(MuzzleMavenRepoUtils.defaultMuzzleRepos(DEPOT_JAVA_URL).map { it.id to it.url })
.containsExactly(
"central-proxy-fabric" to DEPOT_JAVA_FABRIC_URL,
"central-proxy" to DEPOT_JAVA_URL
)
}

@Test
fun `defaultMuzzleRepos does not prepend Fabric to a custom proxy`() {
val customProxy = "https://maven.example.com/repository/"

assertThat(MuzzleMavenRepoUtils.defaultMuzzleRepos(customProxy).map { it.id to it.url })
.containsExactly("central-proxy" to customProxy)
}

@Test
Expand Down
Loading