Skip to content

Commit

Permalink
fix bug while calculating latest version for project data storage (#423)
Browse files Browse the repository at this point in the history
* fix bug while calculating latest version for project data storage

* add logic to determine latest version of a project

* update project data only if there is change

* rename file and test the evaluation and update of latestVersion

* add snapshot check

* remove snapshot check

* add test case for snapshot versions

* add missing import
  • Loading branch information
gs-gunjan authored Nov 15, 2023
1 parent 93d3b1e commit 0d3c96f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.finos.legend.depot.services.api.artifacts.handlers.ProjectArtifactsHandler;
import org.finos.legend.depot.core.services.tracing.TracerFactory;
import org.finos.legend.depot.core.services.metrics.PrometheusMetricsFactory;
import org.finos.legend.sdlc.domain.model.version.VersionId;
import org.slf4j.Logger;

import javax.inject.Inject;
Expand Down Expand Up @@ -308,28 +307,10 @@ private void updateProjectVersionData(StoreProjectData project, String versionId

private void updateProjectData(StoreProjectData projectData, String versionId)
{
if (!VersionValidator.isSnapshotVersion(versionId))
if (projectData.evaluateLatestVersionAndUpdate(versionId))
{
String latestVersion = calculateLatestVersion(projectData.getLatestVersion(), versionId);
if (!latestVersion.equals(projectData.getLatestVersion()))
{
projectData.setLatestVersion(latestVersion);
projects.createOrUpdate(projectData);
}
}
}

private String calculateLatestVersion(String projectLatestVersion, String versionId)
{
if (projectLatestVersion == null)
{
return versionId;
}
else if (VersionId.parseVersionId(versionId).compareTo(VersionId.parseVersionId(projectLatestVersion)) > 1)
{
return versionId;
projects.createOrUpdate(projectData);
}
return projectLatestVersion;
}

private String queueWorkToRefreshProjectVersion(StoreProjectData projectData, String versionId, boolean fullUpdate, boolean transitive, String parentEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.finos.legend.depot.store.model.HasIdentifier;
import org.finos.legend.depot.domain.CoordinateData;
import org.finos.legend.sdlc.domain.model.version.VersionId;
import org.finos.legend.depot.domain.version.VersionValidator;

@JsonIgnoreProperties(ignoreUnknown = true)
public class StoreProjectData extends CoordinateData implements HasIdentifier
Expand Down Expand Up @@ -78,6 +80,17 @@ public void setLatestVersion(String latestVersion)
this.latestVersion = latestVersion;
}

public boolean evaluateLatestVersionAndUpdate(String candidateVersion)
{
if (!VersionValidator.isSnapshotVersion(candidateVersion) &&
(this.getLatestVersion() == null || VersionId.parseVersionId(candidateVersion).compareTo(VersionId.parseVersionId(this.getLatestVersion())) == 1))
{
this.setLatestVersion(candidateVersion);
return true;
}
return false;
}

@Override
@JsonIgnore
public String getId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
package org.finos.legend.depot.domain.project;

import org.finos.legend.depot.domain.CoordinateValidator;
import org.finos.legend.depot.store.model.projects.StoreProjectData;
import org.junit.Assert;
import org.junit.Test;

public class TestProjectValidator
public class TestProjectUtilities
{

@Test
Expand All @@ -41,4 +42,23 @@ public void validArtifactId()
Assert.assertFalse(CoordinateValidator.isValidArtifactId(""));
Assert.assertFalse(CoordinateValidator.isValidArtifactId("singleWordNoDots"));
}

@Test
public void testEvaluateLatestVersionAndUpdate()
{
StoreProjectData projectData = new StoreProjectData("PROD-1", "examples.test", "metadata", null, "2.1.0");

Assert.assertTrue(projectData.evaluateLatestVersionAndUpdate("3.0.0"));
Assert.assertEquals(projectData.getLatestVersion(), "3.0.0");

projectData.setLatestVersion("2.1.0");
Assert.assertFalse(projectData.evaluateLatestVersionAndUpdate("2.1.0"));
Assert.assertFalse(projectData.evaluateLatestVersionAndUpdate("2.0.1"));

projectData = new StoreProjectData("PROD-1", "examples.test", "metadata", null, null);
Assert.assertTrue(projectData.evaluateLatestVersionAndUpdate("3.0.0"));
Assert.assertEquals(projectData.getLatestVersion(), "3.0.0");

Assert.assertFalse(projectData.evaluateLatestVersionAndUpdate("master-SNAPSHOT"));
}
}

0 comments on commit 0d3c96f

Please sign in to comment.