Skip to content

Commit dce00d7

Browse files
authored
refactor: Separate queue service and implement factory for webhooks (#599)
* refactor: Separate queue service and implement factory for webhooks Signed-off-by: Oleg Kopysov <[email protected]> * fix: Fix for the async start of threds Signed-off-by: Oleg Kopysov <[email protected]> * fix: Apply fix after code review Signed-off-by: Oleg Kopysov <[email protected]> --------- Signed-off-by: Oleg Kopysov <[email protected]>
1 parent 5485f13 commit dce00d7

11 files changed

+1269
-998
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
3+
*
4+
* Use of this source code is governed by a MIT license that can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
/**
9+
* This package contains entity classes that represent various types of reports generated by the LPVS service.
10+
*/
11+
package com.lpvs.entity.report;

src/main/java/com/lpvs/service/LPVSQueueProcessorService.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/**
2-
* Copyright (c) 2022, Samsung Electronics Co., Ltd. All rights reserved.
2+
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
33
*
44
* Use of this source code is governed by a MIT license that can be
55
* found in the LICENSE file.
66
*/
77
package com.lpvs.service;
88

99
import com.lpvs.entity.LPVSQueue;
10+
import com.lpvs.service.webhook.LPVSWebhookService;
11+
import com.lpvs.service.webhook.LPVSWebhookServiceFactory;
1012
import io.micrometer.common.util.StringUtils;
1113
import lombok.extern.slf4j.Slf4j;
1214
import org.springframework.beans.factory.annotation.Autowired;
@@ -41,13 +43,20 @@ public class LPVSQueueProcessorService {
4143
@Value("${local.path:}")
4244
private String localPath;
4345

46+
@Autowired private LPVSWebhookService webhookService;
47+
4448
/**
4549
* Constructor for LPVSQueueProcessorService.
4650
*
4751
* @param queueService The LPVSQueueService to be injected.
52+
* @param webhookServiceFactory Service for creating instance of the webhook service.
53+
* @param isInternal Indicates the mode of LPVS operation.
4854
*/
49-
@Autowired
50-
LPVSQueueProcessorService(LPVSQueueService queueService) {
55+
LPVSQueueProcessorService(
56+
LPVSQueueService queueService,
57+
LPVSWebhookServiceFactory webhookServiceFactory,
58+
@Value("${lpvs.mode.internal:false}") boolean isInternal) {
59+
this.webhookService = webhookServiceFactory.createWebhookService(isInternal);
5160
this.queueService = queueService;
5261
}
5362

@@ -72,7 +81,7 @@ protected void queueProcessor() throws Exception {
7281
webhookConfig.setDate(new Date());
7382

7483
// Process the LPVSQueue element.
75-
queueService.processWebHook(webhookConfig);
84+
webhookService.processWebHook(webhookConfig);
7685
}
7786
}
7887
}
Lines changed: 6 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
/**
2-
* Copyright (c) 2022, Samsung Electronics Co., Ltd. All rights reserved.
2+
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
33
*
44
* Use of this source code is governed by a MIT license that can be
55
* found in the LICENSE file.
66
*/
77
package com.lpvs.service;
88

9-
import com.lpvs.entity.LPVSFile;
10-
import com.lpvs.entity.LPVSLicense;
11-
import com.lpvs.entity.LPVSPullRequest;
129
import com.lpvs.entity.LPVSQueue;
13-
import com.lpvs.entity.enums.LPVSPullRequestStatus;
14-
import com.lpvs.repository.LPVSPullRequestRepository;
1510
import com.lpvs.repository.LPVSQueueRepository;
16-
import com.lpvs.service.scan.LPVSDetectService;
17-
import com.lpvs.util.LPVSPayloadUtil;
1811
import lombok.extern.slf4j.Slf4j;
19-
import org.springframework.beans.factory.annotation.Value;
20-
import org.springframework.scheduling.annotation.Async;
12+
import org.springframework.beans.factory.annotation.Autowired;
2113
import org.springframework.stereotype.Service;
22-
23-
import java.nio.file.Files;
24-
import java.nio.file.Paths;
2514
import java.util.*;
2615
import java.util.concurrent.BlockingDeque;
2716
import java.util.concurrent.LinkedBlockingDeque;
@@ -33,35 +22,10 @@
3322
@Slf4j
3423
public class LPVSQueueService {
3524

36-
/**
37-
* Service for interacting with GitHub.
38-
*/
39-
private LPVSGitHubService gitHubService;
40-
41-
/**
42-
* Service for detecting licenses in scanned files.
43-
*/
44-
private LPVSDetectService detectService;
45-
46-
/**
47-
* Service for managing licenses and license conflicts.
48-
*/
49-
private LPVSLicenseService licenseService;
50-
51-
/**
52-
* Repository for storing LPVSPullRequest entities.
53-
*/
54-
private LPVSPullRequestRepository lpvsPullRequestRepository;
55-
5625
/**
5726
* Repository for storing LPVSQueue entities.
5827
*/
59-
private LPVSQueueRepository queueRepository;
60-
61-
/**
62-
* Maximum attempts for processing LPVSQueue elements.
63-
*/
64-
private int maxAttempts;
28+
private final LPVSQueueRepository queueRepository;
6529

6630
/**
6731
* BlockingDeque for managing LPVSQueue elements.
@@ -71,26 +35,11 @@ public class LPVSQueueService {
7135
/**
7236
* Constructor for LPVSQueueService.
7337
*
74-
* @param gitHubService Service for interacting with GitHub.
75-
* @param detectService Service for detecting licenses in scanned files.
76-
* @param licenseService Service for managing licenses and license conflicts.
77-
* @param lpvsPullRequestRepository Repository for storing LPVSPullRequest entities.
78-
* @param queueRepository Repository for storing LPVSQueue entities.
79-
* @param maxAttempts Maximum attempts for processing LPVSQueue elements.
38+
* @param queueRepository Repository for storing LPVSQueue entities.
8039
*/
81-
public LPVSQueueService(
82-
LPVSGitHubService gitHubService,
83-
LPVSDetectService detectService,
84-
LPVSLicenseService licenseService,
85-
LPVSPullRequestRepository lpvsPullRequestRepository,
86-
LPVSQueueRepository queueRepository,
87-
@Value("${lpvs.attempts:4}") int maxAttempts) {
88-
this.gitHubService = gitHubService;
89-
this.detectService = detectService;
90-
this.licenseService = licenseService;
91-
this.lpvsPullRequestRepository = lpvsPullRequestRepository;
40+
@Autowired
41+
public LPVSQueueService(LPVSQueueRepository queueRepository) {
9242
this.queueRepository = queueRepository;
93-
this.maxAttempts = maxAttempts;
9443
}
9544

9645
/**
@@ -156,135 +105,4 @@ public void checkForQueue() throws InterruptedException {
156105
QUEUE.putFirst(webhook);
157106
}
158107
}
159-
160-
/**
161-
* Gets the LPVSQueue element with the latest scan date.
162-
*
163-
* @param webhookConfigList The list of LPVSQueue elements.
164-
* @return The LPVSQueue element with the latest scan date.
165-
*/
166-
public LPVSQueue getLatestScan(List<LPVSQueue> webhookConfigList) {
167-
LPVSQueue latestWebhookConfig = webhookConfigList.get(0);
168-
for (LPVSQueue webhookConfig : webhookConfigList) {
169-
if (latestWebhookConfig.getDate().compareTo(webhookConfig.getDate()) < 0) {
170-
latestWebhookConfig = webhookConfig;
171-
}
172-
}
173-
return latestWebhookConfig;
174-
}
175-
176-
/**
177-
* Asynchronously processes the LPVSQueue element, handling GitHub webhook events.
178-
*
179-
* @param webhookConfig The LPVSQueue element to be processed.
180-
*/
181-
@Async("threadPoolTaskExecutor")
182-
public void processWebHook(LPVSQueue webhookConfig) {
183-
Long id = webhookConfig.getId();
184-
log.info(
185-
"Processing webhook ID: "
186-
+ id
187-
+ ", attempt: "
188-
+ (webhookConfig.getAttempts() + 1)
189-
+ " for PR: "
190-
+ webhookConfig.getPullRequestUrl());
191-
LPVSPullRequest pullRequest =
192-
lpvsPullRequestRepository.findLatestByPullRequestInfo(
193-
webhookConfig.getUserId(),
194-
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
195-
+ "/"
196-
+ LPVSPayloadUtil.getRepositoryName(webhookConfig),
197-
webhookConfig.getPullRequestFilesUrl(),
198-
webhookConfig.getPullRequestHead(),
199-
webhookConfig.getPullRequestBase(),
200-
webhookConfig.getSender(),
201-
LPVSPullRequestStatus.INTERNAL_ERROR.getPullRequestStatus());
202-
203-
if (pullRequest == null) {
204-
pullRequest = new LPVSPullRequest();
205-
pullRequest.setUser(webhookConfig.getUserId());
206-
pullRequest.setRepositoryName(
207-
LPVSPayloadUtil.getRepositoryOrganization(webhookConfig)
208-
+ "/"
209-
+ LPVSPayloadUtil.getRepositoryName(webhookConfig));
210-
pullRequest.setPullRequestUrl(webhookConfig.getPullRequestUrl());
211-
pullRequest.setPullRequestFilesUrl(webhookConfig.getPullRequestFilesUrl());
212-
pullRequest.setPullRequestHead(webhookConfig.getPullRequestHead());
213-
pullRequest.setPullRequestBase(webhookConfig.getPullRequestBase());
214-
pullRequest.setSender(webhookConfig.getSender());
215-
}
216-
217-
try {
218-
219-
pullRequest.setDate(webhookConfig.getDate());
220-
pullRequest.setStatus(LPVSPullRequestStatus.SCANNING.toString());
221-
pullRequest = lpvsPullRequestRepository.saveAndFlush(pullRequest);
222-
223-
String filePath = gitHubService.getPullRequestFiles(webhookConfig);
224-
if (filePath != null && Files.list(Paths.get(filePath)).count() != 0) {
225-
log.debug("Successfully downloaded files");
226-
227-
if (filePath.contains(":::::")) {
228-
filePath = filePath.split(":::::")[0];
229-
}
230-
// check repository license
231-
String repositoryLicense = gitHubService.getRepositoryLicense(webhookConfig);
232-
233-
if (repositoryLicense != null) {
234-
LPVSLicense repoLicense =
235-
licenseService.getLicenseBySpdxIdAndName(
236-
repositoryLicense, Optional.empty());
237-
webhookConfig.setRepositoryLicense(repoLicense.getSpdxId());
238-
} else {
239-
webhookConfig.setRepositoryLicense(null);
240-
}
241-
log.debug("Repository license: " + webhookConfig.getRepositoryLicense());
242-
243-
List<LPVSFile> files = detectService.runScan(webhookConfig, filePath);
244-
245-
// check license conflicts
246-
List<LPVSLicenseService.Conflict<String, String>> detectedConflicts =
247-
licenseService.findConflicts(webhookConfig, files);
248-
249-
log.debug("Creating comment");
250-
gitHubService.commentResults(webhookConfig, files, detectedConflicts, pullRequest);
251-
log.debug("Results posted on GitHub");
252-
delete(webhookConfig);
253-
} else {
254-
log.warn("Files are not found. Probably pull request does not exist.");
255-
throw new Exception(
256-
"Files are not found. Probably pull request does not exist. Terminating.");
257-
}
258-
} catch (Exception | Error e) {
259-
pullRequest.setStatus(LPVSPullRequestStatus.INTERNAL_ERROR.toString());
260-
pullRequest = lpvsPullRequestRepository.saveAndFlush(pullRequest);
261-
log.error("Can't authorize commentResults() " + e.getMessage());
262-
int currentAttempts = webhookConfig.getAttempts() + 1;
263-
if (currentAttempts < maxAttempts) {
264-
webhookConfig.setAttempts(currentAttempts);
265-
try {
266-
addFirst(webhookConfig);
267-
} catch (InterruptedException e1) {
268-
log.warn("Failed to update Queue element");
269-
}
270-
queueRepository.save(webhookConfig);
271-
} else {
272-
log.warn(
273-
"Maximum amount of processing webhook reached for pull request: "
274-
+ pullRequest.getId()
275-
+ " "
276-
+ pullRequest.getPullRequestUrl());
277-
try {
278-
gitHubService.commentResults(webhookConfig, null, null, pullRequest);
279-
} catch (Exception ex) {
280-
log.warn("Failed to post FAIL result " + ex.getMessage());
281-
}
282-
delete(webhookConfig);
283-
log.info(
284-
"Webhook ID: "
285-
+ id
286-
+ " - removed from the queue because the number of attempts exceeded the max value");
287-
}
288-
}
289-
}
290108
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
3+
*
4+
* Use of this source code is governed by a MIT license that can be
5+
* found in the LICENSE file.
6+
*/
7+
package com.lpvs.service.webhook;
8+
9+
import com.lpvs.entity.LPVSQueue;
10+
11+
/**
12+
* The LPVSWebhookService interface defines the contract for processing LPVSQueue elements and handling webhook events.
13+
*/
14+
public interface LPVSWebhookService {
15+
16+
/**
17+
* Processes the LPVSQueue element, handling webhook events.
18+
*
19+
* @param webhookConfig The LPVSQueue element to be processed.
20+
*/
21+
void processWebHook(LPVSQueue webhookConfig);
22+
}

0 commit comments

Comments
 (0)