Skip to content

Commit 2c765a9

Browse files
committed
initial commit
1 parent 7fcedd3 commit 2c765a9

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed

src/main/java/com/redhat/labs/lodestar/models/EngagementUser.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.redhat.labs.lodestar.models;
22

33
import javax.json.bind.annotation.JsonbProperty;
4+
import javax.json.bind.annotation.JsonbTransient;
45
import javax.validation.constraints.NotBlank;
56

67
import lombok.AllArgsConstructor;
@@ -14,6 +15,9 @@
1415
@AllArgsConstructor
1516
public class EngagementUser {
1617

18+
@NotBlank
19+
@JsonbProperty("uuid")
20+
private String uuid;
1721
@NotBlank
1822
@JsonbProperty("first_name")
1923
private String firstName;
@@ -27,4 +31,13 @@ public class EngagementUser {
2731
@JsonbProperty("role")
2832
private String role;
2933

34+
private boolean reset;
35+
36+
@JsonbTransient
37+
public boolean isReset() {
38+
return reset;
39+
}
40+
41+
42+
3043
}

src/main/java/com/redhat/labs/lodestar/service/EngagementService.java

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.security.SecureRandom;
44
import java.util.ArrayList;
55
import java.util.List;
6+
import java.util.Map;
67
import java.util.Optional;
78
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
810

911
import javax.annotation.PostConstruct;
1012
import javax.enterprise.context.ApplicationScoped;
@@ -18,6 +20,7 @@
1820
import com.redhat.labs.lodestar.config.JsonMarshaller;
1921
import com.redhat.labs.lodestar.exception.UnexpectedGitLabResponseException;
2022
import com.redhat.labs.lodestar.models.Engagement;
23+
import com.redhat.labs.lodestar.models.EngagementUser;
2124
import com.redhat.labs.lodestar.models.Status;
2225
import com.redhat.labs.lodestar.models.gitlab.Action;
2326
import com.redhat.labs.lodestar.models.gitlab.Commit;
@@ -37,6 +40,9 @@ public class EngagementService {
3740
private static final String DEFAULT_BRANCH = "master";
3841
private static final String ENGAGEMENT_FILE = "engagement.json";
3942
private static final String STATUS_FILE = "status.json";
43+
private static final String USER_MGMT_FILE_PREFIX = "user-management-";
44+
private static final String USER_MGMT_FILE = USER_MGMT_FILE_PREFIX + "UUID.json";
45+
private static final String USER_MGMT_FILE_PLACEHOLDER = "UUID";
4046

4147
private String engagementPathPrefix;
4248

@@ -46,6 +52,9 @@ public class EngagementService {
4652
@ConfigProperty(name = "stripPathPrefix", defaultValue = "schema/")
4753
String stripPathPrefix;
4854

55+
@ConfigProperty(name = "orchestration.queue.directory", defaultValue = "queue")
56+
String orchestrationQueueDirectory;
57+
4958
@Inject
5059
ProjectService projectService;
5160

@@ -93,6 +102,10 @@ public Project createEngagement(Engagement engagement, String author, String aut
93102
List<File> repoFiles = new ArrayList<>();
94103
repoFiles.add(createEngagmentFile(engagement));
95104

105+
// create user reset file if required
106+
List<File> resetFiles = createUserManagementFiles(engagement);
107+
repoFiles.addAll(resetFiles);
108+
96109
// create actions for multiple commit
97110
CommitMultiple commit = createCommitMultiple(repoFiles, project.getId(), DEFAULT_BRANCH, author, authorEmail,
98111
project.isFirst(), commitMessageOptional);
@@ -245,14 +258,69 @@ private File createEngagmentFile(Engagement engagement) {
245258
return File.builder().content(fileContent).filePath(ENGAGEMENT_FILE).build();
246259
}
247260

261+
private List<File> createUserManagementFiles(Engagement engagement) {
262+
263+
List<File> userResetFiles = new ArrayList<>();
264+
265+
// get all users that requested a reset
266+
List<EngagementUser> users = engagement.getEngagementUsers().stream().filter(user -> user.isReset())
267+
.collect(Collectors.toList());
268+
269+
// create file for each reset request only if the file doesn't already exist
270+
for (EngagementUser user : users) {
271+
272+
// create file name
273+
String fileName = getUserManagementFileName(user.getUuid());
274+
275+
// create full path for file name
276+
String fileNameWithPath = getUserManagementPath(engagement.getCustomerName(), engagement.getProjectName(),
277+
fileName);
278+
279+
// see if file exists
280+
Optional<File> userResetFile = fileService.getFileAllow404(engagement.getProjectId(), fileNameWithPath);
281+
282+
if (userResetFile.isEmpty()) {
283+
284+
// create file
285+
String userAsJson = json.toJson(user);
286+
287+
File resetFile = File.builder().content(userAsJson).filePath(fileName).build();
288+
userResetFiles.add(resetFile);
289+
290+
}
291+
292+
}
293+
294+
return userResetFiles;
295+
296+
}
297+
298+
private String getUserManagementFileName(String uuid) {
299+
return USER_MGMT_FILE.replace(USER_MGMT_FILE_PLACEHOLDER, uuid);
300+
}
301+
302+
private String getUserManagementPath(String customerName, String projectName, String fileName) {
303+
return new StringBuilder(GitLabPathUtils.getPath(engagementPathPrefix, customerName, projectName)).append("/")
304+
.append(orchestrationQueueDirectory).append("/").append(fileName).toString();
305+
}
306+
248307
private CommitMultiple createCommitMultiple(List<File> filesToCommit, Integer projectId, String branch,
249308
String authorName, String authorEmail, boolean isNew, Optional<String> commitMessageOptional) {
250309

251-
List<Action> actions = new ArrayList<>();
310+
// Split files between user-management files and all others
311+
Map<Boolean, List<File>> fileMap = filesToCommit.stream()
312+
.collect(Collectors.partitioningBy(file -> file.getFilePath().contains(USER_MGMT_FILE_PREFIX)));
313+
314+
// create actions for each user management file
315+
List<Action> userManagementFiles = fileMap.get(true).stream().map(file -> createAction(file, true))
316+
.collect(Collectors.toList());
317+
318+
// create actions for all other files
319+
List<Action> otherFiles = fileMap.get(false).stream().map(file -> createAction(file, isNew))
320+
.collect(Collectors.toList());
252321

253-
// convert each file to action - parallelStream was bringing inconsistent
254-
// results
255-
filesToCommit.stream().forEach(file -> actions.add(createAction(file, isNew)));
322+
// merge the actions
323+
List<Action> actions = Stream.of(userManagementFiles, otherFiles).flatMap(x -> x.stream()).collect(Collectors.toList());
256324

257325
// use message if provided. otherwise, defaults
258326
String commitMessage = commitMessageOptional

src/main/java/com/redhat/labs/lodestar/service/ProjectStructureService.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ProjectStructureService {
3535

3636
@ConfigProperty(name = "engagements.repository.id")
3737
Integer engagementRepositoryId;
38-
38+
3939
@ConfigProperty(name = "gitlab.deploy.key")
4040
Integer deployKey;
4141

@@ -65,9 +65,9 @@ public Project createOrUpdateProjectStructure(Engagement engagement, String enga
6565

6666
Optional<Project> project = createOrUpdateProject(existingProjectStructure.getProject(),
6767
existingProjectStructure.getProjectGroupId(), projectGroup.getId());
68-
68+
6969
// enable deployment key on project
70-
if(project.isPresent()) {
70+
if (project.isPresent()) {
7171
projectService.enableDeploymentKeyOnProject(project.get().getId(), deployKey);
7272
}
7373

@@ -101,7 +101,7 @@ ProjectStructure getExistingProjectStructure(Engagement engagement, String engag
101101
}
102102

103103
// get project group by id
104-
if(project.isEmpty()) {
104+
if (project.isEmpty()) {
105105
project = projectService.getProjectById(engagement.getProjectId());
106106
}
107107
builder.project(project);
@@ -230,14 +230,11 @@ void cleanupGroups(ProjectStructure existingProjectStructure) {
230230
return;
231231
}
232232

233-
Instant begin = Instant.now();
234233
// remove project group
235234
removeGroupIfEmpty(existingProjectStructure.getProjectGroupId().get(), 5);
236235
// remove customer group
237236
removeGroupIfEmpty(existingProjectStructure.getCustomerGroupId().get(), 5);
238237

239-
System.out.println("cleanup elapsed time: " + Duration.between(begin, Instant.now()).toMillis() + " ms");
240-
241238
}
242239

243240
void removeGroupIfEmpty(Integer groupId, int retryCount) {
@@ -264,11 +261,9 @@ void removeGroupIfEmpty(Integer groupId, int retryCount) {
264261

265262
count += 1;
266263
try {
267-
System.out.println("sleeping for " + count * 1 + " seconds.");
268264
TimeUnit.SECONDS.sleep(count * 1);
269265
} catch (InterruptedException e) {
270-
// TODO Auto-generated catch block
271-
e.printStackTrace();
266+
Thread.currentThread().interrupt();
272267
}
273268

274269
}

0 commit comments

Comments
 (0)