Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the issue with duplicated records in pull_requests table and runtime error (missing bean) #446

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/lpvs/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand Down Expand Up @@ -139,4 +140,14 @@ public CorsConfigurationSource corsConfigurationSource() {
source.registerCorsConfiguration("/**", configuration);
return source;
}

/**
* Defines a simple ClientRegistrationRepository that always returns null for any registration ID.
*
* @return ClientRegistrationRepository bean.
*/
@Bean
public ClientRegistrationRepository clientRegistrationRepository() {
return registrationId -> null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@ public interface LPVSPullRequestRepository extends JpaRepository<LPVSPullRequest
@Query(value = "SELECT now();", nativeQuery = true)
Date getNow();

/**
* Retrieves the latest LPVSPullRequest entity based on the provided criteria.
*
* @param user The user associated with the pull request. If {@code null}, this parameter is ignored.
* @param repositoryName The name of the repository associated with the pull request. If {@code null}, this parameter is ignored.
* @param pullRequestFilesUrl The URL of the pull request files. If {@code null}, this parameter is ignored.
* @param pullRequestHead The head of the pull request. If {@code null}, this parameter is ignored.
* @param pullRequestBase The base of the pull request. If {@code null}, this parameter is ignored.
* @param sender The sender of the pull request. If {@code null}, this parameter is ignored.
* @param status The status of the pull request. If {@code null}, this parameter is ignored.
* @return The latest LPVSPullRequest entity matching the provided criteria,
* or {@code null} if no matching entity is found.
*/
@Query(
value =
"SELECT * FROM pull_requests pr "
+ "WHERE (:user IS NULL OR pr.user = :user) "
+ "AND (:repositoryName IS NULL OR pr.repository_name = :repositoryName) "
+ "AND (:pullRequestFilesUrl IS NULL OR pr.diff_url = :pullRequestFilesUrl) "
+ "AND (:pullRequestHead IS NULL OR pr.pull_request_head = :pullRequestHead) "
+ "AND (:pullRequestBase IS NULL OR pr.pull_request_base = :pullRequestBase) "
+ "AND (:sender IS NULL OR pr.sender = :sender) "
+ "AND (:status IS NULL OR pr.status = :status) "
+ "ORDER BY pr.scan_date DESC LIMIT 1",
nativeQuery = true)
LPVSPullRequest findLatestByPullRequestInfo(
@Param("user") String user,
@Param("repositoryName") String repositoryName,
@Param("pullRequestFilesUrl") String pullRequestFilesUrl,
@Param("pullRequestHead") String pullRequestHead,
@Param("pullRequestBase") String pullRequestBase,
@Param("sender") String sender,
@Param("status") String status);

/**
* Find all pull requests with the specified base name, paginated.
*
Expand Down
51 changes: 34 additions & 17 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,34 +179,47 @@ public LPVSQueue getLatestScan(List<LPVSQueue> webhookConfigList) {
*/
@Async("threadPoolTaskExecutor")
public void processWebHook(LPVSQueue webhookConfig) {
LPVSPullRequest pullRequest = new LPVSPullRequest();
try {
log.info(
"Processing webhook ID: "
+ webhookConfig.getId()
+ ", attempt: "
+ webhookConfig.getAttempts()
+ 1
+ " for PR: "
+ webhookConfig.getPullRequestUrl());

String filePath = gitHubService.getPullRequestFiles(webhookConfig);
Long id = webhookConfig.getId();
log.info(
"Processing webhook ID: "
+ id
+ ", attempt: "
+ (webhookConfig.getAttempts() + 1)
+ " for PR: "
+ webhookConfig.getPullRequestUrl());
LPVSPullRequest pullRequest =
lpvsPullRequestRepository.findLatestByPullRequestInfo(
webhookConfig.getUserId(),
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig),
webhookConfig.getPullRequestFilesUrl(),
webhookConfig.getPullRequestHead(),
webhookConfig.getPullRequestBase(),
webhookConfig.getSender(),
LPVSPullRequestStatus.INTERNAL_ERROR.getPullRequestStatus());

pullRequest.setPullRequestUrl(webhookConfig.getPullRequestUrl());
if (pullRequest == null) {
pullRequest = new LPVSPullRequest();
pullRequest.setUser(webhookConfig.getUserId());
pullRequest.setPullRequestFilesUrl(webhookConfig.getPullRequestFilesUrl());
pullRequest.setRepositoryName(
LPVSWebhookUtil.getRepositoryOrganization(webhookConfig)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhookConfig));
pullRequest.setDate(webhookConfig.getDate());
pullRequest.setStatus(LPVSPullRequestStatus.SCANNING.toString());
pullRequest.setPullRequestUrl(webhookConfig.getPullRequestUrl());
pullRequest.setPullRequestFilesUrl(webhookConfig.getPullRequestFilesUrl());
pullRequest.setPullRequestHead(webhookConfig.getPullRequestHead());
pullRequest.setPullRequestBase(webhookConfig.getPullRequestBase());
pullRequest.setSender(webhookConfig.getSender());
}

try {

pullRequest.setDate(webhookConfig.getDate());
pullRequest.setStatus(LPVSPullRequestStatus.SCANNING.toString());
pullRequest = lpvsPullRequestRepository.saveAndFlush(pullRequest);
log.debug("ID: " + pullRequest.getId() + " " + pullRequest.toString());

String filePath = gitHubService.getPullRequestFiles(webhookConfig);
if (filePath != null && Files.list(Paths.get(filePath)).count() != 0) {
log.debug("Successfully downloaded files");

Expand Down Expand Up @@ -266,6 +279,10 @@ public void processWebHook(LPVSQueue webhookConfig) {
log.warn("Failed to post FAIL result " + e.getMessage());
}
delete(webhookConfig);
log.info(
"Webhook ID: "
+ id
+ " - removed from the queue because the number of attempts exceeded the max value");
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/database_dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ CREATE TABLE IF NOT EXISTS pull_requests (
url longtext NOT NULL,
diff_url longtext,
status varchar(255) DEFAULT NULL,
pull_request_head varchar(255) NOT NULL,
pull_request_base varchar(255) NOT NULL,
sender varchar(255) NOT NULL,
pull_request_head varchar(255) DEFAULT NULL,
pull_request_base varchar(255) DEFAULT NULL,
sender varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);

Expand Down Expand Up @@ -78,6 +78,9 @@ CREATE TABLE IF NOT EXISTS queue (
pull_request_diff_url longtext,
status_callback_url longtext,
commit_sha varchar(255) DEFAULT NULL,
pull_request_base varchar(255) DEFAULT NULL,
pull_request_head varchar(255) DEFAULT NULL,
sender varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);

Expand Down
Loading