gitHubSingleScan(
+ @PathVariable("gitHubOrg") @NotEmpty @Valid String gitHubOrg,
+ @PathVariable("gitHubRepo") @NotEmpty @Valid String gitHubRepo,
+ @PathVariable("prNumber") @Min(1) @Valid Integer prNumber)
+ throws InterruptedException, IOException {
+ log.debug("New GitHub single scan request received");
+
+ if (GITHUB_SECRET.trim().isEmpty()) {
+ log.error("Received empty GITHUB_SECRET");
+ return ResponseEntity.status(HttpStatus.FORBIDDEN)
+ .headers(LPVSWebhookUtil.generateSecurityHeaders())
+ .body(new LPVSResponseWrapper(ERROR));
+ }
+
+ // Validate and sanitize user inputs to prevent XSS attacks
+ gitHubOrg = HtmlUtils.htmlEscape(gitHubOrg);
+ gitHubRepo = HtmlUtils.htmlEscape(gitHubRepo);
+
+ GitHub gitHub = gitHubConnectionService.connectToGitHubApi();
+ GHRepository repository = gitHub.getRepository(gitHubOrg + "/" + gitHubRepo);
+ GHPullRequest pullRequest = repository.getPullRequest(prNumber);
+ LPVSQueue scanConfig = LPVSWebhookUtil.getGitHubWebhookConfig(repository, pullRequest);
+
+ if (scanConfig == null) {
+ log.error("Error with connection to GitHub.");
+ return ResponseEntity.status(HttpStatus.FORBIDDEN)
+ .headers(LPVSWebhookUtil.generateSecurityHeaders())
+ .body(new LPVSResponseWrapper(ERROR));
+ }
+ scanConfig.setAction(LPVSPullRequestAction.SINGLE_SCAN);
+ scanConfig.setAttempts(0);
+ scanConfig.setDate(new Date());
+ scanConfig.setReviewSystemType("github");
+ queueRepository.save(scanConfig);
+ log.debug("Pull request scanning is enabled");
+ gitHubService.setPendingCheck(scanConfig);
+ log.debug("Set status to Pending done");
+ queueService.addFirst(scanConfig);
+ log.debug("Put Scan config to the queue done");
+ log.debug("Response sent");
+ return ResponseEntity.ok()
+ .headers(LPVSWebhookUtil.generateSecurityHeaders())
+ .body(new LPVSResponseWrapper(SUCCESS));
+ }
+
/**
* Verifies if the signature matches the calculated signature using the GitHub secret.
*
diff --git a/src/main/java/com/lpvs/controller/package-info.java b/src/main/java/com/lpvs/controller/package-info.java
index 321d8a2e..214570b9 100644
--- a/src/main/java/com/lpvs/controller/package-info.java
+++ b/src/main/java/com/lpvs/controller/package-info.java
@@ -9,7 +9,7 @@
* This package contains the controller classes for handling various aspects of the License Pre-Validation Service (LPVS).
* Controllers in this package manage interactions related to GitHub webhooks, user interfaces, and API endpoints.
*
- * - {@link com.lpvs.controller.GitHubWebhooksController}: Manages GitHub webhook events, processes payloads, and interacts
+ * - {@link com.lpvs.controller.GitHubController}: Manages GitHub webhook events, processes payloads, and interacts
* with LPVS services for queue handling and GitHub operations.
*
* - {@link com.lpvs.controller.LPVSWebController}: Controls the web interface and API endpoints for LPVS, including user
diff --git a/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java b/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java
index 4d3ac815..3b684ca8 100644
--- a/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java
+++ b/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java
@@ -9,7 +9,7 @@
import lombok.Getter;
import lombok.Setter;
-import jakarta.persistence.*;
+import jakarta.persistence.*;
import java.io.Serializable;
/**
diff --git a/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java b/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java
index b6ae7336..c07c8048 100644
--- a/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java
+++ b/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java
@@ -10,7 +10,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
-import jakarta.persistence.*;
+import jakarta.persistence.*;
import java.io.Serializable;
/**
diff --git a/src/main/java/com/lpvs/entity/LPVSPullRequest.java b/src/main/java/com/lpvs/entity/LPVSPullRequest.java
index bfc1c007..54249512 100644
--- a/src/main/java/com/lpvs/entity/LPVSPullRequest.java
+++ b/src/main/java/com/lpvs/entity/LPVSPullRequest.java
@@ -11,7 +11,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
-import jakarta.persistence.*;
+import jakarta.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
diff --git a/src/main/java/com/lpvs/entity/LPVSQueue.java b/src/main/java/com/lpvs/entity/LPVSQueue.java
index 1473e016..d86e33c7 100644
--- a/src/main/java/com/lpvs/entity/LPVSQueue.java
+++ b/src/main/java/com/lpvs/entity/LPVSQueue.java
@@ -10,7 +10,7 @@
import lombok.Getter;
import lombok.Setter;
-import jakarta.persistence.*;
+import jakarta.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
diff --git a/src/main/java/com/lpvs/entity/enums/LPVSPullRequestAction.java b/src/main/java/com/lpvs/entity/enums/LPVSPullRequestAction.java
index 2ead8219..d73bbe43 100644
--- a/src/main/java/com/lpvs/entity/enums/LPVSPullRequestAction.java
+++ b/src/main/java/com/lpvs/entity/enums/LPVSPullRequestAction.java
@@ -35,7 +35,12 @@ public enum LPVSPullRequestAction {
/**
* Represents the action of triggering a rescan of a pull request.
*/
- RESCAN("rescan");
+ RESCAN("rescan"),
+
+ /**
+ * Represents the action of triggering a manual single scan of a pull request.
+ */
+ SINGLE_SCAN("single-scan");
/**
* The string representation of the pull request action.
@@ -77,6 +82,8 @@ public static LPVSPullRequestAction convertFrom(String action) {
return UPDATE;
} else if (action.equals(RESCAN.getPullRequestAction())) {
return RESCAN;
+ } else if (action.equals(SINGLE_SCAN.getPullRequestAction())) {
+ return SINGLE_SCAN;
} else {
return null;
}
diff --git a/src/main/java/com/lpvs/service/LPVSDetectService.java b/src/main/java/com/lpvs/service/LPVSDetectService.java
index e3bb47f1..3d29286a 100644
--- a/src/main/java/com/lpvs/service/LPVSDetectService.java
+++ b/src/main/java/com/lpvs/service/LPVSDetectService.java
@@ -13,9 +13,6 @@
import com.lpvs.util.LPVSFileUtil;
import lombok.extern.slf4j.Slf4j;
-import org.kohsuke.github.GHPullRequest;
-import org.kohsuke.github.GHRepository;
-import org.kohsuke.github.GitHub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ExitCodeEvent;
@@ -25,15 +22,12 @@
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;
-import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PostConstruct;
-
-import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
/**
@@ -63,6 +57,11 @@ public class LPVSDetectService {
*/
private LPVSLicenseService licenseService;
+ /**
+ * Service responsible for GitHub connection and operation.
+ */
+ private LPVSGitHubService gitHubService;
+
/**
* Event publisher for triggering application events.
*/
@@ -92,17 +91,20 @@ public class LPVSDetectService {
* @param gitHubConnectionService Service for connecting to the GitHub API.
* @param scanossDetectService Service for license detection using ScanOSS.
* @param licenseService Service for license conflict analysis.
+ * @param gitHubService Service for GitHub connection and operation.
*/
@Autowired
public LPVSDetectService(
@Value("${scanner:scanoss}") String scannerType,
LPVSGitHubConnectionService gitHubConnectionService,
LPVSScanossDetectService scanossDetectService,
- LPVSLicenseService licenseService) {
+ LPVSLicenseService licenseService,
+ LPVSGitHubService gitHubService) {
this.scannerType = scannerType;
this.gitHubConnectionService = gitHubConnectionService;
this.scanossDetectService = scanossDetectService;
this.licenseService = licenseService;
+ this.gitHubService = gitHubService;
}
/**
@@ -122,12 +124,11 @@ public void runOneScan() {
if (trigger != null && !HtmlUtils.htmlEscape(trigger).equals("")) {
try {
LPVSQueue webhookConfig =
- this.getInternalQueueByPullRequest(HtmlUtils.htmlEscape(trigger));
+ gitHubService.getInternalQueueByPullRequest(HtmlUtils.htmlEscape(trigger));
List scanResult =
this.runScan(
- webhookConfig,
- LPVSDetectService.getPathByPullRequest(webhookConfig));
+ webhookConfig, LPVSFileUtil.getPathByPullRequest(webhookConfig));
List> detectedConflicts =
licenseService.findConflicts(webhookConfig, scanResult);
@@ -162,73 +163,6 @@ public void runOneScan() {
}
}
- /**
- * Retrieves an LPVSQueue configuration based on the GitHub repository and pull request.
- *
- * @param repo The GitHub repository.
- * @param pR The GitHub pull request.
- * @return LPVSQueue configuration for the given GitHub repository and pull request.
- */
- private static LPVSQueue getGitHubWebhookConfig(GHRepository repo, GHPullRequest pR) {
- LPVSQueue webhookConfig = new LPVSQueue();
- webhookConfig.setPullRequestUrl(
- pR.getHtmlUrl() != null ? pR.getHtmlUrl().toString() : null);
- if (pR.getHead() != null
- && pR.getHead().getRepository() != null
- && pR.getHead().getRepository().getHtmlUrl() != null) {
- webhookConfig.setPullRequestFilesUrl(
- pR.getHead().getRepository().getHtmlUrl().toString());
- } else {
- webhookConfig.setPullRequestFilesUrl(webhookConfig.getPullRequestUrl());
- }
- webhookConfig.setPullRequestAPIUrl(pR.getUrl() != null ? pR.getUrl().toString() : null);
- webhookConfig.setRepositoryUrl(
- repo.getHtmlUrl() != null ? repo.getHtmlUrl().toString() : null);
- webhookConfig.setUserId("Single scan run");
- webhookConfig.setHeadCommitSHA(pR.getHead() != null ? pR.getHead().getSha() : null);
- return webhookConfig;
- }
-
- /**
- * Retrieves the LPVSQueue configuration for a given GitHub pull request URL.
- *
- * @param pullRequest The GitHub pull request URL.
- * @return LPVSQueue configuration for the given pull request.
- */
- public LPVSQueue getInternalQueueByPullRequest(String pullRequest) {
- try {
- if (pullRequest == null) return null;
- String[] pullRequestSplit = pullRequest.split("/");
- if (pullRequestSplit.length < 5) return null;
- String pullRequestRepo =
- String.join(
- "/",
- Arrays.asList(pullRequestSplit)
- .subList(
- pullRequestSplit.length - 4,
- pullRequestSplit.length - 2));
- int pullRequestNum = Integer.parseInt(pullRequestSplit[pullRequestSplit.length - 1]);
- GitHub gitHub = gitHubConnectionService.connectToGitHubApi();
- GHRepository repo = gitHub.getRepository(pullRequestRepo);
- GHPullRequest pR = repo.getPullRequest(pullRequestNum);
- return LPVSDetectService.getGitHubWebhookConfig(repo, pR);
- } catch (IOException e) {
- log.error("Can't set up github client: " + e);
- }
- return null;
- }
-
- /**
- * Retrieves the local directory path for a given LPVSQueue configuration.
- *
- * @param webhookConfig LPVSQueue configuration.
- * @return Local directory path for the given LPVSQueue.
- */
- public static String getPathByPullRequest(LPVSQueue webhookConfig) {
- if (webhookConfig == null) return null;
- return LPVSFileUtil.getLocalDirectoryPath(webhookConfig);
- }
-
/**
* Runs a license scan based on the selected scanner type.
*
diff --git a/src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java b/src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java
index 4c5a8cae..f4ecb4f2 100644
--- a/src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java
+++ b/src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java
@@ -12,7 +12,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
-import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PostConstruct;
import java.io.IOException;
import java.util.Optional;
diff --git a/src/main/java/com/lpvs/service/LPVSGitHubService.java b/src/main/java/com/lpvs/service/LPVSGitHubService.java
index 18943c65..10e3832c 100644
--- a/src/main/java/com/lpvs/service/LPVSGitHubService.java
+++ b/src/main/java/com/lpvs/service/LPVSGitHubService.java
@@ -28,6 +28,7 @@
import org.springframework.stereotype.Service;
import java.io.IOException;
+import java.util.Arrays;
import java.util.List;
/**
@@ -413,4 +414,35 @@ public String getRepositoryLicense(LPVSQueue webhookConfig) {
}
return "Proprietary";
}
+
+ /**
+ * Retrieves the LPVSQueue configuration for a given GitHub pull request URL.
+ *
+ * @param pullRequest The GitHub pull request URL.
+ * @return LPVSQueue configuration for the given pull request.
+ */
+ public LPVSQueue getInternalQueueByPullRequest(String pullRequest) {
+ try {
+ if (pullRequest == null) {
+ return null;
+ }
+ String[] pullRequestSplit = pullRequest.split("/");
+ if (pullRequestSplit.length < 5) return null;
+ String pullRequestRepo =
+ String.join(
+ "/",
+ Arrays.asList(pullRequestSplit)
+ .subList(
+ pullRequestSplit.length - 4,
+ pullRequestSplit.length - 2));
+ int pullRequestNum = Integer.parseInt(pullRequestSplit[pullRequestSplit.length - 1]);
+ GitHub gitHub = gitHubConnectionService.connectToGitHubApi();
+ GHRepository repo = gitHub.getRepository(pullRequestRepo);
+ GHPullRequest pR = repo.getPullRequest(pullRequestNum);
+ return LPVSWebhookUtil.getGitHubWebhookConfig(repo, pR);
+ } catch (IOException e) {
+ log.error("Can't set up github client: " + e);
+ }
+ return null;
+ }
}
diff --git a/src/main/java/com/lpvs/service/LPVSLicenseService.java b/src/main/java/com/lpvs/service/LPVSLicenseService.java
index 9261017e..1372dd67 100644
--- a/src/main/java/com/lpvs/service/LPVSLicenseService.java
+++ b/src/main/java/com/lpvs/service/LPVSLicenseService.java
@@ -18,7 +18,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
-import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PostConstruct;
import java.util.*;
diff --git a/src/main/java/com/lpvs/service/LPVSQueueService.java b/src/main/java/com/lpvs/service/LPVSQueueService.java
index c9dc4878..977cf8a2 100644
--- a/src/main/java/com/lpvs/service/LPVSQueueService.java
+++ b/src/main/java/com/lpvs/service/LPVSQueueService.java
@@ -14,7 +14,6 @@
import com.lpvs.repository.LPVSQueueRepository;
import com.lpvs.util.LPVSWebhookUtil;
import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@@ -211,8 +210,8 @@ public LPVSQueue getLatestScan(List webhookConfigList) {
public void processWebHook(LPVSQueue webhookConfig) {
LPVSPullRequest pullRequest = new LPVSPullRequest();
try {
- log.info("GitHub Webhook processing...");
- log.info(webhookConfig.toString());
+ log.info("GitHub queue processing...");
+ log.debug(webhookConfig.toString());
String filePath = gitHubService.getPullRequestFiles(webhookConfig);
diff --git a/src/main/java/com/lpvs/util/LPVSFileUtil.java b/src/main/java/com/lpvs/util/LPVSFileUtil.java
index 64db0ae1..7fe1643c 100644
--- a/src/main/java/com/lpvs/util/LPVSFileUtil.java
+++ b/src/main/java/com/lpvs/util/LPVSFileUtil.java
@@ -196,4 +196,15 @@ public static String getScanResultsDirectoryPath(LPVSQueue webhookConfig) {
+ "Results/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig);
}
+
+ /**
+ * Retrieves the local directory path for a given LPVSQueue configuration.
+ *
+ * @param webhookConfig LPVSQueue configuration.
+ * @return Local directory path for the given LPVSQueue.
+ */
+ public static String getPathByPullRequest(LPVSQueue webhookConfig) {
+ if (webhookConfig == null) return null;
+ return getLocalDirectoryPath(webhookConfig);
+ }
}
diff --git a/src/main/java/com/lpvs/util/LPVSWebhookUtil.java b/src/main/java/com/lpvs/util/LPVSWebhookUtil.java
index 2f7f6cd8..5d71cdea 100644
--- a/src/main/java/com/lpvs/util/LPVSWebhookUtil.java
+++ b/src/main/java/com/lpvs/util/LPVSWebhookUtil.java
@@ -11,6 +11,8 @@
import com.lpvs.entity.LPVSQueue;
import com.lpvs.entity.enums.LPVSPullRequestAction;
import lombok.extern.slf4j.Slf4j;
+import org.kohsuke.github.GHPullRequest;
+import org.kohsuke.github.GHRepository;
import org.springframework.http.HttpHeaders;
import java.util.Arrays;
@@ -239,4 +241,31 @@ public static HttpHeaders generateSecurityHeaders() {
return headers;
}
+
+ /**
+ * Retrieves an LPVSQueue configuration based on the GitHub repository and pull request.
+ *
+ * @param repo The GitHub repository.
+ * @param pR The GitHub pull request.
+ * @return LPVSQueue configuration for the given GitHub repository and pull request.
+ */
+ public static LPVSQueue getGitHubWebhookConfig(GHRepository repo, GHPullRequest pR) {
+ LPVSQueue webhookConfig = new LPVSQueue();
+ webhookConfig.setPullRequestUrl(
+ pR.getHtmlUrl() != null ? pR.getHtmlUrl().toString() : null);
+ if (pR.getHead() != null
+ && pR.getHead().getRepository() != null
+ && pR.getHead().getRepository().getHtmlUrl() != null) {
+ webhookConfig.setPullRequestFilesUrl(
+ pR.getHead().getRepository().getHtmlUrl().toString());
+ } else {
+ webhookConfig.setPullRequestFilesUrl(webhookConfig.getPullRequestUrl());
+ }
+ webhookConfig.setPullRequestAPIUrl(pR.getUrl() != null ? pR.getUrl().toString() : null);
+ webhookConfig.setRepositoryUrl(
+ repo.getHtmlUrl() != null ? repo.getHtmlUrl().toString() : null);
+ webhookConfig.setUserId("Single scan run");
+ webhookConfig.setHeadCommitSHA(pR.getHead() != null ? pR.getHead().getSha() : null);
+ return webhookConfig;
+ }
}
diff --git a/src/test/java/com/lpvs/config/SecurityConfigTest.java b/src/test/java/com/lpvs/config/SecurityConfigTest.java
index ac99539b..6ab4b8a1 100644
--- a/src/test/java/com/lpvs/config/SecurityConfigTest.java
+++ b/src/test/java/com/lpvs/config/SecurityConfigTest.java
@@ -14,9 +14,9 @@
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
-import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
+import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
diff --git a/src/test/java/com/lpvs/controller/GitHubWebhooksControllerTest.java b/src/test/java/com/lpvs/controller/GitHubControllerTest.java
similarity index 63%
rename from src/test/java/com/lpvs/controller/GitHubWebhooksControllerTest.java
rename to src/test/java/com/lpvs/controller/GitHubControllerTest.java
index 47135e7c..4cc3280c 100644
--- a/src/test/java/com/lpvs/controller/GitHubWebhooksControllerTest.java
+++ b/src/test/java/com/lpvs/controller/GitHubControllerTest.java
@@ -6,8 +6,10 @@
*/
package com.lpvs.controller;
+import com.lpvs.entity.LPVSQueue;
import com.lpvs.entity.LPVSResponseWrapper;
import com.lpvs.repository.LPVSQueueRepository;
+import com.lpvs.service.LPVSGitHubConnectionService;
import com.lpvs.service.LPVSGitHubService;
import com.lpvs.service.LPVSQueueService;
@@ -19,19 +21,23 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
+import org.kohsuke.github.GHPullRequest;
+import org.kohsuke.github.GHRepository;
+import org.kohsuke.github.GitHub;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
+import java.lang.reflect.Method;
+
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
-import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.*;
@Slf4j
@ExtendWith(SystemStubsExtension.class)
-public class GitHubWebhooksControllerTest {
+public class GitHubControllerTest {
- @SystemStub
- private EnvironmentVariables environmentVars;
+ @SystemStub private EnvironmentVariables environmentVars;
private static final String SIGNATURE = "X-Hub-Signature-256";
private static final String SUCCESS = "Success";
@@ -39,13 +45,18 @@ public class GitHubWebhooksControllerTest {
private LPVSExitHandler exitHandler;
+ GitHub gitHub = mock(GitHub.class);
+ GHRepository ghRepository = mock(GHRepository.class);
+ GHPullRequest ghPullRequest = mock(GHPullRequest.class);
LPVSQueueService mocked_instance_queueServ = mock(LPVSQueueService.class);
LPVSGitHubService mocked_instance_ghServ = mock(LPVSGitHubService.class);
LPVSQueueRepository mocked_queueRepo = mock(LPVSQueueRepository.class);
- GitHubWebhooksController gitHubWebhooksController =
- new GitHubWebhooksController(
+ LPVSGitHubConnectionService mocked_ghConnServ = mock(LPVSGitHubConnectionService.class);
+ GitHubController gitHubController =
+ new GitHubController(
mocked_instance_queueServ,
mocked_instance_ghServ,
+ mocked_ghConnServ,
mocked_queueRepo,
"",
exitHandler);
@@ -54,7 +65,7 @@ public class GitHubWebhooksControllerTest {
public void noSignatureTest() {
ResponseEntity actual;
try {
- actual = gitHubWebhooksController.gitHubWebhooks(null, null);
+ actual = gitHubController.gitHubWebhooks(null, null);
} catch (Exception e) {
actual = null;
}
@@ -67,7 +78,7 @@ public void noSignatureTest() {
public void noPayloadTest() {
ResponseEntity actual;
try {
- actual = gitHubWebhooksController.gitHubWebhooks(SIGNATURE, null);
+ actual = gitHubController.gitHubWebhooks(SIGNATURE, null);
} catch (Exception e) {
actual = null;
}
@@ -116,7 +127,7 @@ public void okTest() {
+ "}";
try {
- actual = gitHubWebhooksController.gitHubWebhooks(SIGNATURE, json_to_test);
+ actual = gitHubController.gitHubWebhooks(SIGNATURE, json_to_test);
} catch (Exception e) {
log.error(e.getMessage());
actual = null;
@@ -132,7 +143,8 @@ public void wrongSecretTest() {
environmentVars.set("LPVS_GITHUB_SECRET", "LPVS");
- String signature = "sha256=c0ca451d2e2a7ea7d50bb29383996a35f43c7a9df0810bd6ffc45cefc8d1ce42";
+ String signature =
+ "sha256=c0ca451d2e2a7ea7d50bb29383996a35f43c7a9df0810bd6ffc45cefc8d1ce42";
String json_to_test =
"{"
@@ -156,14 +168,57 @@ public void wrongSecretTest() {
+ "}"
+ "}";
try {
- gitHubWebhooksController.initializeGitHubSecret();
- boolean secret = gitHubWebhooksController.wrongSecret(signature, json_to_test);
+ gitHubController.initializeGitHubController();
+ boolean secret = gitHubController.wrongSecret(signature, json_to_test);
assertEquals(secret, false);
- secret = gitHubWebhooksController.wrongSecret(signature + " ", json_to_test);
+ secret = gitHubController.wrongSecret(signature + " ", json_to_test);
assertEquals(secret, true);
} catch (Exception e) {
- log.error("GitHubWebhooksControllerTest::wrongSecretTest exception: " + e);
+ log.error("GitHubControllerTest::wrongSecretTest exception: " + e);
fail();
}
}
+
+ @Test
+ public void testGitHubSingleScan_Success() throws Exception {
+ environmentVars.set("LPVS_GITHUB_SECRET", "LPVS");
+ Method method = gitHubController.getClass().getDeclaredMethod("initializeGitHubController");
+ method.setAccessible(true);
+ method.invoke(gitHubController);
+ LPVSQueue mockScanConfig = new LPVSQueue();
+ when(mocked_instance_ghServ.getInternalQueueByPullRequest(anyString()))
+ .thenReturn(mockScanConfig);
+ when(mocked_queueRepo.save(any())).thenReturn(mockScanConfig);
+ doNothing().when(mocked_instance_queueServ).addFirst(any());
+
+ when(mocked_ghConnServ.connectToGitHubApi()).thenReturn(gitHub);
+ when(gitHub.getRepository("org/repo")).thenReturn(ghRepository);
+ when(ghRepository.getPullRequest(1)).thenReturn(ghPullRequest);
+
+ ResponseEntity responseEntity =
+ gitHubController.gitHubSingleScan("org", "repo", 1);
+
+ assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
+ }
+
+ @Test
+ public void testGitHubSingleScan_InvalidSecret() throws Exception {
+ environmentVars.set("LPVS_GITHUB_SECRET", "LPVS");
+ when(mocked_instance_ghServ.getInternalQueueByPullRequest(anyString())).thenReturn(null);
+ ResponseEntity responseEntity =
+ gitHubController.gitHubSingleScan("org", "repo", 1);
+
+ assertEquals(HttpStatus.FORBIDDEN, responseEntity.getStatusCode());
+ }
+
+ @Test
+ public void testGitHubSingleScan_ConnectionError() throws Exception {
+ environmentVars.set("LPVS_GITHUB_SECRET", "LPVS");
+ when(mocked_instance_ghServ.getInternalQueueByPullRequest(anyString()))
+ .thenThrow(new RuntimeException("Connection error"));
+ ResponseEntity responseEntity =
+ gitHubController.gitHubSingleScan("org", "repo", 1);
+
+ assertEquals(HttpStatus.FORBIDDEN, responseEntity.getStatusCode());
+ }
}
diff --git a/src/test/java/com/lpvs/entity/enums/LPVSPullRequestActionTest.java b/src/test/java/com/lpvs/entity/enums/LPVSPullRequestActionTest.java
index 8dae36b5..d2973d73 100644
--- a/src/test/java/com/lpvs/entity/enums/LPVSPullRequestActionTest.java
+++ b/src/test/java/com/lpvs/entity/enums/LPVSPullRequestActionTest.java
@@ -20,6 +20,9 @@ public void testConvertFrom() {
assertEquals(
LPVSPullRequestAction.convertFrom("synchronize"), LPVSPullRequestAction.UPDATE);
assertEquals(LPVSPullRequestAction.convertFrom("rescan"), LPVSPullRequestAction.RESCAN);
+ assertEquals(
+ LPVSPullRequestAction.convertFrom("single-scan"),
+ LPVSPullRequestAction.SINGLE_SCAN);
assertNotEquals(
LPVSPullRequestAction.convertFrom("random_name"), LPVSPullRequestAction.OPEN);
@@ -31,6 +34,9 @@ public void testConvertFrom() {
LPVSPullRequestAction.convertFrom("random_name"), LPVSPullRequestAction.UPDATE);
assertNotEquals(
LPVSPullRequestAction.convertFrom("random_name"), LPVSPullRequestAction.RESCAN);
+ assertNotEquals(
+ LPVSPullRequestAction.convertFrom("random_name"),
+ LPVSPullRequestAction.SINGLE_SCAN);
assertNull(LPVSPullRequestAction.convertFrom("random_name"));
}
@@ -42,5 +48,6 @@ public void testGetPullRequestAction() {
assertEquals(LPVSPullRequestAction.CLOSE.getPullRequestAction(), "closed");
assertEquals(LPVSPullRequestAction.UPDATE.getPullRequestAction(), "synchronize");
assertEquals(LPVSPullRequestAction.RESCAN.getPullRequestAction(), "rescan");
+ assertEquals(LPVSPullRequestAction.SINGLE_SCAN.getPullRequestAction(), "single-scan");
}
}
diff --git a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java
index 3bd4a073..75d07814 100644
--- a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java
+++ b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java
@@ -11,6 +11,7 @@
import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService;
import com.lpvs.util.LPVSCommentUtil;
+import com.lpvs.util.LPVSFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
@@ -24,8 +25,6 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import org.springframework.boot.context.event.ApplicationReadyEvent;
-import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import java.io.IOException;
@@ -39,7 +38,6 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -51,25 +49,14 @@ public class LPVSDetectServiceTest {
@Mock private ApplicationEventPublisher mockEventPublisher;
- @Mock private LPVSGitHubConnectionService gitHubConnectionService;
-
- @Mock private GitHub gitHub;
-
- @Mock private GHRepository ghRepository;
-
- @Mock private GHPullRequest ghPullRequest;
-
@Mock private LPVSScanossDetectService scanossDetectService;
- @Mock private ApplicationContext applicationContext;
-
- @Mock private ApplicationReadyEvent applicationReadyEvent;
-
@InjectMocks private LPVSDetectService lpvsDetectService;
@Nested
class TestInit {
- final LPVSDetectService detectService = new LPVSDetectService("scanoss", null, null, null);
+ final LPVSDetectService detectService =
+ new LPVSDetectService("scanoss", null, null, null, null);
@Test
public void testInit() {
@@ -90,6 +77,7 @@ class TestRunScan__Scanoss {
LPVSGitHubConnectionService github_mock = mock(LPVSGitHubConnectionService.class);
LPVSScanossDetectService scanoss_mock = mock(LPVSScanossDetectService.class);
LPVSLicenseService licenseservice_mock = mock(LPVSLicenseService.class);
+ LPVSGitHubService githubservice_mock = mock(LPVSGitHubService.class);
GitHub mockGitHub = mock(GitHub.class);
GHCommitPointer mockCommitPointer = mock(GHCommitPointer.class);
GHRepository mockRepository = mock(GHRepository.class);
@@ -105,7 +93,11 @@ class TestRunScan__Scanoss {
void setUp() throws IOException {
detectService =
new LPVSDetectService(
- "scanoss", github_mock, scanoss_mock, licenseservice_mock);
+ "scanoss",
+ github_mock,
+ scanoss_mock,
+ licenseservice_mock,
+ githubservice_mock);
webhookConfig = new LPVSQueue();
webhookConfig.setId(1L);
@@ -128,7 +120,7 @@ void setUp() throws IOException {
@Test
void testRunOneScanWithNullTriger() throws NoSuchFieldException, IllegalAccessException {
lpvsDetectService =
- spy(new LPVSDetectService("scanoss", null, scanossDetectService, null));
+ spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null));
setPrivateField(lpvsDetectService, "trigger", null);
setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher);
@@ -144,7 +136,7 @@ void testRunOneScanWithNullTriger() throws NoSuchFieldException, IllegalAccessEx
void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessException {
lpvsDetectService =
- spy(new LPVSDetectService("scanoss", null, scanossDetectService, null));
+ spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null));
setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value");
setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher);
@@ -163,7 +155,7 @@ void testRunOneScan_Branch2()
List.of(conflict_1, conflict_1);
lpvsDetectService =
- spy(new LPVSDetectService("scanoss", null, scanossDetectService, null));
+ spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null));
// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
@@ -205,7 +197,7 @@ void testRunOneScan_Branch3()
doNothing().when(mockEventPublisher).publishEvent(any());
lpvsDetectService =
- spy(new LPVSDetectService("scanoss", null, scanossDetectService, null));
+ spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null));
setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
@@ -330,7 +322,6 @@ void testRunOneScan_TriggerNotNull_Branch3() throws Exception {
when(mockPullRequest.getHead()).thenReturn(mockCommitPointer);
when(licenseservice_mock.findConflicts(webhookConfig, null)).thenReturn(expected);
when(mockCommitPointer.getRepository()).thenReturn(mockHeadRepository2);
- when(mockHeadRepository2.getHtmlUrl()).thenReturn(null);
// Set up expected values
String expectedPullRequestUrl = "https://example.com/pull/1";
@@ -362,39 +353,6 @@ void testCommentBuilder_ConflictFilePresent() throws Exception {
assertNotNull(commentHTML);
}
- @Test
- void testGetInternalQueueByPullRequestWithNull() throws IOException {
- LPVSQueue result = lpvsDetectService.getInternalQueueByPullRequest(null);
- assertNull(result);
- }
-
- @Test
- void testGetInternalQueueByPullRequest() throws IOException {
- String pullRequest = "github/owner/repo/branch/123";
- when(gitHubConnectionService.connectToGitHubApi()).thenReturn(gitHub);
- when(gitHub.getRepository("owner/repo")).thenReturn(ghRepository);
- when(ghRepository.getPullRequest(123)).thenReturn(ghPullRequest);
-
- LPVSQueue result = lpvsDetectService.getInternalQueueByPullRequest(pullRequest);
-
- assertNotNull(result);
- assertEquals(result.getUserId(), "Single scan run");
- }
-
- @Test
- void testGetInternalQueueByPullRequestError() throws IOException {
- String pullRequest = "github/owner/repo/branch/123";
-
- when(gitHubConnectionService.connectToGitHubApi()).thenThrow(IOException.class);
-
- try {
- LPVSQueue result = lpvsDetectService.getInternalQueueByPullRequest(pullRequest);
- assertNull(result, "Expected result to be null");
- } catch (Exception e) {
- fail("Exception not expected to be thrown here");
- }
- }
-
@Test
public void testGetPathByPullRequest() {
@@ -406,7 +364,7 @@ public void testGetPathByPullRequest() {
when(mockWebhookConfig.getPullRequestUrl())
.thenReturn("https://github.com/Samsung/LPVS/pull/1");
- String result = LPVSDetectService.getPathByPullRequest(mockWebhookConfig);
+ String result = LPVSFileUtil.getPathByPullRequest(mockWebhookConfig);
assertNotNull(result);
}
@@ -441,6 +399,7 @@ class TestRunScan__ScanossException {
LPVSGitHubConnectionService github_mock = mock(LPVSGitHubConnectionService.class);
LPVSScanossDetectService scanoss_mock = mock(LPVSScanossDetectService.class);
LPVSLicenseService licenseservice_mock = mock(LPVSLicenseService.class);
+ LPVSGitHubService githubservice_mock = mock(LPVSGitHubService.class);
LPVSQueue webhookConfig;
final String test_path = "test_path";
@@ -450,7 +409,11 @@ class TestRunScan__ScanossException {
void setUp() {
detectService =
new LPVSDetectService(
- "scanoss", github_mock, scanoss_mock, licenseservice_mock);
+ "scanoss",
+ github_mock,
+ scanoss_mock,
+ licenseservice_mock,
+ githubservice_mock);
webhookConfig = new LPVSQueue();
webhookConfig.setId(1L);
@@ -495,7 +458,7 @@ class TestRunScan__NotScanoss {
@BeforeEach
void setUp() {
- detectService = new LPVSDetectService("not_scanoss", null, null, null);
+ detectService = new LPVSDetectService("not_scanoss", null, null, null, null);
}
@Test
diff --git a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
index f67d3aa7..6c1de107 100644
--- a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
+++ b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
@@ -30,9 +30,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kohsuke.github.*;
-import org.mockito.MockedStatic;
-import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
+import org.mockito.*;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException;
@@ -54,10 +53,10 @@
@Slf4j
@ExtendWith(SystemStubsExtension.class)
+@ExtendWith(MockitoExtension.class)
public class LPVSGitHubServiceTest {
- @SystemStub
- private EnvironmentVariables environmentVars;
+ @SystemStub private EnvironmentVariables environmentVars;
/**
* Helper class to mock `GHPullRequest`, because we cannot mock it via Mockito.
@@ -69,6 +68,16 @@ public class LPVSGitHubServiceTest {
*/
private LPVSExitHandler exitHandler;
+ @Mock private GitHub gitHub;
+
+ @Mock private GHRepository ghRepository;
+
+ @Mock private GHPullRequest ghPullRequest;
+
+ @Mock private LPVSGitHubConnectionService gitHubConnectionService;
+
+ @InjectMocks private LPVSGitHubService gitHubService;
+
static class GHPullRequestOurMock extends GHPullRequest {
private final URL mockedGetUrl;
private final String mockedGetTitle;
@@ -4347,25 +4356,10 @@ public void testCheckNotEmpty()
String GH_LOGIN = "";
String GH_AUTH_TOKEN = "non-empty";
String GH_API_URL = "";
- LPVSPullRequestRepository mocked_pullRequestRepository =
- mock(LPVSPullRequestRepository.class);
- LPVSDetectedLicenseRepository mocked_lpvsDetectedLicenseRepository =
- mock(LPVSDetectedLicenseRepository.class);
- LPVSLicenseRepository mocked_lpvsLicenseRepository = mock(LPVSLicenseRepository.class);
- LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository =
- mock(LPVSLicenseConflictRepository.class);
LPVSExitHandler exitHandler = mock(LPVSExitHandler.class);
LPVSGitHubConnectionService lpvsGitHubConnectionService =
new LPVSGitHubConnectionService(
GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler);
-
- final LPVSGitHubService gh_service =
- new LPVSGitHubService(
- mocked_pullRequestRepository,
- mocked_lpvsDetectedLicenseRepository,
- mocked_lpvsLicenseRepository,
- mocked_lpvsLicenseConflictRepository,
- lpvsGitHubConnectionService);
Method method = lpvsGitHubConnectionService.getClass().getDeclaredMethod("checks");
method.setAccessible(true);
method.invoke(lpvsGitHubConnectionService);
@@ -4373,4 +4367,42 @@ public void testCheckNotEmpty()
verify(exitHandler, never()).exit(anyInt());
}
}
+
+ @Nested
+ class getInternalQueueByPullRequests {
+
+ @Test
+ void testGetInternalQueueByPullRequestWithNull() {
+ LPVSQueue result = gitHubService.getInternalQueueByPullRequest(null);
+ assertNull(result);
+ }
+
+ @Test
+ void testGetInternalQueueByPullRequest() throws IOException {
+ String pullRequest = "github/owner/repo/branch/123";
+
+ when(gitHubConnectionService.connectToGitHubApi()).thenReturn(gitHub);
+ when(gitHub.getRepository("owner/repo")).thenReturn(ghRepository);
+ when(ghRepository.getPullRequest(123)).thenReturn(ghPullRequest);
+
+ LPVSQueue result = gitHubService.getInternalQueueByPullRequest(pullRequest);
+
+ assertNotNull(result);
+ assertEquals(result.getUserId(), "Single scan run");
+ }
+
+ @Test
+ void testGetInternalQueueByPullRequestError() throws IOException {
+ String pullRequest = "github/owner/repo/branch/123";
+
+ when(gitHubConnectionService.connectToGitHubApi()).thenThrow(IOException.class);
+
+ try {
+ LPVSQueue result = gitHubService.getInternalQueueByPullRequest(pullRequest);
+ assertNull(result, "Expected result to be null");
+ } catch (Exception e) {
+ fail("Exception not expected to be thrown here");
+ }
+ }
+ }
}
diff --git a/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java b/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
index 9d7cb3a0..b56f627e 100644
--- a/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
+++ b/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
@@ -42,8 +42,7 @@
@ExtendWith(SystemStubsExtension.class)
public class LPVSLicenseServiceTest {
- @SystemStub
- private EnvironmentVariables environmentVars;
+ @SystemStub private EnvironmentVariables environmentVars;
private LPVSExitHandler exitHandler;
diff --git a/src/test/java/com/lpvs/util/LPVSWebhookUtilTestFuzzer.java b/src/test/java/com/lpvs/util/LPVSWebhookUtilTestFuzzer.java
index 89314570..99639b75 100644
--- a/src/test/java/com/lpvs/util/LPVSWebhookUtilTestFuzzer.java
+++ b/src/test/java/com/lpvs/util/LPVSWebhookUtilTestFuzzer.java
@@ -16,11 +16,11 @@
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import static org.junit.jupiter.api.Assertions.*;
+
@ExtendWith(SystemStubsExtension.class)
public class LPVSWebhookUtilTestFuzzer {
- @SystemStub
- private EnvironmentVariables environmentVars;
+ @SystemStub private EnvironmentVariables environmentVars;
@FuzzTest(maxDuration = "60s")
public void fuzzTestGetPullRequestId(FuzzedDataProvider data) {