3
3
import java .security .SecureRandom ;
4
4
import java .util .ArrayList ;
5
5
import java .util .List ;
6
+ import java .util .Map ;
6
7
import java .util .Optional ;
7
8
import java .util .stream .Collectors ;
9
+ import java .util .stream .Stream ;
8
10
9
11
import javax .annotation .PostConstruct ;
10
12
import javax .enterprise .context .ApplicationScoped ;
15
17
import org .slf4j .Logger ;
16
18
import org .slf4j .LoggerFactory ;
17
19
20
+ import com .google .common .collect .Lists ;
18
21
import com .redhat .labs .lodestar .config .JsonMarshaller ;
19
22
import com .redhat .labs .lodestar .exception .UnexpectedGitLabResponseException ;
20
23
import com .redhat .labs .lodestar .models .Engagement ;
@@ -37,6 +40,9 @@ public class EngagementService {
37
40
private static final String DEFAULT_BRANCH = "master" ;
38
41
private static final String ENGAGEMENT_FILE = "engagement.json" ;
39
42
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" ;
40
46
41
47
private String engagementPathPrefix ;
42
48
@@ -46,6 +52,9 @@ public class EngagementService {
46
52
@ ConfigProperty (name = "stripPathPrefix" , defaultValue = "schema/" )
47
53
String stripPathPrefix ;
48
54
55
+ @ ConfigProperty (name = "orchestration.queue.directory" , defaultValue = "queue" )
56
+ String orchestrationQueueDirectory ;
57
+
49
58
@ Inject
50
59
ProjectService projectService ;
51
60
@@ -93,6 +102,9 @@ public Project createEngagement(Engagement engagement, String author, String aut
93
102
List <File > repoFiles = new ArrayList <>();
94
103
repoFiles .add (createEngagmentFile (engagement ));
95
104
105
+ // create user reset file(s) if required
106
+ repoFiles .addAll (createUserManagementFiles (engagement ));
107
+
96
108
// create actions for multiple commit
97
109
CommitMultiple commit = createCommitMultiple (repoFiles , project .getId (), DEFAULT_BRANCH , author , authorEmail ,
98
110
project .isFirst (), commitMessageOptional );
@@ -245,14 +257,46 @@ private File createEngagmentFile(Engagement engagement) {
245
257
return File .builder ().content (fileContent ).filePath (ENGAGEMENT_FILE ).build ();
246
258
}
247
259
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
+
248
282
private CommitMultiple createCommitMultiple (List <File > filesToCommit , Integer projectId , String branch ,
249
283
String authorName , String authorEmail , boolean isNew , Optional <String > commitMessageOptional ) {
250
284
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 ());
252
296
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 ( ));
256
300
257
301
// use message if provided. otherwise, defaults
258
302
String commitMessage = commitMessageOptional
0 commit comments