Skip to content

Commit

Permalink
Modify blocklist implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivassile committed Oct 19, 2023
1 parent 3227883 commit f99369c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
22 changes: 17 additions & 5 deletions core/src/main/java/org/wildfly/channel/ChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ Optional<ResolveLatestVersionResult> resolveLatestVersion(String groupId, String
requireNonNull(artifactId);
requireNonNull(resolver);

Set<String> blocklistedVersions = Collections.emptySet();
if (this.blocklist.isPresent()) {
blocklistedVersions = this.blocklist.get().getVersionsFor(groupId, artifactId);
}

// first we find if there is a stream for that given (groupId, artifactId).
Optional<Stream> foundStream = channelManifest.findStreamFor(groupId, artifactId);
// no stream for this artifact, let's look into the required channel
Expand All @@ -214,6 +219,7 @@ Optional<ResolveLatestVersionResult> resolveLatestVersion(String groupId, String
foundVersions.put(found.get().version, found.get().channel);
}
}
foundVersions.keySet().removeAll(blocklistedVersions);
Optional<String> foundVersionInRequiredChannels = foundVersions.keySet().stream().sorted(COMPARATOR.reversed()).findFirst();
if (foundVersionInRequiredChannels.isPresent()) {
return Optional.of(new ResolveLatestVersionResult(foundVersionInRequiredChannels.get(), foundVersions.get(foundVersionInRequiredChannels.get())));
Expand All @@ -223,6 +229,7 @@ Optional<ResolveLatestVersionResult> resolveLatestVersion(String groupId, String
switch (channelDefinition.getNoStreamStrategy()) {
case LATEST:
Set<String> versions = resolver.getAllVersions(groupId, artifactId, extension, classifier);
versions.removeAll(blocklistedVersions);
final Optional<String> latestVersion = versions.stream().sorted(COMPARATOR.reversed()).findFirst();
if (latestVersion.isPresent()) {
return Optional.of(new ResolveLatestVersionResult(latestVersion.get(), this));
Expand All @@ -231,9 +238,15 @@ Optional<ResolveLatestVersionResult> resolveLatestVersion(String groupId, String
}
case MAVEN_LATEST:
String latestMetadataVersion = resolver.getMetadataLatestVersion(groupId, artifactId);
if (blocklistedVersions.contains(latestMetadataVersion)) {
return Optional.empty();
}
return Optional.of(new ResolveLatestVersionResult(latestMetadataVersion, this));
case MAVEN_RELEASE:
String releaseMetadataVersion = resolver.getMetadataReleaseVersion(groupId, artifactId);
if (blocklistedVersions.contains(releaseMetadataVersion)) {
return Optional.empty();
}
return Optional.of(new ResolveLatestVersionResult(releaseMetadataVersion, this));
default:
return Optional.empty();
Expand All @@ -245,14 +258,13 @@ Optional<ResolveLatestVersionResult> resolveLatestVersion(String groupId, String
// there is a stream, let's now check its version
if (stream.getVersion() != null) {
foundVersion = Optional.of(stream.getVersion());
if (foundVersion.isPresent() && blocklistedVersions.contains(foundVersion.get())) {
return Optional.empty();
}
} else if (stream.getVersionPattern() != null) {
// if there is a version pattern, we resolve all versions from Maven to find the latest one
Set<String> versions = resolver.getAllVersions(groupId, artifactId, extension, classifier);
if (this.blocklist.isPresent()) {
final Set<String> blocklistedVersions = this.blocklist.get().getVersionsFor(groupId, artifactId);

versions.removeAll(blocklistedVersions);
}
versions.removeAll(blocklistedVersions);
foundVersion = foundStream.get().getVersionComparator().matches(versions);
}

Expand Down
6 changes: 4 additions & 2 deletions doc/examples/channel.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ If a new version `1.2.2.Final` is added to the repository, the Channel will reso

### Block versions

Blocklist allows to exclude a concrete version of an artifact from resolution while maintaining the "latest" resolution strategy. To block an artifact version we need to create a new file called `test-blocklist.yaml`:
Blocklist allows to exclude a concrete version of an artifact from resolution while maintaining the "latest" resolution strategy. If the resolution strategy is "maven-latest" or "maven-release" and that version is in the blocklist, this will cause the artifact to be removed from the channel. To block an artifact version we need to create a new file called `test-blocklist.yaml`:

[source, yaml, title="test-blocklist.yaml"]
----
Expand All @@ -100,7 +100,7 @@ name: "test-blocklist"
blocks:
- groupId: "org.test"
artifactId: "artifact-three" #<1>
versions: #<1>
versions: #<2>
- "1.0.1.Final"
...
----
Expand Down Expand Up @@ -134,6 +134,8 @@ Let's say the Maven repositories currently contain versions 1.0.0.Final and 1.0.

When a new version, `1.0.3.Final`, is made available, the channel will instead resolve that version and the blocklist will have no effect.

If the Maven repositories contain only version 1.0.1.Final of `org.test.artifact-three`, the artifact will be removed from the channel because this version is in the blocklist.

## Fix manifest and blocklist versions

So far the channel has been using the latest available versions of manifest and blocklist. If required this can be changed to either use a specific Maven version or a file URL:
Expand Down
5 changes: 1 addition & 4 deletions doc/spec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ A blocklist applies only to the channel that defined it, not its required channe
#### Resolving artifact version
During artifact version resolution, a stream matching artifact GA is located in the channel. If the stream uses concrete versions, that version of the artifact is resolved and returned to the user.
If the stream uses `versionPattern`, the blocklist is checked for excluded versions. The excluded versions are removed from the set of available artifact versions before the latest remaining version matching the stream’s pattern is used to resolve the artifact.
If the blocklist excludes all available artifact versions, `UnresolvedMavenArtifactException` is thrown.
The blocklist is ignored when using `resolveDirectMavenArtifact` method.
During artifact version resolution, a stream matching artifact GA is located in the channel. The blocklist is always checked for excluded versions, except when using `resolveDirectMavenArtifact` method. The excluded versions are removed from the set of available artifact versions before the latest remaining version matching the stream’s pattern is used to resolve the artifact. If the blocklist excludes all available artifact versions, `UnresolvedMavenArtifactException` is thrown.
### Changelog
Expand Down

0 comments on commit f99369c

Please sign in to comment.