Skip to content

Commit

Permalink
Merge pull request #49 from spyrkob/use_logical_version
Browse files Browse the repository at this point in the history
Use logicalVersion field to read the manifest version information if available
  • Loading branch information
spyrkob authored Oct 16, 2024
2 parents eb42fb2 + 95458c1 commit f8116c9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public static ManifestVersionRecord getCurrentVersions(ChannelSession session) t
record.addManifest(new ManifestVersionRecord.NoManifest(repos, channelDefinition.getNoStreamStrategy().toString()));
} else if (manifestCoordinate.getUrl() != null) {
String hashCode = HashUtils.hash(read(manifestCoordinate.getUrl()));
record.addManifest(new ManifestVersionRecord.UrlManifest(manifestCoordinate.getUrl().toExternalForm(), hashCode, manifest.getDescription()));
record.addManifest(new ManifestVersionRecord.UrlManifest(manifestCoordinate.getUrl().toExternalForm(), hashCode, getVersionDescription(manifest)));
} else if (manifestCoordinate.getMaven() != null) {
final String description = manifest.getDescription();
final String description = getVersionDescription(manifest);
record.addManifest(new ManifestVersionRecord.MavenManifest(
manifestCoordinate.getGroupId(),
manifestCoordinate.getArtifactId(),
Expand Down Expand Up @@ -163,18 +163,30 @@ public ManifestVersionRecord getCurrentVersions(List<Channel> channels) throws I
return manifestVersionRecord;
}

private String getManifestDescription(ChannelManifestCoordinate manifestCoordinate, MavenVersionsResolver mavenVersionsResolver) {
private static String getManifestDescription(ChannelManifestCoordinate manifestCoordinate, MavenVersionsResolver mavenVersionsResolver) {
final List<URL> urls = mavenVersionsResolver.resolveChannelMetadata(List.of(manifestCoordinate));
final String description;
if (!urls.isEmpty()) {
final ChannelManifest manifest = ChannelManifestMapper.from(urls.get(0));
description = manifest.getName();
description = getVersionDescription(manifest);
} else {
description = null;
}
return description;
}

private static String getVersionDescription(ChannelManifest manifest) {
final String description;
if (manifest.getSchemaVersion().equals(ChannelManifestMapper.SCHEMA_VERSION_1_0_0)) {
// before manifest v1.1.0, we had a convention of using "name" as version description
// after "logicalVersion" was introduced, this was abandoned
description = manifest.getName();
} else {
description = manifest.getLogicalVersion();
}
return description;
}

private static String read(URL url) throws IOException {
try(InputStream inputStream = url.openStream();
OutputStream outputStream = new ByteArrayOutputStream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void getVersionOfLatestMavenManifest() throws Exception {

final File manifestFile = temp.newFile();
final URL url = manifestFile.toURI().toURL();
Files.writeString(manifestFile.toPath(), ChannelManifestMapper.toYaml(new ChannelManifest("test", null, null, Collections.emptyList())));
Files.writeString(manifestFile.toPath(), ChannelManifestMapper.toYaml(new ChannelManifest("1.0.0", "test", null, null, null, null, Collections.emptyList())));

when(mavenResolver.getAllVersions("org.test", "test", "yaml", "manifest"))
.thenReturn(Set.of("1.0.0", "1.0.1"));
Expand All @@ -90,6 +90,33 @@ public void getVersionOfLatestMavenManifest() throws Exception {
assertThat(currentVersions.getUrlManifests()).isEmpty();
}

@Test
public void getVersionOfLatestMavenManifestWithLogicalVersion() throws Exception {
final ManifestVersionResolver resolver = new ManifestVersionResolver(factory);

final File manifestFile = temp.newFile();
final URL url = manifestFile.toURI().toURL();
Files.writeString(manifestFile.toPath(), ChannelManifestMapper.toYaml(new ChannelManifest("1.1.0", "test", null, "logical-version", null, null, Collections.emptyList())));

when(mavenResolver.getAllVersions("org.test", "test", "yaml", "manifest"))
.thenReturn(Set.of("1.0.0", "1.0.1"));
when(mavenResolver.resolveChannelMetadata(any()))
.thenReturn(List.of(url));

final Channel channel = new Channel("test", "", null, Collections.emptyList(),
new ChannelManifestCoordinate("org.test", "test"), null, null);
final ManifestVersionRecord currentVersions = resolver.getCurrentVersions(List.of(channel));

assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getVersion)
.containsExactly("1.0.1");
assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getDescription)
.containsExactly("logical-version");
assertThat(currentVersions.getOpenManifests()).isEmpty();
assertThat(currentVersions.getUrlManifests()).isEmpty();
}

@Test
public void getHardcodedVersionOfManifestIfPresent() throws Exception {
final ManifestVersionResolver resolver = new ManifestVersionResolver(factory);
Expand Down Expand Up @@ -198,8 +225,12 @@ public void channelSessionToVersionRecord() throws Exception {
.addRepository("test-repo", "http://test.te")
.build();

final ChannelManifest manifest1 = new ChannelManifest.Builder().setDescription("Manifest 1").build();
final ChannelManifest manifest2 = new ChannelManifest.Builder().setDescription("Manifest 2").build();
final ChannelManifest manifest1 = new ChannelManifest.Builder()
.setSchemaVersion(ChannelManifestMapper.SCHEMA_VERSION_1_0_0)
.setName("Manifest 1").build();
final ChannelManifest manifest2 = new ChannelManifest.Builder()
.setSchemaVersion(ChannelManifestMapper.SCHEMA_VERSION_1_0_0)
.setName("Manifest 2").build();

final List<RuntimeChannel> channels = List.of(
new RuntimeChannel(channel1, manifest1, null),
Expand Down

0 comments on commit f8116c9

Please sign in to comment.