1
1
/**
2
- * Copyright (c) 2022 , Samsung Electronics Co., Ltd. All rights reserved.
2
+ * Copyright (c) 2024 , Samsung Electronics Co., Ltd. All rights reserved.
3
3
*
4
4
* Use of this source code is governed by a MIT license that can be
5
5
* found in the LICENSE file.
6
6
*/
7
7
package com .lpvs .service ;
8
8
9
- import com .lpvs .entity .LPVSFile ;
10
- import com .lpvs .entity .LPVSLicense ;
11
- import com .lpvs .entity .LPVSPullRequest ;
12
9
import com .lpvs .entity .LPVSQueue ;
13
- import com .lpvs .entity .enums .LPVSPullRequestStatus ;
14
- import com .lpvs .repository .LPVSPullRequestRepository ;
15
10
import com .lpvs .repository .LPVSQueueRepository ;
16
- import com .lpvs .service .scan .LPVSDetectService ;
17
- import com .lpvs .util .LPVSPayloadUtil ;
18
11
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 ;
21
13
import org .springframework .stereotype .Service ;
22
-
23
- import java .nio .file .Files ;
24
- import java .nio .file .Paths ;
25
14
import java .util .*;
26
15
import java .util .concurrent .BlockingDeque ;
27
16
import java .util .concurrent .LinkedBlockingDeque ;
33
22
@ Slf4j
34
23
public class LPVSQueueService {
35
24
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
-
56
25
/**
57
26
* Repository for storing LPVSQueue entities.
58
27
*/
59
- private LPVSQueueRepository queueRepository ;
60
-
61
- /**
62
- * Maximum attempts for processing LPVSQueue elements.
63
- */
64
- private int maxAttempts ;
28
+ private final LPVSQueueRepository queueRepository ;
65
29
66
30
/**
67
31
* BlockingDeque for managing LPVSQueue elements.
@@ -71,26 +35,11 @@ public class LPVSQueueService {
71
35
/**
72
36
* Constructor for LPVSQueueService.
73
37
*
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.
80
39
*/
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 ) {
92
42
this .queueRepository = queueRepository ;
93
- this .maxAttempts = maxAttempts ;
94
43
}
95
44
96
45
/**
@@ -156,135 +105,4 @@ public void checkForQueue() throws InterruptedException {
156
105
QUEUE .putFirst (webhook );
157
106
}
158
107
}
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
- }
290
108
}
0 commit comments