|
| 1 | +package org.opendevstack.component_catalog.server.services; |
| 2 | + |
| 3 | +import org.opendevstack.component_catalog.server.services.catalog.InvalidCatalogEntityException; |
| 4 | +import org.opendevstack.component_catalog.server.services.catalog.entity.CatalogItemEntityContext; |
| 5 | +import org.opendevstack.component_catalog.server.services.exceptions.InvalidIdException; |
| 6 | +import org.opendevstack.component_catalog.server.services.slug.CatalogItemSlug; |
| 7 | +import lombok.AllArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | + |
| 11 | +import java.util.Optional; |
| 12 | + |
| 13 | +import static org.opendevstack.component_catalog.server.services.common.IdEncoderDecoder.idEncode; |
| 14 | + |
| 15 | +/** |
| 16 | + * Domain service responsible for resolving a {@link CatalogItemSlug} to a {@link CatalogItemEntityContext}. |
| 17 | + * <p> |
| 18 | + * Resolution steps: |
| 19 | + * <ol> |
| 20 | + * <li>Load all catalogs from the marketplace (catalog-of-catalogs).</li> |
| 21 | + * <li>For each catalog, load its items and find the one whose normalised Bitbucket project key |
| 22 | + * matches the slug's {@code projectKey} component and whose repository slug matches |
| 23 | + * the slug's {@code repoName} component.</li> |
| 24 | + * </ol> |
| 25 | + */ |
| 26 | +@Service |
| 27 | +@AllArgsConstructor |
| 28 | +@Slf4j |
| 29 | +public class CatalogItemBySlugService { |
| 30 | + |
| 31 | + private final CatalogsCollectionService catalogsCollectionService; |
| 32 | + private final CatalogEntitiesService catalogEntitiesService; |
| 33 | + |
| 34 | + /** |
| 35 | + * Resolves a {@link CatalogItemSlug} to the matching {@link CatalogItemEntityContext}. |
| 36 | + * |
| 37 | + * @param slug the parsed composite slug |
| 38 | + * @return an {@link Optional} with the matching context, or empty if not found |
| 39 | + * @throws InvalidIdException if any catalog id is structurally invalid |
| 40 | + * @throws InvalidCatalogEntityException if a catalog entity cannot be parsed |
| 41 | + */ |
| 42 | + public Optional<CatalogItemEntityContext> findByCatalogItemSlug(CatalogItemSlug slug) |
| 43 | + throws InvalidIdException, InvalidCatalogEntityException { |
| 44 | + |
| 45 | + log.debug("Resolving catalog item by slug: '{}'", slug); |
| 46 | + |
| 47 | + var catalogsCollection = catalogsCollectionService.getCatalogsCollection(); |
| 48 | + |
| 49 | + if (catalogsCollection.isEmpty()) { |
| 50 | + log.warn("No catalogs collection found while resolving slug '{}'", slug); |
| 51 | + return Optional.empty(); |
| 52 | + } |
| 53 | + |
| 54 | + var targets = catalogsCollection.get().getMetadata().getSpec().getTargets(); |
| 55 | + if (targets == null || targets.length == 0) { |
| 56 | + log.warn("Catalogs collection has no targets while resolving slug '{}'", slug); |
| 57 | + return Optional.empty(); |
| 58 | + } |
| 59 | + |
| 60 | + for (var target : targets) { |
| 61 | + var catalogId = idEncode(target.getUrl()); |
| 62 | + var itemContexts = catalogEntitiesService.getCatalogItemsEntities(catalogId); |
| 63 | + var match = itemContexts.stream() |
| 64 | + .filter(ctx -> itemMatchesSlug(ctx, slug)) |
| 65 | + .findFirst(); |
| 66 | + if (match.isPresent()) { |
| 67 | + log.debug("Resolved slug '{}' to item in catalog target '{}'", slug, target.getUrl()); |
| 68 | + return match; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + log.debug("No catalog item matched slug '{}'", slug); |
| 73 | + return Optional.empty(); |
| 74 | + } |
| 75 | + |
| 76 | + // --- private helpers --- |
| 77 | + |
| 78 | + private boolean itemMatchesSlug(CatalogItemEntityContext ctx, CatalogItemSlug slug) { |
| 79 | + var normalisedProjectKey = CatalogItemSlug.normalise(ctx.getRepoCatalogItemPathAt().getProjectKey()); |
| 80 | + return normalisedProjectKey.equals(slug.getProjectKey()) |
| 81 | + && slug.getRepoName().equalsIgnoreCase(ctx.getRepoCatalogItemPathAt().getRepoSlug()); |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
0 commit comments