Skip to content

Commit

Permalink
Merge pull request #2389 from Siemens-Healthineers/fix/case-insensiti…
Browse files Browse the repository at this point in the history
…ve-comparision-of-vcs-and-purl-during-comp-and-pkg-creation-2370

fix(importCDX): Add check for existing comps and package using case-insensitive comparison of vcs and purl

Reviewed by: afsah.syeda@siemens-healthineers.com
Tested by: akshit.joshi@siemens-healthineers.com
  • Loading branch information
ag4ums authored Apr 16, 2024
2 parents 2d9fa49 + 8af9bd5 commit aa57ca5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@ private void setMainLicenses(Component component) {
* Add new release to the database
*/
public AddDocumentRequestSummary addComponent(Component component, String user) throws SW360Exception {
if(isDuplicateUsingVcs(component.getVcs())){
if(isDuplicateUsingVcs(component, true)){
final AddDocumentRequestSummary addDocumentRequestSummary = new AddDocumentRequestSummary()
.setRequestStatus(AddDocumentRequestStatus.DUPLICATE);
Set<String> duplicates = componentRepository.getComponentIdsByVCS(component.getVcs().toLowerCase());
Set<String> duplicates = componentRepository.getComponentIdsByVCS(component.getVcs(), true);
if (duplicates.size() == 1) {
duplicates.forEach(addDocumentRequestSummary::setId);
}
Expand Down Expand Up @@ -605,11 +605,15 @@ private boolean isDuplicate(String componentName, boolean caseInsenstive) {
return duplicates.size()>0;
}

private boolean isDuplicateUsingVcs(String vcsUrl){
private boolean isDuplicateUsingVcs(Component component, boolean caseInsenstive){
return isDuplicateUsingVcs(component.getVcs(), caseInsenstive);
}

private boolean isDuplicateUsingVcs(String vcsUrl, boolean caseInsenstive){
if (isNullEmptyOrWhitespace(vcsUrl)) {
return false;
}
Set<String> duplicates = componentRepository.getComponentIdsByVCS(vcsUrl.toLowerCase());
Set<String> duplicates = componentRepository.getComponentIdsByVCS(vcsUrl, caseInsenstive);
return duplicates.size()>0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ public class ComponentRepository extends SummaryAwareRepository<Component> {
" } " +
"}";

private static final String BY_VCS_LOWERCASE = "function(doc) {" +
" if (doc.type == 'component') {" +
" emit(doc.vcs.toLowerCase().trim(), doc._id);" +
" } " +
"}";

public ComponentRepository(DatabaseConnectorCloudant db, ReleaseRepository releaseRepository, VendorRepository vendorRepository) {
super(Component.class, db, new ComponentSummary(releaseRepository, vendorRepository));
Map<String, MapReduce> views = new HashMap<String, MapReduce>();
Expand All @@ -172,6 +178,7 @@ public ComponentRepository(DatabaseConnectorCloudant db, ReleaseRepository relea
views.put("bymainlicense", createMapReduce(BY_MAIN_LICENSE, null));
views.put("byvendor", createMapReduce(BY_VENDOR, null));
views.put("byVCS", createMapReduce(BY_VCS, null));
views.put("byVCSLowercase", createMapReduce(BY_VCS_LOWERCASE, null));
initStandardDesignDocument(views, db);
}

Expand Down Expand Up @@ -222,9 +229,11 @@ public Set<String> getComponentIdsByName(String name, boolean caseInsenstive) {
return queryForIdsAsValue("byname", name);
}

public Set<String> getComponentIdsByVCS(String vcs){
Set<String> componentIds = queryForIdsAsValue("byVCS", vcs);
return componentIds;
public Set<String> getComponentIdsByVCS(String vcs, boolean caseInsenstive){
if(caseInsenstive){
return queryForIdsAsValue("byVCSLowercase", vcs.toLowerCase());
}
return queryForIdsAsValue("byVCS", vcs);
}

public List<Component> searchComponentByName(String name, boolean caseSensitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ private List<Package> getPackageByNameAndVersion(String pkgName, String pkgVersi
}

private List<Package> getPackageByPurl(String purl) {
if (purl == null) {
if (isNullEmptyOrWhitespace(purl)) {
return Collections.emptyList();
}
return packageRepository.searchByPurl(purl);
return packageRepository.searchByPurl(purl, true);
}

private void copyImmutableFields(Package destination, Package source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class PackageRepository extends DatabaseRepositoryCloudantClient<Package>
private static final String BY_RELEASE_ID = "function(doc) { if (doc.type == 'package') { emit(doc.releaseId, doc._id); } }";
private static final String BY_LICENSE_IDS = "function(doc) { if (doc.type == 'package') { if (doc.licenseIds) { emit(doc.licenseIds.join(), doc._id); } else { emit('', doc._id); } } }";
private static final String BY_PURL = "function(doc) { if (doc.type == 'package') { emit(doc.purl.trim(), doc._id) } }";
private static final String BY_PURL_LOWERCASE = "function(doc) { if (doc.type == 'package') { emit(doc.purl.toLowerCase().trim(), doc._id) } }";

public PackageRepository(DatabaseConnectorCloudant db) {
super(db, Package.class);
Expand All @@ -63,6 +64,7 @@ public PackageRepository(DatabaseConnectorCloudant db) {
views.put("byReleaseId", createMapReduce(BY_RELEASE_ID, null));
views.put("byLicenseIds", createMapReduce(BY_LICENSE_IDS, null));
views.put("byPurl", createMapReduce(BY_PURL, null));
views.put("byPurlLowercase", createMapReduce(BY_PURL_LOWERCASE, null));
initStandardDesignDocument(views, db);
}

Expand Down Expand Up @@ -107,9 +109,13 @@ public List<Package> searchByNameAndVersion(String name, String version, boolean
return releasesMatchingNameAndVersion;
}

public List<Package> searchByPurl(String purl) {
List<org.eclipse.sw360.datahandler.thrift.packages.Package> packagesMatchingPurl
= new ArrayList<org.eclipse.sw360.datahandler.thrift.packages.Package>(queryView("byPurl", purl));
public List<Package> searchByPurl(String purl, boolean caseInsenstive) {
List<Package> packagesMatchingPurl;
if(caseInsenstive){
packagesMatchingPurl = new ArrayList<Package>(queryView("byPurlLowercase", purl.toLowerCase()));
}else{
packagesMatchingPurl = new ArrayList<Package>(queryView("byPurl", purl));
}
return packagesMatchingPurl;
}

Expand Down

0 comments on commit aa57ca5

Please sign in to comment.