Skip to content

Commit

Permalink
Sync with internal repo (#190)
Browse files Browse the repository at this point in the history
* fix: sync main classes with latest changes

* fix: sync test classes with latest changes

* fix: modify after review

* fix: adjust report message format
fix: fix after review comment (remove unused enums)

* test: adjust tests for updated comment format
  • Loading branch information
m-rudyk authored Aug 17, 2023
1 parent 766c005 commit 253fa48
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 271 deletions.
22 changes: 20 additions & 2 deletions src/main/java/com/lpvs/LicensePreValidationSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;

import com.lpvs.util.LPVSExitHandler;


@SpringBootApplication(scanBasePackages = { "com.lpvs" })
Expand All @@ -24,13 +28,20 @@ public class LicensePreValidationSystem {

private final int corePoolSize;

private static LPVSExitHandler exitHandler;

public LicensePreValidationSystem(@Value("${lpvs.cores:8}") int corePoolSize) {
this.corePoolSize = corePoolSize;
}

public static void main(String[] args) {
SpringApplication app = new SpringApplication(LicensePreValidationSystem.class);
app.run(args);
try {
ApplicationContext applicationContext = SpringApplication.run(LicensePreValidationSystem.class, args);
exitHandler = applicationContext.getBean(LPVSExitHandler.class);
} catch (IllegalArgumentException e) {
System.err.println("An IllegalArgumentException occurred: " + e.getMessage());
System.exit(1);
}
}

@Bean("threadPoolTaskExecutor")
Expand All @@ -41,5 +52,12 @@ public TaskExecutor getAsyncExecutor(){
return executor;
}

@GetMapping("/exit")
public static void exit(int exitCode) {
exitHandler.exit(exitCode);
if (exitCode != 0) {
System.exit(exitCode);
}
}
}

20 changes: 10 additions & 10 deletions src/main/java/com/lpvs/controller/GitHubWebhooksController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
import com.lpvs.repository.LPVSQueueRepository;
import com.lpvs.service.LPVSGitHubService;
import com.lpvs.service.LPVSQueueService;
import com.lpvs.util.LPVSExitHandler;
import com.lpvs.util.LPVSWebhookUtil;
import com.lpvs.entity.LPVSResponseWrapper;

import lombok.extern.slf4j.Slf4j;

import org.apache.catalina.core.ApplicationContext;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
Expand All @@ -36,8 +34,7 @@
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

@RestController
@Slf4j
@RestController @Slf4j
public class GitHubWebhooksController {

private String GITHUB_SECRET;
Expand All @@ -55,7 +52,7 @@ public void setProps() {
.orElse(Optional.ofNullable(System.getenv("LPVS_GITHUB_SECRET")).orElse(""));
if (this.GITHUB_SECRET.isEmpty()) {
log.error("LPVS_GITHUB_SECRET(github.secret) is not set.");
System.exit(SpringApplication.exit(applicationContext, () -> -1));
exitHandler.exit(-1);
}
}

Expand All @@ -67,16 +64,19 @@ public void setProps() {

private LPVSGitHubService gitHubService;

private LPVSExitHandler exitHandler;

private static final String SIGNATURE = "X-Hub-Signature-256";
private static final String SUCCESS = "Success";
private static final String ERROR = "Error";
private static final String ALGORITHM = "HmacSHA256";

public GitHubWebhooksController(LPVSQueueService queueService, LPVSGitHubService gitHubService, LPVSQueueRepository queueRepository, @Value("${github.secret:}") String GITHUB_SECRET) {
public GitHubWebhooksController(LPVSQueueService queueService, LPVSGitHubService gitHubService, LPVSQueueRepository queueRepository, @Value("${github.secret:}") String GITHUB_SECRET, LPVSExitHandler exitHandler) {
this.queueService = queueService;
this.gitHubService = gitHubService;
this.queueRepository = queueRepository;
this.GITHUB_SECRET = GITHUB_SECRET;
this.exitHandler = exitHandler;
}

/**
Expand All @@ -89,7 +89,7 @@ public GitHubWebhooksController(LPVSQueueService queueService, LPVSGitHubService
*/
@RequestMapping(value = "/webhooks", method = RequestMethod.POST)
public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(@RequestHeader(SIGNATURE) String signature, @RequestBody String payload) throws Exception {
log.debug("New webhook request received");
log.debug("New GitHub webhook request received");

// if signature is empty return 401
if (!StringUtils.hasText(signature)) {
Expand Down
27 changes: 7 additions & 20 deletions src/main/java/com/lpvs/entity/LPVSDiffFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,14 @@
import java.util.LinkedList;
import java.util.List;

@Getter
@Setter
@Getter @Setter
public class LPVSDiffFile {
private String originalFile;
private String newFile;
private List<String> addedLines;
private List<String> deletedLines;
private List<String> unchangedLines;
private String oldFileName;
private String newFileName;
private List<String> changedLines;

public void appendAddedLine(String line){
if (this.addedLines == null) this.addedLines = new LinkedList<>();
this.addedLines.add(line);
}

public void appendDeletedLine(String line){
if (this.deletedLines == null) this.deletedLines = new LinkedList<>();
this.deletedLines.add(line);
}

public void appendUnchangedLine(String line){
if (this.unchangedLines == null) this.unchangedLines = new LinkedList<>();
this.unchangedLines.add(line);
public void appendPatchedLine(String line){
if (this.changedLines == null) this.changedLines = new LinkedList<>();
this.changedLines.add(line);
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/lpvs/entity/LPVSFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ public class LPVSFile {
public String convertLicensesToString(LPVSVcs vcs) {
String licenseNames = "";
for (LPVSLicense license : this.licenses) {
String licSpdxId = license.getSpdxId();
// Check if the license SPDX ID has scanner-specific name
if (licSpdxId.startsWith("LicenseRef")) {
// Change the name of the license that will be displayed in PR comment to scanner-independent
licSpdxId = "UNREVIEWED LICENSE : " + licSpdxId.replaceAll("LicenseRef-scancode-", "").replaceAll("LicenseRef-scanoss-", "");
}
if (vcs != null && vcs.equals(LPVSVcs.GITHUB)) {
licenseNames += (license.getChecklistUrl() != null ? "<a target=\"_blank\" href=\"" + license.getChecklistUrl() + "\">" : "") +
license.getSpdxId() +
licSpdxId +
(license.getChecklistUrl() != null ? "</a>" : "") +
" (" + license.getAccess().toLowerCase() + "), ";
} else {
licenseNames += license.getSpdxId() + (license.getChecklistUrl() != null ? " (" + license.getChecklistUrl() + ")" : "") +
licenseNames += licSpdxId + (license.getChecklistUrl() != null ? " (" + license.getChecklistUrl() + ")" : "") +
" - " + license.getAccess().toLowerCase() + ", ";
}
}
Expand Down
48 changes: 27 additions & 21 deletions src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.lpvs.repository.LPVSLicenseRepository;
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.util.LPVSCommentUtil;
import com.lpvs.util.LPVSExitHandler;
import com.lpvs.util.LPVSFileUtil;
import com.lpvs.util.LPVSWebhookUtil;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -27,8 +28,6 @@
import org.kohsuke.github.GHCommitState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

Expand All @@ -53,9 +52,6 @@ public class LPVSGitHubService {
private final static String GITHUB_AUTH_TOKEN_ENV_VAR_NAME = "LPVS_GITHUB_TOKEN";
private final static String GITHUB_API_URL_ENV_VAR_NAME = "LPVS_GITHUB_API_URL";

@Autowired
ApplicationContext applicationContext;

@Autowired
private LPVSPullRequestRepository pullRequestRepository;

Expand All @@ -68,20 +64,23 @@ public class LPVSGitHubService {
@Autowired
private LPVSLicenseConflictRepository lpvsLicenseConflictRepository;

@Autowired
private LPVSExitHandler exitHandler;

public LPVSGitHubService(@Value("${" + GITHUB_LOGIN_PROP_NAME + "}") String GITHUB_LOGIN,
@Value("${" + GITHUB_AUTH_TOKEN_PROP_NAME + "}") String GITHUB_AUTH_TOKEN,
@Value("${" + GITHUB_API_URL_PROP_NAME + "}") String GITHUB_API_URL,
LPVSPullRequestRepository pullRequestRepository,
LPVSDetectedLicenseRepository lpvsDetectedLicenseRepository,
LPVSLicenseRepository lpvsLicenseRepository,
LPVSLicenseConflictRepository lpvsLicenseConflictRepository) {
LPVSLicenseConflictRepository lpvsLicenseConflictRepository,
LPVSExitHandler exitHandler) {
this.GITHUB_LOGIN = Optional.ofNullable(GITHUB_LOGIN).filter(s -> !s.isEmpty())
.orElse(Optional.ofNullable(System.getenv(GITHUB_LOGIN_ENV_VAR_NAME)).orElse(""));
this.GITHUB_AUTH_TOKEN = Optional.ofNullable(GITHUB_AUTH_TOKEN).filter(s -> !s.isEmpty())
.orElse(Optional.ofNullable(System.getenv(GITHUB_AUTH_TOKEN_ENV_VAR_NAME)).orElse(""));
this.GITHUB_API_URL = Optional.ofNullable(GITHUB_API_URL).filter(s -> !s.isEmpty())
.orElse(Optional.ofNullable(System.getenv(GITHUB_API_URL_ENV_VAR_NAME)).orElse(""));
this.exitHandler = exitHandler;
this.pullRequestRepository = pullRequestRepository;
this.lpvsDetectedLicenseRepository = lpvsDetectedLicenseRepository;
this.lpvsLicenseRepository = lpvsLicenseRepository;
Expand All @@ -93,7 +92,7 @@ public LPVSGitHubService(@Value("${" + GITHUB_LOGIN_PROP_NAME + "}") String GITH
private void checks() throws Exception {
if (this.GITHUB_AUTH_TOKEN.isEmpty()) {
log.error(GITHUB_AUTH_TOKEN_ENV_VAR_NAME + "(" + GITHUB_AUTH_TOKEN_PROP_NAME + ") is not set.");
System.exit(SpringApplication.exit(applicationContext, () -> -1));
exitHandler.exit(-1);
}
}

Expand All @@ -113,8 +112,7 @@ public String getPullRequestFiles (LPVSQueue webhookConfig) {
return null;
}
log.debug("Saving files...");
return LPVSFileUtil.saveFiles(pullRequest.listFiles(), LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)+"/"+ LPVSWebhookUtil.getRepositoryName(webhookConfig),
webhookConfig.getHeadCommitSHA(), pullRequest.getDeletions());
return LPVSFileUtil.saveGithubDiffs(pullRequest.listFiles(), webhookConfig);
} catch (IOException e){
log.error("Can't authorize getPullRequestFiles() " + e);
}
Expand All @@ -125,10 +123,14 @@ private GHPullRequest getPullRequest(LPVSQueue webhookConfig, GHRepository repos
try {
List<GHPullRequest> pullRequests = repository.getPullRequests(GHIssueState.OPEN);
for (GHPullRequest pullRequest : pullRequests) {
log.debug("Pull request check: " + pullRequest.getUrl().toString() + " / " + webhookConfig.getPullRequestAPIUrl());
if (pullRequest.getUrl().toString().equals(webhookConfig.getPullRequestAPIUrl())){
log.debug("Return pull request " + pullRequest.getDiffUrl());
return pullRequest;
if (null != pullRequest.getUrl()) {
log.debug("Pull request check: " + pullRequest.getUrl().toString() + " / " + webhookConfig.getPullRequestAPIUrl());
if (pullRequest.getUrl().toString().equals(webhookConfig.getPullRequestAPIUrl())){
log.debug("Return pull request " + pullRequest.getDiffUrl());
return pullRequest;
}
} else {
log.warn("Failed to get pull request URL");
}
}
} catch (IOException e){
Expand All @@ -144,7 +146,7 @@ public void setPendingCheck(LPVSQueue webhookConfig) {
else gitHub = GitHub.connectToEnterpriseWithOAuth(GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
GHRepository repository = gitHub.getRepository(LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig));
repository.createCommitStatus(webhookConfig.getHeadCommitSHA(), GHCommitState.PENDING, null,
"Scanning opensource licenses", "[License Pre-Validation Service]");
"Scanning opensource licenses", "[License Pre-Validation Service]");
} catch (IOException e) {
log.error("Can't authorize setPendingCheck()" + e);
}
Expand Down Expand Up @@ -229,10 +231,11 @@ public void commentResults(LPVSQueue webhookConfig, List<LPVSFile> scanResults,

if (conflicts != null && conflicts.size() > 0) {
hasConflicts = true;
commitComment += "**Detected license conflicts:**\n\n\n";
commitComment += "<ul>";
StringBuilder commitCommentBuilder = new StringBuilder();
commitCommentBuilder.append("**Detected license conflicts:**\n\n\n");
commitCommentBuilder.append("<ul>");
for (LPVSLicenseService.Conflict<String, String> conflict : conflicts) {
commitComment += "<li>" + conflict.l1 + " and " + conflict.l2 + "</li>";
commitCommentBuilder.append("<li>" + conflict.l1 + " and " + conflict.l2 + "</li>");
LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense();
detectedIssue.setPullRequest(lpvsPullRequest);
Long l1 = lpvsLicenseRepository.searchBySpdxId(conflict.l1).getLicenseId();
Expand All @@ -248,19 +251,22 @@ public void commentResults(LPVSQueue webhookConfig, List<LPVSFile> scanResults,
detectedIssue.setIssue(true);
lpvsDetectedLicenseRepository.saveAndFlush(detectedIssue);
}
commitComment += "</ul>";
commitCommentBuilder.append("</ul>");
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);
pullRequest.comment("**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n" +
commitComment + "(" + webhookConfig.getHubLink() + ")</p>");
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);
pullRequest.comment("**\\[License Pre-Validation Service\\]** No license issue detected \n\n" +
commitComment + "(" + webhookConfig.getHubLink() + ")</p>");
repository.createCommitStatus(webhookConfig.getHeadCommitSHA(), GHCommitState.SUCCESS, null,
"No license issue detected", "[License Pre-Validation Service]");
}
Expand Down
Loading

0 comments on commit 253fa48

Please sign in to comment.