Skip to content

Commit

Permalink
fix: add changes after review comment, remove unnecessary check
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Rudyk <[email protected]>
  • Loading branch information
m-rudyk committed Feb 15, 2024
1 parent 04b4d6b commit bf7fd83
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 53 deletions.
47 changes: 9 additions & 38 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,40 +146,7 @@ public BlockingDeque<LPVSQueue> getQueue() {
public void checkForQueue() throws InterruptedException {
log.debug("Checking for previous queue");
List<LPVSQueue> webhookConfigList = queueRepository.getQueueList();
if (webhookConfigList.size() > 0) {
LPVSQueue webhookConfig = getLatestScan(webhookConfigList);
webhookConfig.setAttempts(webhookConfig.getAttempts() + 1);
queueRepository.save(webhookConfig);
}
for (LPVSQueue webhook : webhookConfigList) {
log.info("PROCESSING WebHook id = " + webhook.getId());
if (webhook.getAttempts() > maxAttempts) {
LPVSPullRequest pullRequest = new LPVSPullRequest();
pullRequest.setPullRequestUrl(webhook.getPullRequestUrl());
pullRequest.setUser(webhook.getUserId());
pullRequest.setPullRequestFilesUrl(webhook.getPullRequestFilesUrl());
pullRequest.setRepositoryName(
LPVSWebhookUtil.getRepositoryOrganization(webhook)
+ "/"
+ LPVSWebhookUtil.getRepositoryName(webhook));
pullRequest.setDate(webhook.getDate());
pullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString());
pullRequest.setPullRequestHead(webhook.getPullRequestHead());
pullRequest.setPullRequestBase(webhook.getPullRequestBase());
pullRequest.setSender(webhook.getSender());
pullRequest = lpvsPullRequestRepository.saveAndFlush(pullRequest);

if (webhook.getUserId().equals("GitHub hook")) {
gitHubService.setErrorCheck(webhook);
}
queueRepository.deleteById(webhook.getId());
log.info(
"Webhook id = "
+ webhook.getId()
+ " is deleted. Details on PR #"
+ pullRequest.getId());
continue;
}
log.info("Add WebHook id = " + webhook.getId() + " to the queue.");
QUEUE.putFirst(webhook);
}
Expand Down Expand Up @@ -210,8 +177,13 @@ public LPVSQueue getLatestScan(List<LPVSQueue> webhookConfigList) {
public void processWebHook(LPVSQueue webhookConfig) {
LPVSPullRequest pullRequest = new LPVSPullRequest();
try {
log.info("GitHub queue processing...");
log.debug(webhookConfig.toString());
log.info(
"Processing webhook ID: "
+ webhookConfig.getId()
+ ", attempt: "
+ webhookConfig.getAttempts()
+ " for PR: "
+ webhookConfig.getPullRequestUrl());

String filePath = gitHubService.getPullRequestFiles(webhookConfig);

Expand Down Expand Up @@ -258,22 +230,21 @@ public void processWebHook(LPVSQueue webhookConfig) {
log.debug("Creating comment");
gitHubService.commentResults(webhookConfig, files, detectedConflicts, pullRequest);
log.debug("Results posted on GitHub");
delete(webhookConfig);
} else {
log.warn("Files are not found. Probably pull request is not exists.");
gitHubService.commentResults(webhookConfig, null, null, pullRequest);
delete(webhookConfig);
throw new Exception(
"Files are not found. Probably pull request does not exist. Terminating.");
}
delete(webhookConfig);
} catch (Exception | Error e) {
pullRequest.setStatus(LPVSPullRequestStatus.INTERNAL_ERROR.toString());
pullRequest = lpvsPullRequestRepository.saveAndFlush(pullRequest);
log.error("Can't authorize commentResults() " + e);
e.printStackTrace();
int currentAttempts = webhookConfig.getAttempts();
if (currentAttempts < maxAttempts) {
webhookConfig.setAttempts(currentAttempts++);
webhookConfig.setAttempts(currentAttempts + 1);
try {
addFirst(webhookConfig);
} catch (InterruptedException e1) {
Expand Down
15 changes: 0 additions & 15 deletions src/test/java/com/lpvs/service/LPVSQueueServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -841,26 +841,11 @@ void setUp() {

@Test
public void testCheckForQueue() {
LPVSQueue webhookConfig = new LPVSQueue();
webhookConfig.setAttempts(0);
webhookConfig.setDate(new Date());
when(mocked_queueRepository.getQueueList()).thenReturn(List.of(webhookConfig));
assertDoesNotThrow(() -> queueService.checkForQueue());
verify(mocked_queueRepository).save(webhookConfig);
}

@Test
public void testCheckForQueue__Alternative() {
LPVSQueue webhookConfig = new LPVSQueue();
webhookConfig.setAttempts(100);
webhookConfig.setDate(new Date());
webhookConfig.setUserId("id");
webhookConfig.setRepositoryUrl("https://github.com/Samsung/LPVS");
when(mocked_queueRepository.getQueueList()).thenReturn(List.of(webhookConfig));
when(mocked_lpvsPullRequestRepository.saveAndFlush(Mockito.any(LPVSPullRequest.class)))
.thenAnswer(i -> i.getArguments()[0]);
assertDoesNotThrow(() -> queueService.checkForQueue());
verify(mocked_queueRepository).save(webhookConfig);
}

@Test
Expand Down

0 comments on commit bf7fd83

Please sign in to comment.