-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathCodeLocationCreationService.java
More file actions
100 lines (80 loc) · 5.75 KB
/
CodeLocationCreationService.java
File metadata and controls
100 lines (80 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* blackduck-common
*
* Copyright (c) 2024 Black Duck Software, Inc.
*
* Use subject to the terms and conditions of the Black Duck Software End User Software License and Maintenance Agreement. All rights reserved worldwide.
*/
package com.blackduck.integration.blackduck.codelocation;
import com.blackduck.integration.blackduck.api.core.response.UrlSingleResponse;
import com.blackduck.integration.blackduck.api.generated.discovery.ApiDiscovery;
import com.blackduck.integration.blackduck.api.generated.view.UserView;
import com.blackduck.integration.blackduck.service.BlackDuckApiClient;
import com.blackduck.integration.blackduck.service.DataService;
import com.blackduck.integration.blackduck.service.dataservice.NotificationService;
import com.blackduck.integration.blackduck.service.dataservice.UserService;
import com.blackduck.integration.blackduck.service.model.NotificationTaskRange;
import com.blackduck.integration.exception.IntegrationException;
import com.blackduck.integration.log.IntLogger;
import com.blackduck.integration.util.NameVersion;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Optional;
import java.util.Set;
public class CodeLocationCreationService extends DataService {
public static final int DEFAULT_WAIT_INTERVAL_IN_SECONDS = 60;
private final CodeLocationWaiter codeLocationWaiter;
private final NotificationService notificationService;
private final UserService userService;
public CodeLocationCreationService(BlackDuckApiClient blackDuckApiClient, ApiDiscovery apiDiscovery, IntLogger logger, CodeLocationWaiter codeLocationWaiter, NotificationService notificationService, UserService userService) {
super(blackDuckApiClient, apiDiscovery, logger);
this.codeLocationWaiter = codeLocationWaiter;
this.notificationService = notificationService;
this.userService = userService;
}
public <T extends CodeLocationBatchOutput<?>> CodeLocationCreationData<T> createCodeLocations(CodeLocationCreationRequest<T> codeLocationCreationRequest) throws IntegrationException {
NotificationTaskRange notificationTaskRange = calculateCodeLocationRange();
T output = codeLocationCreationRequest.executeRequest();
return new CodeLocationCreationData<>(notificationTaskRange, output);
}
public <T extends CodeLocationBatchOutput<?>> CodeLocationCreationData<T> createCodeLocationsWithoutNotificationTaskRange(CodeLocationCreationRequest<T> codeLocationCreationRequest) throws IntegrationException {
T output = codeLocationCreationRequest.executeRequest();
return new CodeLocationCreationData<>(null, output);
}
public <T extends CodeLocationBatchOutput<?>> T createCodeLocationsAndWait(CodeLocationCreationRequest<T> codeLocationCreationRequest, long timeoutInSeconds) throws IntegrationException, InterruptedException {
return createCodeLocationsAndWait(codeLocationCreationRequest, timeoutInSeconds, DEFAULT_WAIT_INTERVAL_IN_SECONDS);
}
public <T extends CodeLocationBatchOutput<?>> T createCodeLocationsAndWait(CodeLocationCreationRequest<T> codeLocationCreationRequest, long timeoutInSeconds, int waitIntervalInSeconds) throws IntegrationException, InterruptedException {
CodeLocationCreationData<T> codeLocationCreationData = createCodeLocations(codeLocationCreationRequest);
NotificationTaskRange notificationTaskRange = codeLocationCreationData.getNotificationTaskRange();
T output = codeLocationCreationData.getOutput();
Optional<NameVersion> projectAndVersion = output.getProjectAndVersion();
if (projectAndVersion.isPresent()) {
waitForCodeLocations(notificationTaskRange, projectAndVersion.get(), output.getSuccessfulCodeLocationNames(), output.getExpectedNotificationCount(), timeoutInSeconds, waitIntervalInSeconds);
} else {
logger.info("Cannot wait for a code location that is not mapped to a project version. Skipping.");
}
return output;
}
public CodeLocationWaitResult waitForCodeLocations(NotificationTaskRange notificationTaskRange, NameVersion projectAndVersion, Set<String> codeLocationNames,
int expectedNotificationCount, long timeoutInSeconds) throws IntegrationException, InterruptedException {
return waitForCodeLocations(notificationTaskRange, projectAndVersion, codeLocationNames, expectedNotificationCount, timeoutInSeconds, DEFAULT_WAIT_INTERVAL_IN_SECONDS);
}
public CodeLocationWaitResult waitForCodeLocations(NotificationTaskRange notificationTaskRange, NameVersion projectAndVersion, Set<String> codeLocationNames,
int expectedNotificationCount, long timeoutInSeconds, int waitIntervalInSeconds) throws IntegrationException, InterruptedException {
UserView currentUser = userService.findCurrentUser();
return codeLocationWaiter.checkCodeLocationsAddedToBom(currentUser, notificationTaskRange, projectAndVersion, codeLocationNames, expectedNotificationCount, timeoutInSeconds, waitIntervalInSeconds);
}
public NotificationTaskRange calculateCodeLocationRange() throws IntegrationException {
long startTime = System.currentTimeMillis();
LocalDateTime localStartTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneOffset.UTC);
LocalDateTime threeDaysLater = localStartTime.plusDays(3);
UrlSingleResponse<UserView> userResponse = apiDiscovery.metaSingleResponse(ApiDiscovery.CURRENT_USER_PATH);
UserView currentUser = blackDuckApiClient.getResponse(userResponse);
Date startDate = new Date(System.currentTimeMillis());
Date endDate = Date.from(threeDaysLater.atZone(ZoneOffset.UTC).toInstant());
return new NotificationTaskRange(startTime, startDate, endDate);
}
}