Skip to content

Commit

Permalink
initial new condition to include snapshot in processing version list
Browse files Browse the repository at this point in the history
  • Loading branch information
ntqdinh-axonivy committed Jan 9, 2025
1 parent 4569511 commit 6ddb4c7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
import java.util.List;

public interface MetadataService {

int syncAllProductsMetadata();

boolean syncProductMetadata(Product product);

void updateArtifactAndMetadata(String productId , List<String> versions , List<Artifact> artifacts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,64 +69,6 @@ public void updateMavenArtifactVersionData(Set<Metadata> metadataSet, MavenArtif
}
}

public int syncAllProductsMetadata() {
List<Product> products = productRepo.getAllProductsWithIdAndReleaseTagAndArtifact();
log.warn("**MetadataService: Start to sync version for {} product(s)", products.size());
int nonUpdatedSyncCount = 0;
for (Product product : products) {
if (!syncProductMetadata(product)) {
nonUpdatedSyncCount += 1;
}
}
log.warn("**MetadataService: version sync finished");
return nonUpdatedSyncCount;
}

@Override
public boolean syncProductMetadata(Product product) {
if (product == null) {
return false;
}

// Set up cache before sync
String productId = product.getId();
Set<Metadata> metadataSet = new HashSet<>(metadataRepo.findByProductId(product.getId()));
MavenArtifactVersion artifactVersionCache = mavenArtifactVersionRepo.findById(product.getId()).orElse(
MavenArtifactVersion.builder().productId(productId).build());
MetadataSync syncCache = metadataSyncRepo.findById(product.getId()).orElse(
MetadataSync.builder().productId(product.getId()).syncedVersions(new HashSet<>()).build());
Set<Artifact> artifactsFromNewTags = new HashSet<>();

// Find artifacts from unhandled tags
List<String> nonSyncedVersionOfTags = VersionUtils.removeSyncedVersionsFromReleasedVersions(
product.getReleasedVersions(), syncCache.getSyncedVersions());
if (ObjectUtils.isNotEmpty(nonSyncedVersionOfTags)) {
artifactsFromNewTags.addAll(getArtifactsFromNonSyncedVersion(product.getId(), nonSyncedVersionOfTags));
syncCache.getSyncedVersions().addAll(nonSyncedVersionOfTags);
log.info("**MetadataService: New tags detected: {} in product {}", nonSyncedVersionOfTags.toString(),
productId);
}

// Sync versions from maven & update artifacts-version table
metadataSet.addAll(MavenUtils.convertArtifactsToMetadataSet(artifactsFromNewTags, productId));
if (ObjectUtils.isNotEmpty(product.getArtifacts())) {
metadataSet.addAll(
MavenUtils.convertArtifactsToMetadataSet(new HashSet<>(product.getArtifacts()), productId));
}
if (CollectionUtils.isEmpty(metadataSet)) {
log.info("**MetadataService: No artifact found in product {}", productId);
return false;
}
artifactVersionCache.setAdditionalArtifactsByVersion(new HashMap<>());
updateMavenArtifactVersionData(metadataSet, artifactVersionCache);

// Persist changed
metadataSyncRepo.save(syncCache);
mavenArtifactVersionRepo.save(artifactVersionCache);
metadataRepo.saveAll(metadataSet);
return true;
}

@Override
public void updateArtifactAndMetadata(String productId, List<String> versions, List<Artifact> artifacts) {
Set<Metadata> metadataSet = new HashSet<>(metadataRepo.findByProductId(productId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,14 @@ private void updateContentsFromMavenXML(Product product, String metadataContent,
mavenVersions.add(versionNodes.item(i).getTextContent());
}

// Check if having new released version in Maven
// Check if having new released version or unreleased dev version in Maven
List<String> currentVersions = ObjectUtils.isNotEmpty(product.getReleasedVersions()) ?
product.getReleasedVersions() :
productModuleContentRepo.findVersionsByProductId(product.getId());
mavenVersions = mavenVersions.stream().filter(version -> !currentVersions.contains(version)).toList();
List<String> versionChanges =
mavenVersions.stream().filter(version -> !currentVersions.contains(version)|| (VersionUtils.isOfficialVersionOrUnReleasedDevVersion(mavenVersions,version) && !VersionUtils.isReleasedVersion(version))).toList();

if (ObjectUtils.isEmpty(mavenVersions)) {
if (ObjectUtils.isEmpty(versionChanges)) {
return;
}

Expand All @@ -520,19 +521,19 @@ private void updateContentsFromMavenXML(Product product, String metadataContent,
product.setNewestPublishedDate(lastUpdated);
product.setNewestReleaseVersion(latestVersion);
}
updateProductCompatibility(product, mavenVersions);
updateProductCompatibility(product, versionChanges);

Optional.ofNullable(product.getReleasedVersions()).ifPresentOrElse(releasedVersion -> {},
() -> product.setReleasedVersions(new ArrayList<>()));

List<ProductModuleContent> productModuleContents = new ArrayList<>();
for (String version : mavenVersions) {
for (String version : versionChanges) {
product.getReleasedVersions().add(version);
ProductModuleContent productModuleContent = handleProductArtifact(version, product.getId(), mavenArtifact,
product.getNames().get(EN_LANGUAGE));
Optional.ofNullable(productModuleContent).ifPresent(productModuleContents::add);
}
nonSyncReleasedVersions.addAll(mavenVersions);
nonSyncReleasedVersions.addAll(versionChanges);

if (ObjectUtils.isNotEmpty(productModuleContents)) {
productModuleContentRepo.saveAll(productModuleContents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,6 @@ void testUpdateMavenArtifactVersionFromMetadata() {
}
}

@Test
void testSyncAllProductsMetadata() {
Mockito.when(productRepo.getAllProductsWithIdAndReleaseTagAndArtifact()).thenReturn(List.of(new Product()));
Mockito.when(metadataRepo.findByProductId(Mockito.isNull())).thenReturn(new ArrayList<>());
int result = metadataService.syncAllProductsMetadata();
Assertions.assertEquals(1, result);
Mockito.when(productRepo.getAllProductsWithIdAndReleaseTagAndArtifact()).thenReturn(getMockProducts());
result = metadataService.syncAllProductsMetadata();
Assertions.assertEquals(0, result);
}

@Test
void testUpdateMavenArtifactVersionData() {
Metadata mockMetadata = getMockMetadata();
Expand Down

0 comments on commit 6ddb4c7

Please sign in to comment.