Skip to content

Commit

Permalink
more bugfixes to version number retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
toby1984 committed Jun 21, 2024
1 parent 049f054 commit 5d0db14
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static UpdateAvailable fromString(String s)

public Artifact artifact;
public Version currentVersion;
// TODO: Rename to 'latestReleasedVersion' version, snapshots are not considered
public Version latestVersion;
public UpdateAvailable updateAvailable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public Statistics createCopy() {
*/
void resetStatistics();

void setBlacklist(Blacklist blacklist);

/**
* Try to update version information.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,15 @@ public VersionInfo(VersionInfo other)
this.lastRepositoryUpdate = other.lastRepositoryUpdate;
this.lastRequestDate = other.lastRequestDate;
}


public Optional<Boolean> hasReleaseDate(Version versionNumber) {
return hasReleaseDate( versionNumber.versionString );
}

public Optional<Boolean> hasReleaseDate(String versionNumber) {
return getVersion(versionNumber).map( Version::hasReleaseDate );
}

public Optional<Version> getVersion(String versionNumber)
{
// TODO: O(n) performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import de.codesourcery.versiontracker.common.Artifact;
import de.codesourcery.versiontracker.common.ArtifactResponse;
import de.codesourcery.versiontracker.common.ArtifactResponse.UpdateAvailable;
import de.codesourcery.versiontracker.common.Blacklist;
import de.codesourcery.versiontracker.common.IVersionProvider;
import de.codesourcery.versiontracker.common.IVersionStorage;
import de.codesourcery.versiontracker.common.QueryRequest;
Expand Down Expand Up @@ -160,7 +161,19 @@ protected IVersionStorage createVersionStorage()

// unit-testing hook
protected IVersionProvider createVersionProvider() {
return new MavenCentralVersionProvider( repo1BaseUrl, restApiBaseUrl );

final Blacklist bl = new Blacklist();
final String v = System.getProperty( "versionTracker.blacklistedGroupIds" );
if ( v != null ) {
final String[] ids = v.split(",");
for ( final String id : ids ) {
final String groupId = id.trim();
bl.addIgnoredVersion( groupId, ".*", Blacklist.VersionMatcher.REGEX );
}
}
final MavenCentralVersionProvider result = new MavenCentralVersionProvider( repo1BaseUrl, restApiBaseUrl );
result.setBlacklist( bl );
return result;
}

private static Optional<Duration> getDurationFromSystemProperties(String key) {
Expand Down Expand Up @@ -345,6 +358,16 @@ public QueryResponse processQuery(QueryRequest request) throws InterruptedExcept
if ( info.hasVersions() )
{
if ( artifact.hasReleaseVersion() ) {
// FIXME: Problem here is that versionTracker.getVersionInfo()
// will only retrieve the release dates for the *TRULY* latest release/snapshot versions
// (as according to the Maven Indexer XML file) as it doesn't know about any blacklists
// that a client is using.
// In this case, the release date of that specific version may very well not have been fetched
// and hence the enforcer rule can never perform an age comparison since the release date
// always stays unknown.
//
// TODO: Trigger an MavenCentralProvider#update() call just with that version number

x.latestVersion = info.findLatestReleaseVersion( request.blacklist ).orElse( null );
if ( LOG.isDebugEnabled() ) {
LOG.debug("processQuery(): latest release version from metadata: "+info.latestReleaseVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;

/**
* Background process that periodically wakes up and initiates
Expand Down Expand Up @@ -210,8 +211,12 @@ public boolean requiresUpdate(VersionInfo info, Artifact artifact) {
if ( requiresUpdate( info ) ) {
return true;
}
final Predicate<Version> needsUpdate =v -> v == null || ! v.hasReleaseDate() || ! info.hasReleaseDate( v ).orElse( false );
boolean updateNeeded;
if ( info.latestReleaseVersion == null || ! info.latestReleaseVersion.hasReleaseDate()) {
if ( needsUpdate.test( info.latestReleaseVersion ) )
{
updateNeeded = true;
} else if ( info.latestSnapshotVersion != null && needsUpdate.test( info.latestSnapshotVersion ) ) {
updateNeeded = true;
} else if ( StringUtils.isNotBlank( artifact.version ) ) {
// when called by the Background Updater, the artifact version will be blank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.codesourcery.versiontracker.common.Artifact;
import de.codesourcery.versiontracker.common.Blacklist;
import de.codesourcery.versiontracker.common.IVersionProvider;
import de.codesourcery.versiontracker.common.JSONHelper;
import de.codesourcery.versiontracker.common.Version;
Expand Down Expand Up @@ -171,6 +172,8 @@ public interface MyStreamHandler<T>
private final Object THREAD_POOL_LOCK=new Object();
private ThreadPoolExecutor threadPool;

private Blacklist blacklist = new Blacklist();

public MavenCentralVersionProvider()
{
this( DEFAULT_REPO1_BASE_URL, DEFAULT_SONATYPE_REST_API_BASE_URL);
Expand All @@ -183,7 +186,14 @@ public MavenCentralVersionProvider(String repo1BaseUrl, String sonatypeRestApiBa
this.repo1BaseUrl = repo1BaseUrl+(repo1BaseUrl.trim().endsWith("/") ? "" : "/" );
this.sonatypeRestApiBaseUrl = sonatypeRestApiBaseUrl+(sonatypeRestApiBaseUrl.trim().endsWith("/") ? "" : "/" );
}


@Override
public void setBlacklist(Blacklist blacklist)
{
Validate.notNull( blacklist, "blacklist must not be null" );
this.blacklist = blacklist;
}

public static void main(String[] args) throws IOException
{
final Artifact test = new Artifact();
Expand All @@ -205,10 +215,22 @@ public static void main(String[] args) throws IOException
.forEach( x -> System.out.println( x.versionString + " => " + x.releaseDate ) );
}

private boolean isBlacklisted(Artifact a) {
return blacklist.isAllVersionsBlacklisted( a.groupId, a.artifactId );
}

@Override
public UpdateResult update(VersionInfo info, Set<String> additionalVersionsToFetchReleaseDatesFor) throws IOException
{
final Artifact artifact = info.artifact;

if ( isBlacklisted( artifact ) ) {
if ( LOG.isDebugEnabled() ) {
LOG.debug( "update(): Not updating blacklisted artifact " + artifact );
}
return UpdateResult.BLACKLISTED;
}

final URL url = new URL( repo1BaseUrl +metaDataPath( artifact ) );

if ( LOG.isDebugEnabled() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class BlacklistTest
{
Expand Down Expand Up @@ -91,6 +93,15 @@ private boolean isBlacklisted(String groupId, String artifactId, String version
return bl.isArtifactBlacklisted(a);
}

@Test
void testAllVersionBlacklisted()
{
bl.addIgnoredVersion("com.voipfuture", ".*",Blacklist.VersionMatcher.REGEX);
assertTrue( bl.isAllVersionsBlacklisted( "com.voipfuture", "something" ) );
assertTrue( bl.isAllVersionsBlacklisted( "com.voipfuture.something", "something" ) );
assertFalse( bl.isAllVersionsBlacklisted( "com.somethingelse", "something" ) );
}

@Test
void testExactVersionOnly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ public DependencyAgeRule() {

private boolean isTooOld(ArtifactResponse response,Age threshold)
{
if ( response.hasCurrentVersion() &&
response.hasLatestVersion() )
if ( response.hasCurrentVersion() && response.hasLatestVersion() )
{
if ( response.currentVersion.hasReleaseDate() && response.latestVersion.hasReleaseDate() &&
! Objects.equals( response.currentVersion.versionString, response.latestVersion.versionString ) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package de.codesourcery.versiontracker.server;

import de.codesourcery.versiontracker.common.Artifact;
import de.codesourcery.versiontracker.common.Blacklist;
import de.codesourcery.versiontracker.common.IVersionProvider;
import de.codesourcery.versiontracker.common.IVersionStorage;
import de.codesourcery.versiontracker.common.VersionInfo;
Expand Down Expand Up @@ -111,6 +112,12 @@ public void test() throws InterruptedException {

private final Statistics stats = new Statistics();

@Override
public void setBlacklist(Blacklist blacklist)
{
throw new UnsupportedOperationException( "Method setBlacklist not implemented" );
}

@Override
public Statistics getStatistics() {
return stats;
Expand Down

0 comments on commit 5d0db14

Please sign in to comment.