Skip to content

Commit 9a129c8

Browse files
committed
Make sure default platforms recommended by registries have higher preferrences than those referenced from downstream platforms
1 parent 2a82f2b commit 9a129c8

File tree

2 files changed

+156
-26
lines changed

2 files changed

+156
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.quarkus.devtools.project.create;
2+
3+
import java.nio.file.Path;
4+
import java.util.List;
5+
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
9+
import io.quarkus.devtools.testing.registry.client.TestRegistryClientBuilder;
10+
import io.quarkus.maven.dependency.ArtifactCoords;
11+
12+
public class QuarkusPlatformBasedOnArchivedQuarkusCoreTest extends MultiplePlatformBomsTestBase {
13+
14+
private static final String ACME_PLATFORM_KEY = "org.acme.platform";
15+
private static final String OTHER_PLATFORM_KEY = "org.other.platform";
16+
17+
@BeforeAll
18+
public static void setup() throws Exception {
19+
TestRegistryClientBuilder.newInstance()
20+
//.debug()
21+
.baseDir(configDir())
22+
// registry
23+
.newRegistry("registry.acme.org")
24+
// platform key
25+
.newPlatform(ACME_PLATFORM_KEY)
26+
// 3.0 STREAM
27+
.newStream("5.0")
28+
// 3.0.5 release
29+
.newRelease("5.0.5")
30+
.quarkusVersion("3.0.5")
31+
.newMember("acme-zoo-bom").addExtension("org.acme", "acme-rabbit", "5.0.5")
32+
.release().stream().platform()
33+
// not promoted later release
34+
.newArchivedStream("6.0")
35+
.newRelease("6.0.0")
36+
.quarkusVersion("4.0.0")
37+
.newMember("acme-zoo-bom").addExtension("org.acme", "acme-rabbit", "6.0.0")
38+
.release().stream().platform()
39+
.registry().clientBuilder()
40+
// Other registry
41+
.newRegistry("registry.other.org")
42+
.newPlatform(OTHER_PLATFORM_KEY)
43+
.newStream("4.0")
44+
.newRelease("4.0.0")
45+
.quarkusVersion("4.0.0")
46+
.addCoreMember()
47+
.addExtension("quarkus-rest")
48+
.addExtension("quarkus-magic")
49+
.release().stream().platform()
50+
// 3.0 STREAM
51+
.newArchivedStream("3.0")
52+
.newRelease("3.0.5")
53+
.quarkusVersion("3.0.5")
54+
.addCoreMember()
55+
.addExtension("quarkus-rest")
56+
.addExtension("quarkus-magic")
57+
.registry()
58+
.clientBuilder()
59+
.build();
60+
61+
enableRegistryClient();
62+
}
63+
64+
protected String getMainPlatformKey() {
65+
return OTHER_PLATFORM_KEY;
66+
}
67+
68+
@Test
69+
public void testLatestRecommendedQuarkusCore() throws Exception {
70+
final Path projectDir = newProjectDir("latest-other-platform");
71+
createProject(projectDir, List.of("quarkus-rest"));
72+
73+
assertModel(projectDir,
74+
List.of(mainPlatformBom()),
75+
List.of(ArtifactCoords.jar(OTHER_PLATFORM_KEY, "quarkus-rest", null)),
76+
"4.0.0");
77+
}
78+
79+
@Test
80+
public void testLatestRecommendedAcmeRabbit() throws Exception {
81+
final Path projectDir = newProjectDir("latest-acme-platform");
82+
createProject(projectDir, List.of("rabbit"));
83+
84+
assertModel(projectDir,
85+
List.of(mainPlatformBom(), ArtifactCoords.pom(ACME_PLATFORM_KEY, "acme-zoo-bom", "5.0.5")),
86+
List.of(ArtifactCoords.jar("org.acme", "acme-rabbit", null)),
87+
"3.0.5");
88+
}
89+
90+
@Test
91+
public void testLatestRecommendedQuarkusCoreAndAcmeRabbitCombination() throws Exception {
92+
final Path projectDir = newProjectDir("latest-acme-platform");
93+
createProject(projectDir, List.of("rabbit", "quarkus-rest", "quarkus-magic"));
94+
95+
assertModel(projectDir,
96+
List.of(mainPlatformBom(), ArtifactCoords.pom(ACME_PLATFORM_KEY, "acme-zoo-bom", "5.0.5")),
97+
List.of(ArtifactCoords.jar("org.acme", "acme-rabbit", null),
98+
ArtifactCoords.jar(OTHER_PLATFORM_KEY, "quarkus-rest", null),
99+
ArtifactCoords.jar(OTHER_PLATFORM_KEY, "quarkus-magic", null)),
100+
"3.0.5");
101+
}
102+
}

independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/ExtensionCatalogResolver.java

+54-26
Original file line numberDiff line numberDiff line change
@@ -404,26 +404,10 @@ private void collectPlatformExtensions(final ExtensionCatalogBuilder catalogBuil
404404
throws RegistryResolutionException {
405405
final RegistryExtensionResolver registry = registries.get(registryIndex);
406406

407-
final List<PlatformCatalog> downstreamPreferences = new ArrayList<>(catalogBuilder.upstreamQuarkusVersions.size());
408-
for (String quarkusVersion : catalogBuilder.upstreamQuarkusVersions) {
409-
if (!registry.isAcceptsQuarkusVersionQueries(quarkusVersion)) {
410-
continue;
411-
}
412-
final PlatformCatalog pc = registry.resolvePlatformCatalog(quarkusVersion);
413-
if (pc == null) {
414-
continue;
415-
}
416-
downstreamPreferences.add(pc);
417-
}
418-
419-
PlatformCatalog pc = registry.resolvePlatformCatalog();
420-
if (pc == null && downstreamPreferences.isEmpty()) {
407+
final PlatformCatalog pc = resolvePlatformCatalog(registry, catalogBuilder.upstreamQuarkusVersions);
408+
if (pc == null) {
421409
return;
422410
}
423-
if (!downstreamPreferences.isEmpty()) {
424-
downstreamPreferences.add(pc);
425-
pc = CatalogMergeUtility.mergePlatformCatalogs(downstreamPreferences);
426-
}
427411

428412
int platformIndex = 0;
429413
for (Platform platform : pc.getPlatforms()) {
@@ -446,7 +430,7 @@ private void collectPlatformExtensions(final ExtensionCatalogBuilder catalogBuil
446430
int memberIndex = 0;
447431
for (ArtifactCoords bom : release.getMemberBoms()) {
448432
memberIndex++;
449-
final ExtensionCatalog.Mutable ec = (ExtensionCatalog.Mutable) registry.resolvePlatformExtensions(bom);
433+
final ExtensionCatalog.Mutable ec = registry.resolvePlatformExtensions(bom);
450434
if (ec != null) {
451435
final OriginPreference originPreference = new OriginPreference(registryIndex, platformIndex,
452436
releaseIndex, memberIndex, compatiblityCode);
@@ -461,9 +445,53 @@ private void collectPlatformExtensions(final ExtensionCatalogBuilder catalogBuil
461445
}
462446
}
463447

464-
private void addOriginPreference(final ExtensionCatalog.Mutable ec, OriginPreference originPreference) {
465-
Map<String, Object> metadata = ec.getMetadata();
466-
metadata.put("origin-preference", originPreference);
448+
/**
449+
* Resolves a platform catalog from a given registry. The method may return null in case the registry does not
450+
* provide platforms.
451+
*
452+
* <p>
453+
* It starts by resolving the default platform catalog. If a list of Quarkus core versions is provided,
454+
* it will also resolve platform catalogs for each Quarkus core version (if the registry recognizes
455+
* those Quarkus core versions), merge all the resolved platform catalogs into a single one and return the result.
456+
*
457+
* @param registry the registry to resolve the catalogs from
458+
* @param quarkusVersions optional extra Quarkus core versions
459+
* @return platform catalog or null if the registry does not provide any platforms
460+
* @throws RegistryResolutionException in case a registry querying failed or some other error happened
461+
*/
462+
private static PlatformCatalog resolvePlatformCatalog(RegistryExtensionResolver registry, List<String> quarkusVersions)
463+
throws RegistryResolutionException {
464+
// default registry recommendations
465+
PlatformCatalog defaultCatalog = registry.resolvePlatformCatalog();
466+
if (quarkusVersions.isEmpty()) {
467+
return defaultCatalog;
468+
}
469+
470+
List<PlatformCatalog> catalogsToMerge = List.of();
471+
for (int i = 0; i < quarkusVersions.size(); ++i) {
472+
var quarkusVersion = quarkusVersions.get(i);
473+
if (registry.isAcceptsQuarkusVersionQueries(quarkusVersion)) {
474+
final PlatformCatalog pcForQuarkusVersion = registry.resolvePlatformCatalog(quarkusVersion);
475+
if (pcForQuarkusVersion != null) {
476+
if (catalogsToMerge.isEmpty()) {
477+
catalogsToMerge = new ArrayList<>(quarkusVersions.size() - i + 1);
478+
if (defaultCatalog != null) {
479+
catalogsToMerge.add(defaultCatalog);
480+
}
481+
}
482+
catalogsToMerge.add(pcForQuarkusVersion);
483+
}
484+
}
485+
}
486+
487+
if (catalogsToMerge.isEmpty()) {
488+
return defaultCatalog;
489+
}
490+
return CatalogMergeUtility.mergePlatformCatalogs(catalogsToMerge);
491+
}
492+
493+
private static void addOriginPreference(final ExtensionCatalog.Mutable ec, OriginPreference originPreference) {
494+
ec.getMetadata().put("origin-preference", originPreference);
467495
}
468496

469497
public ExtensionCatalog resolveExtensionCatalog(String quarkusCoreVersion) throws RegistryResolutionException {
@@ -740,15 +768,15 @@ private void ensureRegistriesConfigured() throws RegistryResolutionException {
740768
}
741769
}
742770

743-
private void appendNonPlatformExtensions(
771+
private static void appendNonPlatformExtensions(
744772
ExtensionCatalogBuilder catalogBuilder,
745773
String quarkusVersion) throws RegistryResolutionException {
746774
for (RegistryExtensionResolver registry : catalogBuilder.getRegistriesForQuarkusCore(quarkusVersion)) {
747775
appendNonPlatformExtensions(registry, catalogBuilder, quarkusVersion);
748776
}
749777
}
750778

751-
private void appendNonPlatformExtensions(RegistryExtensionResolver registry, ExtensionCatalogBuilder catalogBuilder,
779+
private static void appendNonPlatformExtensions(RegistryExtensionResolver registry, ExtensionCatalogBuilder catalogBuilder,
752780
String quarkusVersion) throws RegistryResolutionException {
753781
final ExtensionCatalog.Mutable nonPlatformCatalog = registry.resolveNonPlatformExtensions(quarkusVersion);
754782
if (nonPlatformCatalog == null) {
@@ -777,7 +805,7 @@ private int getRegistryIndex(String registryId) {
777805
throw new IllegalStateException(buf.toString());
778806
}
779807

780-
private void collectPlatformExtensions(String quarkusCoreVersion, ExtensionCatalogBuilder catalogBuilder,
808+
private static void collectPlatformExtensions(String quarkusCoreVersion, ExtensionCatalogBuilder catalogBuilder,
781809
Set<String> processedPlatformKeys)
782810
throws RegistryResolutionException {
783811
final List<RegistryExtensionResolver> quarkusVersionRegistries = catalogBuilder
@@ -803,7 +831,7 @@ private void collectPlatformExtensions(String quarkusCoreVersion, ExtensionCatal
803831
}
804832
}
805833

806-
private void collectPlatformExtensions(ExtensionCatalogBuilder catalogBuilder,
834+
private static void collectPlatformExtensions(ExtensionCatalogBuilder catalogBuilder,
807835
RegistryExtensionResolver registry, int platformIndex,
808836
Platform p) throws RegistryResolutionException {
809837

0 commit comments

Comments
 (0)