Skip to content

Commit

Permalink
fix: Fix license search by alternative names and remove code duplicat…
Browse files Browse the repository at this point in the history
…es (#497)

* fix: Fix incorrect work of license check by alternative name

Signed-off-by: Oleg Kopysov <[email protected]>

* refactor: Remove code duplicates

Signed-off-by: Oleg Kopysov <[email protected]>

* fix: Fix unit tests after the source code update

Signed-off-by: Oleg Kopysov <[email protected]>

---------

Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Apr 29, 2024
1 parent a4be036 commit 0849204
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long>
*/
@Query(
value =
"SELECT * FROM license_list WHERE license_list.license_alternative_names LIKE %:licenseName% ORDER BY id DESC LIMIT 1",
"SELECT * FROM license_list WHERE FIND_IN_SET(:licenseName, license_alternative_names) > 0 ORDER BY id DESC LIMIT 1",
nativeQuery = true)
LPVSLicense searchByAlternativeLicenseNames(@Param("licenseName") String licenseName);

Expand Down
59 changes: 33 additions & 26 deletions src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ public LPVSLicenseService(
this.exitHandler = exitHandler;
}

/**
* Load all license conflicts from the database.
*/
private void loadLicenseConflicts() {
List<LPVSLicenseConflict> conflicts =
lpvsLicenseConflictRepository.takeAllLicenseConflicts();
for (LPVSLicenseConflict conflict : conflicts) {
Conflict<String, String> conf =
new Conflict<>(
conflict.getConflictLicense().getSpdxId(),
conflict.getRepositoryLicense().getSpdxId());
if (!licenseConflicts.contains(conf)) {
licenseConflicts.add(conf);
}
}
}

/**
* Initializes the LPVSLicenseService by loading licenses and license conflicts from the database.
*/
Expand Down Expand Up @@ -129,17 +146,7 @@ private void init() {
licenseConflicts = new ArrayList<>();

if (licenseConflictsSource.equalsIgnoreCase("db")) {
List<LPVSLicenseConflict> conflicts =
lpvsLicenseConflictRepository.takeAllLicenseConflicts();
for (LPVSLicenseConflict conflict : conflicts) {
Conflict<String, String> conf =
new Conflict<>(
conflict.getConflictLicense().getSpdxId(),
conflict.getRepositoryLicense().getSpdxId());
if (!licenseConflicts.contains(conf)) {
licenseConflicts.add(conf);
}
}
loadLicenseConflicts();
// print info
log.info(
"LICENSE CONFLICTS: loaded "
Expand All @@ -163,20 +170,10 @@ private void init() {
public void reloadFromTables() {
if (licenses.isEmpty()) {
licenses = lpvsLicenseRepository.takeAllLicenses();
log.info("LOADED " + licenses.size() + " licenses from DB.");

List<LPVSLicenseConflict> conflicts =
lpvsLicenseConflictRepository.takeAllLicenseConflicts();
for (LPVSLicenseConflict conflict : conflicts) {
Conflict<String, String> conf =
new Conflict<>(
conflict.getConflictLicense().getSpdxId(),
conflict.getRepositoryLicense().getSpdxId());
if (!licenseConflicts.contains(conf)) {
licenseConflicts.add(conf);
}
}
log.info("LOADED " + licenseConflicts.size() + " license conflicts from DB.");
log.info("RELOADED " + licenses.size() + " licenses from DB.");

loadLicenseConflicts();
log.info("RELOADED " + licenseConflicts.size() + " license conflicts from DB.");
}
}

Expand Down Expand Up @@ -276,8 +273,18 @@ public List<Conflict<String, String>> findConflicts(

// 1. Check conflict between repository license and detected licenses
String repositoryLicense = webhookConfig.getRepositoryLicense();
// ToDo: add check for license alternative names. Reason: GitHub can use not SPDX ID.

if (repositoryLicense != null) {
LPVSLicense repoLicense = lpvsLicenseRepository.searchBySpdxId(repositoryLicense);
if (repoLicense == null) {
repoLicense =
lpvsLicenseRepository.searchByAlternativeLicenseNames(repositoryLicense);
}

if (repoLicense != null) {
repositoryLicense = repoLicense.getSpdxId();
}

for (String detectedLicenseUnique : detectedLicensesUnique) {
for (Conflict<String, String> licenseConflict : licenseConflicts) {
Conflict<String, String> possibleConflict =
Expand Down
45 changes: 17 additions & 28 deletions src/main/java/com/lpvs/util/LPVSWebhookUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,31 @@ public static boolean checkPayload(String payload) {
}

/**
* Retrieves the organization name from the repository URL in the LPVSQueue object.
* Checks if the given LPVSQueue object is not null and has a non-null repository URL.
*
* @param webhookConfig LPVSQueue object containing repository information.
* @return The organization name.
* @throws IllegalArgumentException If the provided LPVSQueue object is null or if the repository URL is absent.
* @param webhookConfig the LPVSQueue object to check
* @throws IllegalArgumentException If the webhook configuration is null or if the repository URL is absent.
*/
public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
private static void checkWebhookConfig(LPVSQueue webhookConfig) {
if (null == webhookConfig) {
log.error("Webhook Config is absent");
throw new IllegalArgumentException("Webhook is absent");
throw new IllegalArgumentException("Webhook Config is absent");
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository URL info in webhook config");
throw new IllegalArgumentException("No repository URL info in webhook config");
}
}

/**
* Retrieves the organization name from the repository URL in the LPVSQueue object.
*
* @param webhookConfig LPVSQueue object containing repository information.
* @return The organization name.
*/
public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
checkWebhookConfig(webhookConfig);
List<String> url = Arrays.asList(webhookConfig.getRepositoryUrl().split("/"));
return url.get(url.size() - 2);
}
Expand All @@ -135,19 +143,9 @@ public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
*
* @param webhookConfig LPVSQueue object containing repository information.
* @return The repository name.
* @throws IllegalArgumentException If the provided LPVSQueue object is null or if the repository URL is absent.
*/
public static String getRepositoryName(LPVSQueue webhookConfig) {
if (null == webhookConfig) {
log.error("Webhook Config is absent");
throw new IllegalArgumentException("Webhook is absent");
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository URL info in webhook config");
throw new IllegalArgumentException("No repository URL info in webhook config");
}

checkWebhookConfig(webhookConfig);
List<String> url = Arrays.asList(webhookConfig.getRepositoryUrl().split("/"));
return url.get(url.size() - 1);
}
Expand All @@ -162,7 +160,7 @@ public static String getRepositoryName(LPVSQueue webhookConfig) {
public static String getRepositoryUrl(LPVSQueue webhookConfig) {
if (null == webhookConfig) {
log.error("Webhook Config is absent");
throw new IllegalArgumentException("Webhook is absent");
throw new IllegalArgumentException("Webhook Config is absent");
}
return webhookConfig.getRepositoryUrl();
}
Expand All @@ -175,16 +173,7 @@ public static String getRepositoryUrl(LPVSQueue webhookConfig) {
* @throws IllegalArgumentException If the provided LPVSQueue object is null or if the pull request URL is absent.
*/
public static String getPullRequestId(LPVSQueue webhookConfig) {
if (null == webhookConfig) {
log.error("Webhook Config is absent");
throw new IllegalArgumentException("Webhook is absent");
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository URL info in webhook config");
throw new IllegalArgumentException("No repository URL info in webhook config");
}

checkWebhookConfig(webhookConfig);
if (null == webhookConfig.getPullRequestUrl()) {
log.error("Pull Request URL is absent in webhook config");
throw new IllegalArgumentException("Pull Request URL is absent in webhook config");
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/database_dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ CREATE TABLE IF NOT EXISTS member (
UNIQUE (email,provider)
);

INSERT INTO license_list (id, license_name, license_spdx, license_usage) VALUES
(1, 'GNU General Public License v3.0 only','GPL-3.0-only','PROHIBITED'),
(2, 'OpenSSL License','OpenSSL','PERMITTED'),
(3, 'GNU Lesser General Public License v2.0 or later','LGPL-2.0-or-later','RESTRICTED'),
(4, 'MIT License', 'MIT', 'PERMITTED'),
(5, 'Apache License 2.0', 'Apache-2.0', 'PERMITTED'),
(6, 'GNU General Public License v2.0 only', 'GPL-2.0-only', 'RESTRICTED'),
(7, 'GNU Lesser General Public License v3.0 or later', 'LGPL-3.0-or-later', 'PROHIBITED');
INSERT INTO license_list (id, license_name, license_spdx, license_alternative_names, license_usage) VALUES
(1, 'GNU General Public License v3.0 only','GPL-3.0-only','','PROHIBITED'),
(2, 'OpenSSL License','OpenSSL','OPENSSL_LICENSE,SSLeay license and OpenSSL License','PERMITTED'),
(3, 'GNU Lesser General Public License v2.0 or later','LGPL-2.0-or-later','','RESTRICTED'),
(4, 'MIT License','MIT','Bouncy Castle Licence,The MIT License,The MIT License (MIT)','PERMITTED'),
(5, 'Apache License 2.0','Apache-2.0','Android-Apache-2.0,Apache 2,Apache 2.0,Apache 2.0 license,Apache License (v2.0),Apache License v2,Apache License v2.0,Apache License Version 2.0,Apache License Version 2.0 January 2004,Apache Public License 2.0,Apache Software License (Apache 2.0),Apache Software License (Apache License 2.0),Apache Software License - Version 2.0,Apache v2,Apache v2.0,Apache Version 2.0,Apache-2.0 License,APACHE2,ASF 2.0,http://www.apache.org/licenses/LICENSE-2.0.txt,https://www.apache.org/licenses/LICENSE-2.0,https://www.apache.org/licenses/LICENSE-2.0.txt,the Apache License ASL Version 2.0,The Apache License Version 2.0,The Apache Software License Version 2.0','PERMITTED'),
(6, 'GNU General Public License v2.0 only','GPL-2.0-only','','RESTRICTED'),
(7, 'GNU Lesser General Public License v3.0 or later','LGPL-3.0-or-later','GNU Lesser General Public License v3 or later (LGPLv3+),Lesser General Public License version 3 or greater,LGPLv3+','PROHIBITED');

INSERT INTO license_conflicts (conflict_license_id, repository_license_id) VALUES
(1, 3), (1, 6), (2, 6), (2, 3), (3, 5), (3, 7), (5, 6), (6, 7);
18 changes: 18 additions & 0 deletions src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -153,6 +154,13 @@ public void testFindConflicts() {
add(new LPVSLicenseService.Conflict<>("", ""));
}
});

LPVSLicenseRepository lpvsLicenseRepository = Mockito.mock(LPVSLicenseRepository.class);
ReflectionTestUtils.setField(
licenseService, "lpvsLicenseRepository", lpvsLicenseRepository);
when(lpvsLicenseRepository.searchBySpdxId(anyString())).thenReturn(null);
when(lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString())).thenReturn(null);

Assertions.assertNotNull(licenseService.findConflicts(webhookConfig, fileList));
}

Expand Down Expand Up @@ -425,6 +433,12 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
conflicts_field.setAccessible(true);
conflicts_field.set(licenseService, List.of(conflict_1));

LPVSLicenseRepository lpvsLicenseRepository = Mockito.mock(LPVSLicenseRepository.class);
Field license_repository =
licenseService.getClass().getDeclaredField("lpvsLicenseRepository");
license_repository.setAccessible(true);
license_repository.set(licenseService, lpvsLicenseRepository);

lpvs_license_1 = new LPVSLicense(1L, null, spdx_id_1, null, null, null);
lpvs_file_1 =
new LPVSFile(
Expand Down Expand Up @@ -461,6 +475,10 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
null,
null);

when(lpvsLicenseRepository.searchBySpdxId(anyString())).thenReturn(null);
when(lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString()))
.thenReturn(lpvs_license_1);

webhookConfig = new LPVSQueue();
webhookConfig.setRepositoryLicense("MIT");
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/lpvs/util/LPVSWebhookUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void checkNull() {
() -> {
LPVSWebhookUtil.getRepositoryOrganization(null);
});
assertEquals("Webhook is absent", exception.getMessage());
assertEquals("Webhook Config is absent", exception.getMessage());

exception =
assertThrows(
Expand All @@ -221,7 +221,7 @@ public void checkNull() {
() -> {
LPVSWebhookUtil.getRepositoryName(null);
});
assertEquals("Webhook is absent", exception.getMessage());
assertEquals("Webhook Config is absent", exception.getMessage());

exception =
assertThrows(
Expand All @@ -237,15 +237,15 @@ public void checkNull() {
() -> {
LPVSWebhookUtil.getRepositoryUrl(null);
});
assertEquals("Webhook is absent", exception.getMessage());
assertEquals("Webhook Config is absent", exception.getMessage());

exception =
assertThrows(
IllegalArgumentException.class,
() -> {
LPVSWebhookUtil.getPullRequestId(null);
});
assertEquals("Webhook is absent", exception.getMessage());
assertEquals("Webhook Config is absent", exception.getMessage());

exception =
assertThrows(
Expand Down

0 comments on commit 0849204

Please sign in to comment.