From bc7542dd76279cd37b3a1069a97b677c26d4ce97 Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Wed, 28 Feb 2024 22:10:41 +0200 Subject: [PATCH 1/2] fix: Fix the issue with duplicated records in pull_requests table Signed-off-by: Oleg Kopysov --- .../repository/LPVSPullRequestRepository.java | 34 +++++++++++++ .../com/lpvs/service/LPVSQueueService.java | 51 ++++++++++++------- src/main/resources/database_dump.sql | 9 ++-- 3 files changed, 74 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/lpvs/repository/LPVSPullRequestRepository.java b/src/main/java/com/lpvs/repository/LPVSPullRequestRepository.java index 86063854..0b9b17e4 100644 --- a/src/main/java/com/lpvs/repository/LPVSPullRequestRepository.java +++ b/src/main/java/com/lpvs/repository/LPVSPullRequestRepository.java @@ -29,6 +29,40 @@ public interface LPVSPullRequestRepository extends JpaRepository 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"); @@ -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"); } } } diff --git a/src/main/resources/database_dump.sql b/src/main/resources/database_dump.sql index 126f7af5..b154c89c 100644 --- a/src/main/resources/database_dump.sql +++ b/src/main/resources/database_dump.sql @@ -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) ); @@ -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) ); From 95d19a7788624354bd6e8e3ac3a55dc9d5a5ec5d Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Thu, 29 Feb 2024 08:36:14 +0200 Subject: [PATCH 2/2] fix: Fix runtime error with missing ClientRegistrationRepository bean Signed-off-by: Oleg Kopysov --- src/main/java/com/lpvs/config/SecurityConfig.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/com/lpvs/config/SecurityConfig.java b/src/main/java/com/lpvs/config/SecurityConfig.java index 9b9b8abd..060189d5 100644 --- a/src/main/java/com/lpvs/config/SecurityConfig.java +++ b/src/main/java/com/lpvs/config/SecurityConfig.java @@ -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; @@ -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; + } }