Skip to content

Commit

Permalink
fix: Fix logs and exceptions handling for single scan mode
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed May 18, 2024
1 parent c1db058 commit f43f077
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/lpvs/LicensePreValidationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.lpvs;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -25,6 +26,7 @@
@SpringBootApplication(scanBasePackages = {"com.lpvs"})
@EnableAutoConfiguration
@EnableAsync
@Slf4j
public class LicensePreValidationService {

/**
Expand Down Expand Up @@ -56,9 +58,11 @@ public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(LicensePreValidationService.class, args);
exitHandler = applicationContext.getBean(LPVSExitHandler.class);
} catch (IllegalArgumentException e) {
System.err.println("An IllegalArgumentException occurred: " + e.getMessage());
} catch (IllegalArgumentException ex1) {
log.error("An IllegalArgumentException occurred: " + ex1.getMessage());
System.exit(1);
} catch (Exception ex2) {
log.info("LPVS application is being closed.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void commentResults(
GHPullRequest pullRequest = getPullRequest(webhookConfig, repository);

if (pullRequest == null) {
log.error("Can't find pull request " + webhookConfig.getPullRequestUrl());
log.error("Pull request is not found " + webhookConfig.getPullRequestUrl());
lpvsPullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString());
pullRequestRepository.saveAndFlush(lpvsPullRequest);
return;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/lpvs/service/scan/LPVSDetectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ public void runOneScan() {
String report =
LPVSCommentUtil.reportCommentBuilder(
webhookConfig, scanResult, detectedConflicts);
log.info(report);
if (report != null && !report.isEmpty()) {
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("Single scan finished with errors.");
log.error("Can't trigger single scan: " + ex.getMessage());
}
SpringApplication.exit(ctx, () -> 0);
Expand Down Expand Up @@ -174,7 +176,7 @@ public List<LPVSFile> runScan(LPVSQueue webhookConfig, String path) throws Excep
}
return files;
} catch (IllegalArgumentException | NullPointerException ex) {
log.error(ex.getMessage());
log.error("Exception occurred during running the scan.");
return new ArrayList<>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public void runScan(LPVSQueue webhookConfig, String path) throws Exception {
LPVSPayloadUtil.createBufferReader(
LPVSPayloadUtil.createInputStreamReader(
process.getErrorStream()));
log.error(output.readLine());
String line = output.readLine();
if (line != null) {
log.error(line);
}
throw new Exception(
"Scanoss scanner terminated with non-zero code. Terminating.");
} finally {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,20 @@ void testCommentBuilder_ConflictFilePresent() throws Exception {
assertNotNull(commentHTML);
}

@Test
void testCommentBuilder_NoConflictNoLicense() {
List<LPVSLicenseService.Conflict<String, String>> expected = new ArrayList<>();
LPVSQueue webhookConfig = new LPVSQueue();
List<LPVSFile> scanResults = new ArrayList<>();
String commentGitHub =
LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, expected);
String commentHTML =
LPVSCommentUtil.buildHTMLComment(webhookConfig, scanResults, expected);

assertEquals(commentGitHub, "");
assertEquals(commentHTML, "<html><body></body></html>");
}

@Test
public void testGetPathByPullRequest() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ public void testRunScan_StatusEqualsOne() throws Exception {
}
}

@Test
public void testRunScan_StatusEqualsOneNoErrorStream() throws Exception {
Mockito.when(lpvsQueue.getRepositoryUrl()).thenReturn("https://github.com/Samsung/LPVS");
Mockito.when(lpvsQueue.getPullRequestUrl())
.thenReturn("https://github.com/Samsung/LPVS/pull/1");
Process process = Mockito.mock(Process.class);
try (MockedConstruction<ProcessBuilder> mockedPb =
Mockito.mockConstruction(
ProcessBuilder.class,
(mock, context) -> {
when(mock.inheritIO()).thenReturn(mock);
when(mock.start()).thenReturn(process);
when(process.getErrorStream()).thenReturn(null);
when(process.waitFor()).thenReturn(1);
})) {
Exception exception =
assertThrows(
Exception.class, () -> scanossDetectService.runScan(lpvsQueue, "path"));

// Verify that the method throws an exception when the status is 1
assertEquals(null, exception.getMessage());

verify(mockedPb.constructed().get(0)).start();
verify(process, times(1)).waitFor();
}
}

@Test
public void testRunScan_StatusEqualsZero() throws Exception {
Process process = Mockito.mock(Process.class);
Expand Down

0 comments on commit f43f077

Please sign in to comment.