Skip to content

Commit fe39a19

Browse files
committed
Reduce cognitive complexity of findByCatalogItemSlug method
1 parent 4ab0fea commit fe39a19

1 file changed

Lines changed: 23 additions & 22 deletions

File tree

src/main/java/org/opendevstack/component_catalog/server/services/CatalogItemBySlugService.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,35 @@ public Optional<CatalogItemEntityContext> findByCatalogItemSlug(CatalogItemSlug
6262

6363
for (var target : targets) {
6464
var catalogId = idEncode(target.getUrl());
65-
6665
var catalogIdPathAt = catalogServiceAdapter.bitbucketPathAtFromId(catalogId);
6766

6867
// Optimisation: skip catalogs whose Bitbucket project key does not match the slug's project key,
6968
// avoiding unnecessary item loading. This relies on the assumption that catalog items live in the
7069
// same Bitbucket project as their catalog definition.
71-
if (CatalogItemSlug.normalise(catalogIdPathAt.getProjectKey()).equals(slug.getProjectKey())) {
72-
log.debug("Catalog target '{}' matches slug project key '{}', checking items...",
73-
target.getUrl(), slug.getProjectKey());
74-
75-
var slugCatalogEntity = catalogEntitiesService.getCatalogEntity(catalogId);
76-
77-
if (slugCatalogEntity.isPresent()) {
78-
// A slug uniquely identifies one item. Multiple matches (e.g. the same repo referenced
79-
// in more than one catalog entry) are not expected; we return the first one found.
80-
for (var itemTarget : slugCatalogEntity.get().getMetadata().getSpec().getTargets()) {
81-
var itemPathAt = bitbucketService.pathAtBuilder()
82-
.rawUrl(itemTarget.getUrl())
83-
.build();
84-
if (CatalogItemSlug.normalise(itemPathAt.getProjectKey()).equals(slug.getProjectKey())
85-
&& itemPathAt.getRepoSlug().equalsIgnoreCase(slug.getRepoName())) {
86-
var itemId = idEncode(itemPathAt.getPathAt());
87-
log.debug("Resolved slug '{}' to item id '{}' in catalog target '{}'", slug, itemId, target.getUrl());
88-
return catalogEntitiesService.getCatalogItemEntity(itemId);
89-
}
90-
}
91-
}
70+
if (!CatalogItemSlug.normalise(catalogIdPathAt.getProjectKey()).equals(slug.getProjectKey())) {
71+
continue;
72+
}
73+
74+
log.debug("Catalog target '{}' matches slug project key '{}', checking items...",
75+
target.getUrl(), slug.getProjectKey());
9276

77+
var slugCatalogEntity = catalogEntitiesService.getCatalogEntity(catalogId);
78+
if (slugCatalogEntity.isEmpty()) {
79+
continue;
80+
}
81+
82+
// A slug uniquely identifies one item. Multiple matches (e.g. the same repo referenced
83+
// in more than one catalog entry) are not expected; we return the first one found.
84+
for (var itemTarget : slugCatalogEntity.get().getMetadata().getSpec().getTargets()) {
85+
var itemPathAt = bitbucketService.pathAtBuilder()
86+
.rawUrl(itemTarget.getUrl())
87+
.build();
88+
if (CatalogItemSlug.normalise(itemPathAt.getProjectKey()).equals(slug.getProjectKey())
89+
&& itemPathAt.getRepoSlug().equalsIgnoreCase(slug.getRepoName())) {
90+
var itemId = idEncode(itemPathAt.getPathAt());
91+
log.debug("Resolved slug '{}' to item id '{}' in catalog target '{}'", slug, itemId, target.getUrl());
92+
return catalogEntitiesService.getCatalogItemEntity(itemId);
93+
}
9394
}
9495
}
9596

0 commit comments

Comments
 (0)