Skip to content

Commit 04466fb

Browse files
authored
Merge pull request #106 from dwasinge/user-reset
User Reset Implementation
2 parents 7fcedd3 + f000b64 commit 04466fb

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-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: 48 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;
@@ -15,6 +17,7 @@
1517
import org.slf4j.Logger;
1618
import org.slf4j.LoggerFactory;
1719

20+
import com.google.common.collect.Lists;
1821
import com.redhat.labs.lodestar.config.JsonMarshaller;
1922
import com.redhat.labs.lodestar.exception.UnexpectedGitLabResponseException;
2023
import com.redhat.labs.lodestar.models.Engagement;
@@ -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,9 @@ 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(s) if required
106+
repoFiles.addAll(createUserManagementFiles(engagement));
107+
96108
// create actions for multiple commit
97109
CommitMultiple commit = createCommitMultiple(repoFiles, project.getId(), DEFAULT_BRANCH, author, authorEmail,
98110
project.isFirst(), commitMessageOptional);
@@ -245,14 +257,46 @@ private File createEngagmentFile(Engagement engagement) {
245257
return File.builder().content(fileContent).filePath(ENGAGEMENT_FILE).build();
246258
}
247259

260+
private List<File> createUserManagementFiles(Engagement engagement) {
261+
262+
if (null == engagement.getEngagementUsers()) {
263+
return Lists.newArrayList();
264+
}
265+
266+
return engagement.getEngagementUsers().stream().filter(user -> user.isReset())
267+
.filter(user -> fileService
268+
.getFileAllow404(engagement.getProjectId(), getUserManagementFileName(user.getUuid()))
269+
.isEmpty())
270+
.map(user -> {
271+
return File.builder().content(json.toJson(user)).filePath(getUserManagementFileName(user.getUuid()))
272+
.build();
273+
}).collect(Collectors.toList());
274+
275+
}
276+
277+
private String getUserManagementFileName(String uuid) {
278+
return new StringBuilder(orchestrationQueueDirectory).append("/")
279+
.append(USER_MGMT_FILE.replace(USER_MGMT_FILE_PLACEHOLDER, uuid)).toString();
280+
}
281+
248282
private CommitMultiple createCommitMultiple(List<File> filesToCommit, Integer projectId, String branch,
249283
String authorName, String authorEmail, boolean isNew, Optional<String> commitMessageOptional) {
250284

251-
List<Action> actions = new ArrayList<>();
285+
// Split files between user-management files and all others
286+
Map<Boolean, List<File>> fileMap = filesToCommit.stream()
287+
.collect(Collectors.partitioningBy(file -> file.getFilePath().contains(USER_MGMT_FILE_PREFIX)));
288+
289+
// create actions for each user management file
290+
List<Action> userManagementFiles = fileMap.get(true).stream().map(file -> createAction(file, true))
291+
.collect(Collectors.toList());
292+
293+
// create actions for all other files
294+
List<Action> otherFiles = fileMap.get(false).stream().map(file -> createAction(file, isNew))
295+
.collect(Collectors.toList());
252296

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

257301
// use message if provided. otherwise, defaults
258302
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)