Skip to content

Commit

Permalink
set latest version, whether it's a pre-release or not
Browse files Browse the repository at this point in the history
  • Loading branch information
amvanbaren committed Feb 11, 2022
1 parent cd6f3e4 commit a6b8c1e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 39 deletions.
47 changes: 22 additions & 25 deletions server/src/main/java/org/eclipse/openvsx/ExtensionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.openvsx.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.util.Streamable;
import org.springframework.stereotype.Component;

@Component
Expand Down Expand Up @@ -207,10 +208,6 @@ private void storeResources(ExtensionVersion extVersion, List<FileResource> reso
public void updateExtension(Extension extension) {
extension.setLatest(getLatestVersion(extension, false));
extension.setLatestPreRelease(getLatestVersion(extension, true));
if (extension.getLatest() == null) {
// Use a pre-release version as latest if it's the only available version
extension.setLatest(extension.getLatestPreRelease());
}

if (extension.getLatest() != null) {
// There is at least one active version => activate the extension
Expand All @@ -223,17 +220,13 @@ public void updateExtension(Extension extension) {
}
}

private ExtensionVersion getLatestVersion(Extension extension, boolean preRelease) {
ExtensionVersion latest = null;
SemanticVersion latestSemver = null;
for (var extVer : repositories.findActiveVersions(extension, preRelease)) {
var semver = extVer.getSemanticVersion();
if (latestSemver == null || latestSemver.compareTo(semver) < 0) {
latest = extVer;
latestSemver = semver;
}
private ExtensionVersion getLatestVersion(Extension extension, boolean onlyPreRelease) {
var versions = repositories.findActiveVersions(extension);
if(onlyPreRelease) {
versions = versions.filter(ExtensionVersion::isPreRelease);
}
return latest;

return getLatestVersion(versions);
}

/**
Expand All @@ -244,10 +237,6 @@ private ExtensionVersion getLatestVersion(Extension extension, boolean preReleas
public void updateExtension(Extension extension, Iterable<ExtensionVersion> versions) {
extension.setLatest(getLatestVersion(versions, false));
extension.setLatestPreRelease(getLatestVersion(versions, true));
if (extension.getLatest() == null) {
// Use a pre-release version as latest if it's the only available version
extension.setLatest(extension.getLatestPreRelease());
}

if (extension.getLatest() != null) {
// There is at least one active version => activate the extension
Expand All @@ -260,18 +249,26 @@ public void updateExtension(Extension extension, Iterable<ExtensionVersion> vers
}
}

private ExtensionVersion getLatestVersion(Iterable<ExtensionVersion> versions, boolean preRelease) {
private ExtensionVersion getLatestVersion(Iterable<ExtensionVersion> versions, boolean onlyPreRelease) {
var filteredVersions = Streamable.of(versions).filter(ExtensionVersion::isActive);
if(onlyPreRelease) {
filteredVersions = filteredVersions.filter(ExtensionVersion::isPreRelease);
}

return getLatestVersion(filteredVersions);
}

private ExtensionVersion getLatestVersion(Streamable<ExtensionVersion> versions) {
ExtensionVersion latest = null;
SemanticVersion latestSemver = null;
for (var extVer : versions) {
if (extVer.isActive() && extVer.isPreRelease() == preRelease) {
var semver = extVer.getSemanticVersion();
if (latestSemver == null || latestSemver.compareTo(semver) < 0) {
latest = extVer;
latestSemver = semver;
}
var semver = extVer.getSemanticVersion();
if (latestSemver == null || latestSemver.compareTo(semver) < 0) {
latest = extVer;
latestSemver = semver;
}
}

return latest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public interface ExtensionVersionRepository extends Repository<ExtensionVersion,

Streamable<ExtensionVersion> findByExtensionAndActiveTrue(Extension extension);

Streamable<ExtensionVersion> findByExtensionAndPreReleaseAndActiveTrue(Extension extension, boolean preRelease);

ExtensionVersion findByVersionAndExtension(String version, Extension extension);

ExtensionVersion findByVersionAndExtensionNameIgnoreCaseAndExtensionNamespaceNameIgnoreCase(String version, String extensionName, String namespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ public Streamable<String> getActiveVersionStrings(Extension extension) {
return extensionVersionRepo.getActiveVersionStrings(extension);
}

public Streamable<ExtensionVersion> findActiveVersions(Extension extension, boolean preRelease) {
return extensionVersionRepo.findByExtensionAndPreReleaseAndActiveTrue(extension, preRelease);
}

public Streamable<ExtensionVersion> findBundledExtensionsReference(Extension extension) {
return extensionVersionRepo.findByBundledExtensions(extensionId(extension));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public SearchHits<ExtensionSearch> search(ISearchService.Options options, Pageab
// filter text
if (options.queryString != null) {
matchingExtensions = matchingExtensions.filter(extension ->
// var latest = extension.getLatest();
extension.getName().toLowerCase().contains(options.queryString.toLowerCase())
|| extension.getNamespace().getName().contains(options.queryString.toLowerCase())
|| (extension.getLatest().getDescription() != null && extension.getLatest().getDescription()
Expand Down
4 changes: 1 addition & 3 deletions server/src/test/java/org/eclipse/openvsx/AdminAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,8 @@ private List<ExtensionVersion> mockExtension(int numberOfVersions, int numberOfB
extension.setLatest(versions.get(versions.size() - 1));
Mockito.when(repositories.findVersions(extension))
.thenReturn(Streamable.of(versions));
Mockito.when(repositories.findActiveVersions(extension, false))
Mockito.when(repositories.findActiveVersions(extension))
.thenReturn(Streamable.of(versions));
Mockito.when(repositories.findActiveVersions(extension, true))
.thenReturn(Streamable.empty());
Mockito.when(repositories.getVersionStrings(extension))
.thenReturn(Streamable.of(versions).map(ev -> ev.getVersion()));
Mockito.when(repositories.getActiveVersionStrings(extension))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ private void mockForPublish(String mode) {
.thenReturn(0l);
Mockito.when(repositories.findVersions(any(Extension.class)))
.thenReturn(Streamable.empty());
Mockito.when(repositories.findActiveVersions(any(Extension.class), any(boolean.class)))
Mockito.when(repositories.findActiveVersions(any(Extension.class)))
.thenReturn(Streamable.empty());
Mockito.when(repositories.getVersionStrings(any(Extension.class)))
.thenReturn(Streamable.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,8 @@ public void testSignPublisherAgreementReactivateExtension() throws Exception {
extVersion.setExtension(extension);
Mockito.when(repositories.findVersionsByAccessToken(accessToken, false))
.thenReturn(Streamable.of(extVersion));
Mockito.when(repositories.findActiveVersions(extension, false))
Mockito.when(repositories.findActiveVersions(extension))
.thenReturn(Streamable.of(extVersion));
Mockito.when(repositories.findActiveVersions(extension, true))
.thenReturn(Streamable.empty());

eclipse.signPublisherAgreement(user);

Expand Down

0 comments on commit a6b8c1e

Please sign in to comment.