Skip to content

Commit

Permalink
fix: Fix the issue with duplicated records in pull_requests table
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed Feb 28, 2024
1 parent d0e8ba2 commit bc7542d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
34 changes: 34 additions & 0 deletions src/main/java/com/lpvs/repository/LPVSPullRequestRepository.java
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

0 comments on commit bc7542d

Please sign in to comment.