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 ;
18
20
import com .redhat .labs .lodestar .config .JsonMarshaller ;
19
21
import com .redhat .labs .lodestar .exception .UnexpectedGitLabResponseException ;
20
22
import com .redhat .labs .lodestar .models .Engagement ;
23
+ import com .redhat .labs .lodestar .models .EngagementUser ;
21
24
import com .redhat .labs .lodestar .models .Status ;
22
25
import com .redhat .labs .lodestar .models .gitlab .Action ;
23
26
import com .redhat .labs .lodestar .models .gitlab .Commit ;
@@ -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,10 @@ 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 if required
106
+ List <File > resetFiles = createUserManagementFiles (engagement );
107
+ repoFiles .addAll (resetFiles );
108
+
96
109
// create actions for multiple commit
97
110
CommitMultiple commit = createCommitMultiple (repoFiles , project .getId (), DEFAULT_BRANCH , author , authorEmail ,
98
111
project .isFirst (), commitMessageOptional );
@@ -245,14 +258,69 @@ private File createEngagmentFile(Engagement engagement) {
245
258
return File .builder ().content (fileContent ).filePath (ENGAGEMENT_FILE ).build ();
246
259
}
247
260
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
+
248
307
private CommitMultiple createCommitMultiple (List <File > filesToCommit , Integer projectId , String branch ,
249
308
String authorName , String authorEmail , boolean isNew , Optional <String > commitMessageOptional ) {
250
309
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 ());
252
321
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 ());
256
324
257
325
// use message if provided. otherwise, defaults
258
326
String commitMessage = commitMessageOptional
0 commit comments