Skip to content

Commit

Permalink
fix: Remove unnecessary try/catch section from the service (#447)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>
  • Loading branch information
o-kopysov committed Mar 1, 2024
1 parent d04c45a commit 843f81f
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long>
*
* @return List of SPDX identifiers as Strings.
*/
@Query(value = "select license_list.spdxId from LPVSLicense license_list")
@Query(value = "select licenses.spdxId from LPVSLicense licenses")
List<String> takeAllSpdxId();
}
280 changes: 127 additions & 153 deletions src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,178 +213,152 @@ public void setErrorCheck(LPVSQueue webhookConfig) {
* @param scanResults List of detected files and licenses.
* @param conflicts List of license conflicts.
* @param lpvsPullRequest LPVS entity representing the pull request.
* @throws IOException if an error occurs during GitHub interaction.
*/
public void commentResults(
LPVSQueue webhookConfig,
List<LPVSFile> scanResults,
List<LPVSLicenseService.Conflict<String, String>> conflicts,
LPVSPullRequest lpvsPullRequest)
throws IOException {
throws Exception {

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

if (pullRequest == null) {
log.error("Can't find pull request " + webhookConfig.getPullRequestUrl());
lpvsPullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString());
pullRequestRepository.saveAndFlush(lpvsPullRequest);
return;
}
if (scanResults == null && conflicts == null) {
log.error(
"Files are not found in pull request " + webhookConfig.getPullRequestUrl());
lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString());
pullRequestRepository.saveAndFlush(lpvsPullRequest);
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.SUCCESS,
null,
"Files are not found",
"[License Pre-Validation Service]");
return;
}
if (pullRequest == null) {
log.error("Can't find pull request " + webhookConfig.getPullRequestUrl());
lpvsPullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString());
pullRequestRepository.saveAndFlush(lpvsPullRequest);
return;
}
if (scanResults == null && conflicts == null) {
log.error("Files are not found in pull request " + webhookConfig.getPullRequestUrl());
lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString());
pullRequestRepository.saveAndFlush(lpvsPullRequest);
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.SUCCESS,
null,
"Files are not found",
"[License Pre-Validation Service]");
return;
}

boolean hasProhibitedOrRestricted = false;
boolean hasConflicts = false;
String commitComment = "";
boolean hasProhibitedOrRestricted = false;
boolean hasConflicts = false;
String commitComment = "";

if (scanResults != null && scanResults.size() != 0) {
commitComment = "**Detected licenses:**\n\n\n";
for (LPVSFile file : scanResults) {
commitComment += "**File:** " + file.getFilePath() + "\n";
commitComment +=
"**License(s):** "
+ file.convertLicensesToString(LPVSVcs.GITHUB)
+ "\n";
commitComment +=
"**Component:** "
+ file.getComponentName()
+ " ("
+ file.getComponentFilePath()
+ ")\n";
commitComment +=
"**Matched Lines:** "
+ LPVSCommentUtil.getMatchedLinesAsLink(
webhookConfig, file, LPVSVcs.GITHUB)
+ "\n";
commitComment += "**Snippet Match:** " + file.getSnippetMatch() + "\n\n\n\n";
for (LPVSLicense license : file.getLicenses()) {
LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense();
detectedIssue.setPullRequest(lpvsPullRequest);
detectedIssue.setLicense(license);
detectedIssue.setFilePath(file.getFilePath());
detectedIssue.setType(file.getSnippetType());
detectedIssue.setMatch(file.getSnippetMatch());
detectedIssue.setLines(file.getMatchedLines());
detectedIssue.setComponentFilePath(file.getComponentFilePath());
detectedIssue.setComponentFileUrl(file.getComponentFileUrl());
detectedIssue.setComponentName(file.getComponentName());
detectedIssue.setComponentLines(file.getComponentLines());
detectedIssue.setComponentUrl(file.getComponentUrl());
detectedIssue.setComponentVersion(file.getComponentVersion());
detectedIssue.setComponentVendor(file.getComponentVendor());
if (license.getAccess().isEmpty()
|| license.getAccess().equalsIgnoreCase("prohibited")
|| license.getAccess().equalsIgnoreCase("restricted")
|| license.getAccess().equalsIgnoreCase("unreviewed")) {
hasProhibitedOrRestricted = true;
detectedIssue.setIssue(true);
} else {
detectedIssue.setIssue(false);
}
lpvsDetectedLicenseRepository.saveAndFlush(detectedIssue);
}
}
}

if (conflicts != null && conflicts.size() > 0) {
hasConflicts = true;
StringBuilder commitCommentBuilder = new StringBuilder();
commitCommentBuilder.append("**Detected license conflicts:**\n\n\n");
commitCommentBuilder.append("<ul>");
for (LPVSLicenseService.Conflict<String, String> conflict : conflicts) {
commitCommentBuilder.append(
"<li>" + conflict.l1 + " and " + conflict.l2 + "</li>");
if (scanResults != null && scanResults.size() != 0) {
commitComment = "**Detected licenses:**\n\n\n";
for (LPVSFile file : scanResults) {
commitComment += "**File:** " + file.getFilePath() + "\n";
commitComment +=
"**License(s):** " + file.convertLicensesToString(LPVSVcs.GITHUB) + "\n";
commitComment +=
"**Component:** "
+ file.getComponentName()
+ " ("
+ file.getComponentFilePath()
+ ")\n";
commitComment +=
"**Matched Lines:** "
+ LPVSCommentUtil.getMatchedLinesAsLink(
webhookConfig, file, LPVSVcs.GITHUB)
+ "\n";
commitComment += "**Snippet Match:** " + file.getSnippetMatch() + "\n\n\n\n";
for (LPVSLicense license : file.getLicenses()) {
LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense();
detectedIssue.setPullRequest(lpvsPullRequest);
Long l1 = lpvsLicenseRepository.searchBySpdxId(conflict.l1).getLicenseId();
Long l2 = lpvsLicenseRepository.searchBySpdxId(conflict.l2).getLicenseId();
detectedIssue.setLicenseConflict(
lpvsLicenseConflictRepository.findLicenseConflict(l1, l2));
if (webhookConfig.getRepositoryLicense() != null) {
LPVSLicense repoLicense =
lpvsLicenseRepository.searchBySpdxId(
webhookConfig.getRepositoryLicense());
if (repoLicense == null) {
repoLicense =
lpvsLicenseRepository.searchByAlternativeLicenseNames(
webhookConfig.getRepositoryLicense());
}
detectedIssue.setRepositoryLicense(repoLicense);
detectedIssue.setLicense(license);
detectedIssue.setFilePath(file.getFilePath());
detectedIssue.setType(file.getSnippetType());
detectedIssue.setMatch(file.getSnippetMatch());
detectedIssue.setLines(file.getMatchedLines());
detectedIssue.setComponentFilePath(file.getComponentFilePath());
detectedIssue.setComponentFileUrl(file.getComponentFileUrl());
detectedIssue.setComponentName(file.getComponentName());
detectedIssue.setComponentLines(file.getComponentLines());
detectedIssue.setComponentUrl(file.getComponentUrl());
detectedIssue.setComponentVersion(file.getComponentVersion());
detectedIssue.setComponentVendor(file.getComponentVendor());
if (license.getAccess().isEmpty()
|| license.getAccess().equalsIgnoreCase("prohibited")
|| license.getAccess().equalsIgnoreCase("restricted")
|| license.getAccess().equalsIgnoreCase("unreviewed")) {
hasProhibitedOrRestricted = true;
detectedIssue.setIssue(true);
} else {
detectedIssue.setIssue(false);
}
detectedIssue.setIssue(true);
lpvsDetectedLicenseRepository.saveAndFlush(detectedIssue);
}
commitCommentBuilder.append("</ul>");
if (null != webhookConfig.getHubLink()) {
commitCommentBuilder.append(
"\n\n###### <p align='right'>Check the validation details at the link(");
commitCommentBuilder.append(webhookConfig.getHubLink());
commitCommentBuilder.append(")</p>");
}
commitComment += commitCommentBuilder.toString();
}
}

if (hasProhibitedOrRestricted || hasConflicts) {
lpvsPullRequest.setStatus(LPVSPullRequestStatus.ISSUES_DETECTED.toString());
pullRequestRepository.save(lpvsPullRequest);
pullRequest.comment(
"**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n"
+ commitComment);
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.FAILURE,
null,
"Potential license problem(s) detected",
"[License Pre-Validation Service]");
} else {
lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString());
pullRequestRepository.save(lpvsPullRequest);
pullRequest.comment(
"**\\[License Pre-Validation Service\\]** No license issue detected \n\n"
+ commitComment);
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.SUCCESS,
null,
"No license issue detected",
"[License Pre-Validation Service]");
if (conflicts != null && conflicts.size() > 0) {
hasConflicts = true;
StringBuilder commitCommentBuilder = new StringBuilder();
commitCommentBuilder.append("**Detected license conflicts:**\n\n\n");
commitCommentBuilder.append("<ul>");
for (LPVSLicenseService.Conflict<String, String> conflict : conflicts) {
commitCommentBuilder.append("<li>" + conflict.l1 + " and " + conflict.l2 + "</li>");
LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense();
detectedIssue.setPullRequest(lpvsPullRequest);
Long l1 = lpvsLicenseRepository.searchBySpdxId(conflict.l1).getLicenseId();
Long l2 = lpvsLicenseRepository.searchBySpdxId(conflict.l2).getLicenseId();
detectedIssue.setLicenseConflict(
lpvsLicenseConflictRepository.findLicenseConflict(l1, l2));
if (webhookConfig.getRepositoryLicense() != null) {
LPVSLicense repoLicense =
lpvsLicenseRepository.searchBySpdxId(
webhookConfig.getRepositoryLicense());
if (repoLicense == null) {
repoLicense =
lpvsLicenseRepository.searchByAlternativeLicenseNames(
webhookConfig.getRepositoryLicense());
}
detectedIssue.setRepositoryLicense(repoLicense);
}
detectedIssue.setIssue(true);
lpvsDetectedLicenseRepository.saveAndFlush(detectedIssue);
}
} catch (IOException | NullPointerException | IllegalArgumentException e) {
lpvsPullRequest.setStatus(LPVSPullRequestStatus.INTERNAL_ERROR.toString());
pullRequestRepository.saveAndFlush(lpvsPullRequest);
log.error("Can't authorize commentResults(): " + e.getMessage());
try {
GHRepository repository =
gitHub.getRepository(
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.ERROR,
null,
"Internal scan error occurs",
"[License Pre-Validation Service]");
} catch (Exception ex) {
log.error("Can't authorize " + ex);
commitCommentBuilder.append("</ul>");
if (null != webhookConfig.getHubLink()) {
commitCommentBuilder.append(
"\n\n###### <p align='right'>Check the validation details at the link(");
commitCommentBuilder.append(webhookConfig.getHubLink());
commitCommentBuilder.append(")</p>");
}
commitComment += commitCommentBuilder.toString();
}

if (hasProhibitedOrRestricted || hasConflicts) {
lpvsPullRequest.setStatus(LPVSPullRequestStatus.ISSUES_DETECTED.toString());
pullRequestRepository.save(lpvsPullRequest);
pullRequest.comment(
"**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n"
+ commitComment);
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.FAILURE,
null,
"Potential license problem(s) detected",
"[License Pre-Validation Service]");
} else {
lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString());
pullRequestRepository.save(lpvsPullRequest);
pullRequest.comment(
"**\\[License Pre-Validation Service\\]** No license issue detected \n\n"
+ commitComment);
repository.createCommitStatus(
webhookConfig.getHeadCommitSHA(),
GHCommitState.SUCCESS,
null,
"No license issue detected",
"[License Pre-Validation Service]");
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
Expand Down Expand Up @@ -275,8 +274,8 @@ public void processWebHook(LPVSQueue webhookConfig) {
+ pullRequest.getPullRequestUrl());
try {
gitHubService.commentResults(webhookConfig, null, null, pullRequest);
} catch (IOException e1) {
log.warn("Failed to post FAIL result " + e.getMessage());
} catch (Exception ex) {
log.warn("Failed to post FAIL result " + ex.getMessage());
}
delete(webhookConfig);
log.info(
Expand Down

0 comments on commit 843f81f

Please sign in to comment.