Skip to content

Commit

Permalink
fix: fix signlescan option
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Rudyk <m.rudyk@samsung.com>
  • Loading branch information
m-rudyk committed Mar 4, 2024
1 parent 6cdeca2 commit b32302c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 48 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
25 changes: 5 additions & 20 deletions src/main/java/com/lpvs/service/LPVSDetectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
import com.lpvs.entity.LPVSQueue;
import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService;
import com.lpvs.util.LPVSCommentUtil;
import com.lpvs.util.LPVSFileUtil;

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.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;
Expand Down Expand Up @@ -61,11 +56,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 All @@ -78,11 +68,6 @@ public class LPVSDetectService {
@Value("${build.html.report:}")
private String htmlReport;

/**
* Spring application context.
*/
@Autowired ApplicationContext ctx;

/**
* Constructs an instance of LPVSDetectService with the specified parameters.
*
Expand Down Expand Up @@ -119,14 +104,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 +138,11 @@ 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));
}
}

Expand Down
22 changes: 21 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,33 @@ 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<>();
}
}
}

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
9 changes: 5 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ 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=singlescan

# GitHub settings
# Corresponding env. variable LPVS_GITHUB_LOGIN
github.login=
github.login=m-rudyk
# Corresponding env. variable LPVS_GITHUB_TOKEN
github.token=
github.token=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
# Corresponding env. variable LPVS_GITHUB_API_URL
github.api.url=https://api.github.com
# Corresponding env. variable LPVS_GITHUB_SECRET
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=
19 changes: 2 additions & 17 deletions src/test/java/com/lpvs/service/LPVSDetectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 +46,6 @@
@ExtendWith(MockitoExtension.class)
public class LPVSDetectServiceTest {

@Mock private ApplicationEventPublisher mockEventPublisher;

@Mock private LPVSScanossDetectService scanossDetectService;

@InjectMocks private LPVSDetectService lpvsDetectService;
Expand Down Expand Up @@ -123,7 +120,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 +135,8 @@ 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, "eventPublisher", mockEventPublisher);
// doNothing().when(mockEventPublisher).publishEvent(any());

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());
}
Expand Down Expand Up @@ -173,11 +169,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());

detectService.runOneScan();

setPrivateField(detectService, "htmlReport", "");
Expand All @@ -194,15 +187,12 @@ 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);

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

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

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());
}
Expand All @@ -246,7 +234,6 @@ 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);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
Expand Down Expand Up @@ -279,7 +266,6 @@ 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);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
Expand Down Expand Up @@ -313,7 +299,6 @@ 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);

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

0 comments on commit b32302c

Please sign in to comment.