Skip to content

Commit

Permalink
fix: modify single scan feature (#463)
Browse files Browse the repository at this point in the history
* fix: fix signlescan option

Signed-off-by: Mykola Rudyk <m.rudyk@samsung.com>

* test: add test for reloading tables from DB for singlescan

Signed-off-by: Mykola Rudyk <m.rudyk@samsung.com>

* fix: fix remarks after review, close application after one scan

Signed-off-by: Mykola Rudyk <m.rudyk@samsung.com>

---------

Signed-off-by: Mykola Rudyk <m.rudyk@samsung.com>
  • Loading branch information
m-rudyk authored Mar 5, 2024
1 parent 6cdeca2 commit cfc05c6
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/entity/LPVSDetectedLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* This class is mapped to the "detected_license" table in the "lpvs" schema.
*/
@Entity
@Table(name = "detected_license", schema = "${lpvs.schema:lpvs}")
@Table(name = "detected_license")
@Getter
@Setter
public class LPVSDetectedLicense implements Serializable {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/lpvs/entity/LPVSLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
@Entity
@Table(
name = "license_list",
schema = "${lpvs.schema:lpvs}",
indexes = {@Index(name = "spdx_id", columnList = "license_spdx", unique = true)})
@Getter
@Setter
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/entity/LPVSLicenseConflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* This class is mapped to the "license_conflicts" table in the "lpvs" schema.
*/
@Entity
@Table(name = "license_conflicts", schema = "${lpvs.schema:lpvs}")
@Table(name = "license_conflicts")
@Getter
@Setter
@NoArgsConstructor
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/entity/LPVSPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* This class is mapped to the "pull_requests" table in the "lpvs" schema.
*/
@Entity
@Table(name = "pull_requests", schema = "${lpvs.schema:lpvs}")
@Table(name = "pull_requests")
@Getter
@Setter
@AllArgsConstructor
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/entity/LPVSQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* This class is mapped to the "queue" table in the "lpvs" schema.
*/
@Entity
@Table(name = "queue", schema = "${lpvs.schema:lpvs}")
@Table(name = "queue")
@Getter
@Setter
public class LPVSQueue implements Serializable {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/lpvs/entity/auth/LPVSMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
@Entity
@Table(
name = "member",
schema = "${lpvs.schema:lpvs}",
indexes = {@Index(name = "unq_member", columnList = "email, provider", unique = true)})
public class LPVSMember {

Expand Down
42 changes: 19 additions & 23 deletions src/main/java/com/lpvs/service/LPVSDetectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
*/
package com.lpvs.service;

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSQueue;
import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService;
import com.lpvs.util.LPVSCommentUtil;
import com.lpvs.util.LPVSFileUtil;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ExitCodeEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSQueue;
import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService;
import com.lpvs.util.LPVSCommentUtil;

import jakarta.annotation.PostConstruct;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

/**
* Service class for detecting licenses in GitHub pull requests using a specified scanner.
Expand Down Expand Up @@ -61,11 +61,6 @@ public class LPVSDetectService {
*/
private LPVSGitHubService gitHubService;

/**
* Event publisher for triggering application events.
*/
@Autowired private ApplicationEventPublisher eventPublisher;

/**
* GitHub pull request used to trigger a single license scan (optional).
*/
Expand Down Expand Up @@ -119,14 +114,15 @@ private void init() {
@EventListener(ApplicationReadyEvent.class)
public void runOneScan() {
if (trigger != null && !HtmlUtils.htmlEscape(trigger).equals("")) {
log.info("Triggered signle scan operation");
log.info("Triggered single scan operation");
try {
licenseService.reloadFromTables();
LPVSQueue webhookConfig =
gitHubService.getInternalQueueByPullRequest(HtmlUtils.htmlEscape(trigger));

List<LPVSFile> scanResult =
this.runScan(
webhookConfig, LPVSFileUtil.getPathByPullRequest(webhookConfig));
webhookConfig, gitHubService.getPullRequestFiles(webhookConfig));

List<LPVSLicenseService.Conflict<String, String>> detectedConflicts =
licenseService.findConflicts(webhookConfig, scanResult);
Expand All @@ -152,12 +148,12 @@ public void runOneScan() {
webhookConfig, scanResult, detectedConflicts);
log.info(report);
}
log.info("Single scan completed.");
} catch (Exception ex) {
log.error("\n\n\n Single scan finished with errors \n\n\n");
log.error("Can't triger single scan " + ex);
log.error("Can't trigger single scan: " + ex.getMessage());
}
// exiting application
eventPublisher.publishEvent(new ExitCodeEvent(new Object(), 0));
SpringApplication.exit(ctx, () -> 0);
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,37 @@ private void init() {

} catch (Exception ex) {
log.warn("LICENSES and LICENSE CONFLICTS are not loaded.");
log.error(ex.toString());
log.error(ex.getMessage());
licenses = new ArrayList<>();
licenseConflicts = new ArrayList<>();
}
}
}

/**
* This method reloads licenses and license conflicts from database tables if the
* licenses list in case of single scan triggered and used in memory database.
*/
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.");
}
}

/**
* Finds a license by SPDX identifier.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-singlescan.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring.sql.init.platform=h2
# DB Configuration
spring.sql.init.data-locations=classpath*:database_dump.sql
spring.datasource.initialization-mode=always
spring.datasource.url=jdbc:h2:mem:singlescan;DATABASE_TO_UPPER=false;MODE=MySQL;NON_KEYWORDS=USER
spring.datasource.url=jdbc:h2:mem:singlescan;DATABASE_TO_UPPER=false;MODE=MySQL;NON_KEYWORDS=USER;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=
spring.datasource.password=

Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ license_conflict=db

# Logger configuration
logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
spring.profiles.active=

# GitHub settings
# Corresponding env. variable LPVS_GITHUB_LOGIN
Expand All @@ -32,7 +33,7 @@ lpvs.attempts=4

# DB Configuration
# The name of DB schema
lpvs.schema=lpvs
spring.jpa.properties.hibernate.default_schema=lpvs

spring.jpa.open-in-view=true
spring.jpa.properties.hibernate.connection.CharSet=utf8
Expand All @@ -43,6 +44,6 @@ spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/${lpvs.schema}
spring.datasource.url=jdbc:mysql://localhost:3306/lpvs
spring.datasource.username=
spring.datasource.password=
26 changes: 9 additions & 17 deletions src/test/java/com/lpvs/service/LPVSDetectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.lpvs.util.LPVSFileUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.context.ApplicationContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -25,7 +26,6 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;

import java.io.IOException;
import java.lang.reflect.Field;
Expand All @@ -47,8 +47,6 @@
@ExtendWith(MockitoExtension.class)
public class LPVSDetectServiceTest {

@Mock private ApplicationEventPublisher mockEventPublisher;

@Mock private LPVSScanossDetectService scanossDetectService;

@InjectMocks private LPVSDetectService lpvsDetectService;
Expand Down Expand Up @@ -83,6 +81,7 @@ class TestRunScan__Scanoss {
GHRepository mockRepository = mock(GHRepository.class);
GHPullRequest mockPullRequest = mock(GHPullRequest.class);
GHRepository mockHeadRepository = mock(GHRepository.class);
ApplicationContext mockApplicationContext = mock(ApplicationContext.class);

LPVSQueue webhookConfig;
final String test_path = "test_path";
Expand Down Expand Up @@ -123,7 +122,6 @@ void testRunOneScanWithNullTriger() throws NoSuchFieldException, IllegalAccessEx
spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null));

setPrivateField(lpvsDetectService, "trigger", null);
setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher);

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());

Expand All @@ -139,8 +137,7 @@ void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessExceptio
spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null));

setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value");
setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher);
doNothing().when(mockEventPublisher).publishEvent(any());
setPrivateField(lpvsDetectService, "ctx", mockApplicationContext);

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());
}
Expand Down Expand Up @@ -173,10 +170,8 @@ void testRunOneScan_Branch2()

setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);
setPrivateField(detectService, "htmlReport", null);

doNothing().when(mockEventPublisher).publishEvent(any());
setPrivateField(detectService, "ctx", mockApplicationContext);

detectService.runOneScan();

Expand All @@ -194,15 +189,13 @@ void testRunOneScan_Branch3()
List<LPVSLicenseService.Conflict<String, String>> expected =
List.of(conflict_1, conflict_1);

doNothing().when(mockEventPublisher).publishEvent(any());

lpvsDetectService =
spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null));

setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "htmlReport", "build");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);
setPrivateField(detectService, "ctx", mockApplicationContext);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
Expand All @@ -228,8 +221,7 @@ void testRunOneScan_trigerInternalQueueException()
throws NoSuchFieldException, IllegalAccessException {

setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value");
setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher);
doNothing().when(mockEventPublisher).publishEvent(any());
setPrivateField(lpvsDetectService, "ctx", mockApplicationContext);

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());
}
Expand All @@ -246,7 +238,7 @@ void testRunOneScan_TriggerNotNull() throws Exception {
setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "htmlReport", "build/report/test.html");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);
setPrivateField(detectService, "ctx", mockApplicationContext);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
Expand Down Expand Up @@ -279,7 +271,7 @@ void testRunOneScan_TriggerNotNull_Branch2() throws Exception {
setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "htmlReport", "build/report/test.html");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);
setPrivateField(detectService, "ctx", mockApplicationContext);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
Expand Down Expand Up @@ -313,7 +305,7 @@ void testRunOneScan_TriggerNotNull_Branch3() throws Exception {
setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "htmlReport", "build/report/test.html");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);
setPrivateField(detectService, "ctx", mockApplicationContext);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
Expand Down
Loading

0 comments on commit cfc05c6

Please sign in to comment.