Skip to content

Commit

Permalink
refactor: Rename util class and move common methods to util class (#515)
Browse files Browse the repository at this point in the history
* refactor: Rename util class and move common methods to util class

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

* test: Add unit tests for missing methods

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

* fix: Fix the description of createInputStreamReader method

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

---------

Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored May 3, 2024
1 parent 68f16d6 commit 0dd4336
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 297 deletions.
22 changes: 11 additions & 11 deletions src/main/java/com/lpvs/controller/GitHubController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.lpvs.service.LPVSGitHubService;
import com.lpvs.service.LPVSQueueService;
import com.lpvs.util.LPVSExitHandler;
import com.lpvs.util.LPVSWebhookUtil;
import com.lpvs.util.LPVSPayloadUtil;
import com.lpvs.entity.LPVSResponseWrapper;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -146,13 +146,13 @@ public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(
if (!StringUtils.hasText(signature) || signature.length() > 72) {
log.error("Received empty or too long signature");
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.headers(LPVSWebhookUtil.generateSecurityHeaders())
.headers(LPVSPayloadUtil.generateSecurityHeaders())
.body(new LPVSResponseWrapper(ERROR));
}
if (!GITHUB_SECRET.trim().isEmpty() && wrongSecret(signature, payload)) {
log.error("Received empty or incorrect GITHUB_SECRET");
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.headers(LPVSWebhookUtil.generateSecurityHeaders())
.headers(LPVSPayloadUtil.generateSecurityHeaders())
.body(new LPVSResponseWrapper(ERROR));
}

Expand All @@ -161,10 +161,10 @@ public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(
log.debug("Response to empty payload sent");
// Implement Content Security Policy (CSP) headers
return ResponseEntity.ok()
.headers(LPVSWebhookUtil.generateSecurityHeaders())
.headers(LPVSPayloadUtil.generateSecurityHeaders())
.body(new LPVSResponseWrapper(SUCCESS));
} else if (LPVSWebhookUtil.checkPayload(payload)) {
LPVSQueue webhookConfig = LPVSWebhookUtil.getGitHubWebhookConfig(payload);
} else if (LPVSPayloadUtil.checkPayload(payload)) {
LPVSQueue webhookConfig = LPVSPayloadUtil.getGitHubWebhookConfig(payload);
webhookConfig.setDate(new Date());
webhookConfig.setReviewSystemType("github");
queueRepository.save(webhookConfig);
Expand All @@ -177,7 +177,7 @@ public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(

log.debug("Response sent");
return ResponseEntity.ok()
.headers(LPVSWebhookUtil.generateSecurityHeaders())
.headers(LPVSPayloadUtil.generateSecurityHeaders())
.body(new LPVSResponseWrapper(SUCCESS));
}

Expand Down Expand Up @@ -208,7 +208,7 @@ public ResponseEntity<LPVSResponseWrapper> gitHubSingleScan(
if (GITHUB_SECRET.trim().isEmpty()) {
log.error("Received empty GITHUB_SECRET");
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.headers(LPVSWebhookUtil.generateSecurityHeaders())
.headers(LPVSPayloadUtil.generateSecurityHeaders())
.body(new LPVSResponseWrapper(ERROR));
}

Expand All @@ -220,7 +220,7 @@ public ResponseEntity<LPVSResponseWrapper> gitHubSingleScan(
GitHub gitHub = gitHubConnectionService.connectToGitHubApi();
GHRepository repository = gitHub.getRepository(gitHubOrg + "/" + gitHubRepo);
GHPullRequest pullRequest = repository.getPullRequest(prNumber);
LPVSQueue scanConfig = LPVSWebhookUtil.getGitHubWebhookConfig(repository, pullRequest);
LPVSQueue scanConfig = LPVSPayloadUtil.getGitHubWebhookConfig(repository, pullRequest);
scanConfig.setAction(LPVSPullRequestAction.SINGLE_SCAN);
scanConfig.setAttempts(0);
scanConfig.setDate(new Date());
Expand All @@ -233,13 +233,13 @@ public ResponseEntity<LPVSResponseWrapper> gitHubSingleScan(
log.debug("Put Scan config to the queue done");
log.debug("Response sent");
return ResponseEntity.ok()
.headers(LPVSWebhookUtil.generateSecurityHeaders())
.headers(LPVSPayloadUtil.generateSecurityHeaders())
.body(new LPVSResponseWrapper(SUCCESS));
} catch (Exception e) {
log.error("Can't authorize single pull request scan " + e.getMessage());
}
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.headers(LPVSWebhookUtil.generateSecurityHeaders())
.headers(LPVSPayloadUtil.generateSecurityHeaders())
.body(new LPVSResponseWrapper(ERROR));
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/lpvs/controller/LPVSWebController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.service.LPVSLoginCheckService;
import com.lpvs.service.LPVSStatisticsService;
import com.lpvs.util.LPVSWebhookUtil;
import com.lpvs.util.LPVSPayloadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.context.annotation.Profile;
Expand Down Expand Up @@ -130,7 +130,7 @@ class WebApiEndpoints {
@ResponseBody
public ResponseEntity<LPVSMember> personalInfoSettings(Authentication authentication) {
lpvsLoginCheckService.loginVerification(authentication);
HttpHeaders headers = LPVSWebhookUtil.generateSecurityHeaders();
HttpHeaders headers = LPVSPayloadUtil.generateSecurityHeaders();
LPVSMember member = lpvsLoginCheckService.getMemberFromMemberMap(authentication);
sanitizeUserInputs(member);
return ResponseEntity.ok().headers(headers).body(member);
Expand All @@ -149,7 +149,7 @@ public ResponseEntity<LPVSLoginMember> loginMember(Authentication authentication
lpvsLoginCheckService.loginVerification(authentication);

// Include security headers in the response
HttpHeaders headers = LPVSWebhookUtil.generateSecurityHeaders();
HttpHeaders headers = LPVSPayloadUtil.generateSecurityHeaders();

Map<String, Object> oauthLoginMemberMap =
lpvsLoginCheckService.getOauthLoginMemberMap(authentication);
Expand Down Expand Up @@ -186,7 +186,7 @@ public ResponseEntity<LPVSMember> postSettingTest(
lpvsLoginCheckService.loginVerification(authentication);

// Include security headers in the response
HttpHeaders headers = LPVSWebhookUtil.generateSecurityHeaders();
HttpHeaders headers = LPVSPayloadUtil.generateSecurityHeaders();

LPVSMember findMember = lpvsLoginCheckService.getMemberFromMemberMap(authentication);
try {
Expand Down Expand Up @@ -221,7 +221,7 @@ public ResponseEntity<HistoryEntity> newHistoryPageByUser(
Pageable pageable,
Authentication authentication) {

HttpHeaders headers = LPVSWebhookUtil.generateSecurityHeaders();
HttpHeaders headers = LPVSPayloadUtil.generateSecurityHeaders();

HistoryPageEntity historyPageEntity =
lpvsLoginCheckService.pathCheck(type, name, pageable, authentication);
Expand Down Expand Up @@ -278,7 +278,7 @@ public ResponseEntity<LPVSResult> resultPage(
Authentication authentication) {

lpvsLoginCheckService.loginVerification(authentication);
HttpHeaders headers = LPVSWebhookUtil.generateSecurityHeaders();
HttpHeaders headers = LPVSPayloadUtil.generateSecurityHeaders();

Optional<LPVSPullRequest> prOpt = lpvsPullRequestRepository.findById(prId);
if (!prOpt.isPresent()) {
Expand Down Expand Up @@ -372,7 +372,7 @@ public ResponseEntity<Dashboard> dashboardPage(
@PathVariable("name") String name,
Authentication authentication) {

HttpHeaders headers = LPVSWebhookUtil.generateSecurityHeaders();
HttpHeaders headers = LPVSPayloadUtil.generateSecurityHeaders();
Dashboard dashboardEntity =
lpvsStatisticsService.getDashboardEntity(
type, HtmlUtils.htmlEscape(name), authentication);
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.util.LPVSCommentUtil;
import com.lpvs.util.LPVSFileUtil;
import com.lpvs.util.LPVSWebhookUtil;
import com.lpvs.util.LPVSPayloadUtil;
import lombok.extern.slf4j.Slf4j;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GHRepository;
Expand Down Expand Up @@ -102,14 +102,14 @@ public String getPullRequestFiles(LPVSQueue webhookConfig) {
gitHub = gitHubConnectionService.connectToGitHubApi();
log.debug(
"Repository Info: "
+ LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
+ LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
+ LPVSPayloadUtil.getRepositoryName(webhookConfig));
GHRepository repository =
gitHub.getRepository(
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
+ LPVSPayloadUtil.getRepositoryName(webhookConfig));

GHPullRequest pullRequest = getPullRequest(webhookConfig, repository);
if (pullRequest == null) {
Expand Down Expand Up @@ -168,9 +168,9 @@ public void setPendingCheck(LPVSQueue webhookConfig) {
gitHub = gitHubConnectionService.connectToGitHubApi();
GHRepository repository =
gitHub.getRepository(
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
+ LPVSPayloadUtil.getRepositoryName(webhookConfig));
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.PENDING,
Expand All @@ -192,9 +192,9 @@ public void setErrorCheck(LPVSQueue webhookConfig) {
gitHub = gitHubConnectionService.connectToGitHubApi();
GHRepository repository =
gitHub.getRepository(
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
+ LPVSPayloadUtil.getRepositoryName(webhookConfig));
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.ERROR,
Expand Down Expand Up @@ -223,9 +223,9 @@ public void commentResults(

GHRepository repository =
gitHub.getRepository(
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
+ LPVSPayloadUtil.getRepositoryName(webhookConfig));
GHPullRequest pullRequest = getPullRequest(webhookConfig, repository);

if (pullRequest == null) {
Expand Down Expand Up @@ -370,9 +370,9 @@ public void commentResults(
*/
public String getRepositoryLicense(LPVSQueue webhookConfig) {
try {
String repositoryName = LPVSWebhookUtil.getRepositoryName(webhookConfig);
String repositoryName = LPVSPayloadUtil.getRepositoryName(webhookConfig);
String repositoryOrganization =
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig);
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig);
gitHub = gitHubConnectionService.connectToGitHubApi();
GHRepository repository =
gitHub.getRepository(repositoryOrganization + "/" + repositoryName);
Expand Down Expand Up @@ -412,7 +412,7 @@ public LPVSQueue getInternalQueueByPullRequest(String pullRequest) {
gitHub = gitHubConnectionService.connectToGitHubApi();
GHRepository repo = gitHub.getRepository(pullRequestRepo);
GHPullRequest pR = repo.getPullRequest(pullRequestNum);
return LPVSWebhookUtil.getGitHubWebhookConfig(repo, pR);
return LPVSPayloadUtil.getGitHubWebhookConfig(repo, pR);
} catch (IOException e) {
log.error("Can't set up github client: " + e);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.repository.LPVSQueueRepository;
import com.lpvs.service.scan.LPVSDetectService;
import com.lpvs.util.LPVSWebhookUtil;
import com.lpvs.util.LPVSPayloadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
Expand Down Expand Up @@ -190,9 +190,9 @@ public void processWebHook(LPVSQueue webhookConfig) {
LPVSPullRequest pullRequest =
lpvsPullRequestRepository.findLatestByPullRequestInfo(
webhookConfig.getUserId(),
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig),
+ LPVSPayloadUtil.getRepositoryName(webhookConfig),
webhookConfig.getPullRequestFilesUrl(),
webhookConfig.getPullRequestHead(),
webhookConfig.getPullRequestBase(),
Expand All @@ -203,9 +203,9 @@ public void processWebHook(LPVSQueue webhookConfig) {
pullRequest = new LPVSPullRequest();
pullRequest.setUser(webhookConfig.getUserId());
pullRequest.setRepositoryName(
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
+ LPVSPayloadUtil.getRepositoryName(webhookConfig));
pullRequest.setPullRequestUrl(webhookConfig.getPullRequestUrl());
pullRequest.setPullRequestFilesUrl(webhookConfig.getPullRequestFilesUrl());
pullRequest.setPullRequestHead(webhookConfig.getPullRequestHead());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.lpvs.repository.LPVSLicenseRepository;
import com.lpvs.service.LPVSLicenseService;
import com.lpvs.service.scan.LPVSScanService;
import com.lpvs.util.LPVSWebhookUtil;
import com.lpvs.util.LPVSPayloadUtil;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -69,28 +69,6 @@ public LPVSScanossDetectService(
this.lpvsLicenseRepository = lpvsLicenseRepository;
}

/**
* Creates an InputStreamReader for the given process, handling UTF-8 encoding.
*
* @param process The process whose error stream InputStreamReader is to be created.
* @return An InputStreamReader for the error stream of the process.
* @throws UnsupportedEncodingException If UTF-8 encoding is not supported.
*/
protected InputStreamReader createInputStreamReader(Process process)
throws UnsupportedEncodingException {
return new InputStreamReader(process.getErrorStream(), "UTF-8");
}

/**
* Creates a BufferedReader for the given InputStreamReader.
*
* @param inputStreamReader The InputStreamReader to create a BufferedReader from.
* @return A BufferedReader for the given InputStreamReader.
*/
protected BufferedReader createBufferReader(InputStreamReader inputStreamReader) {
return new BufferedReader(inputStreamReader);
}

/**
* Initiates the Scanoss scan for the specified LPVSQueue and file path.
*
Expand Down Expand Up @@ -133,7 +111,10 @@ public void runScan(LPVSQueue webhookConfig, String path) throws Exception {
log.error("Scanoss scanner terminated with non-zero code. Terminating.");
BufferedReader output = null;
try {
output = createBufferReader(createInputStreamReader(process));
output =
LPVSPayloadUtil.createBufferReader(
LPVSPayloadUtil.createInputStreamReader(
process.getErrorStream()));
log.error(output.readLine());
throw new Exception(
"Scanoss scanner terminated with non-zero code. Terminating.");
Expand Down Expand Up @@ -173,9 +154,9 @@ public List<LPVSFile> checkLicenses(LPVSQueue webhookConfig) {
+ File.separator
+ "Results"
+ File.separator
+ LPVSWebhookUtil.getRepositoryName(webhookConfig)
+ LPVSPayloadUtil.getRepositoryName(webhookConfig)
+ File.separator
+ LPVSWebhookUtil.getPullRequestId(webhookConfig)
+ LPVSPayloadUtil.getPullRequestId(webhookConfig)
+ ".json"));
} else {
reader =
Expand All @@ -185,7 +166,7 @@ public List<LPVSFile> checkLicenses(LPVSQueue webhookConfig) {
+ File.separator
+ "Results"
+ File.separator
+ LPVSWebhookUtil.getRepositoryName(webhookConfig)
+ LPVSPayloadUtil.getRepositoryName(webhookConfig)
+ File.separator
+ webhookConfig.getHeadCommitSHA()
+ ".json"));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/util/LPVSCommentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class LPVSCommentUtil {
public static String getMatchedLinesAsLink(
LPVSQueue webhookConfig, LPVSFile file, LPVSVcs vcs) {
String prefix =
LPVSWebhookUtil.getRepositoryUrl(webhookConfig)
LPVSPayloadUtil.getRepositoryUrl(webhookConfig)
+ "/blob/"
+ webhookConfig.getHeadCommitSHA()
+ "/"
Expand Down
Loading

0 comments on commit 0dd4336

Please sign in to comment.