1
1
package com .redhat .labs .lodestar .service ;
2
2
3
+ import java .security .*;
3
4
import java .time .Instant ;
4
5
import java .time .LocalDateTime ;
5
6
import java .time .ZoneOffset ;
@@ -27,6 +28,7 @@ public class MigrationService {
27
28
private static final String ARTIFACT_JSON = ENGAGEMENT_DIR + "artifacts.json" ;
28
29
private static final String HOSTING_JSON = ENGAGEMENT_DIR + "hosting.json" ;
29
30
private static final String ENGAGEMENT_JSON = ENGAGEMENT_DIR + "engagement.json" ;
31
+ private static final String CATEGORY_JSON = ENGAGEMENT_DIR + "category.json" ;
30
32
31
33
@ Inject
32
34
EngagementService engagementService ;
@@ -53,7 +55,7 @@ public class MigrationService {
53
55
String commitBranch ;
54
56
55
57
private final Map <Integer , Engagement > allEngagements = new HashMap <>();
56
-
58
+
57
59
/**
58
60
* The migration is idempotent so no harm in rerunning. It will only update
59
61
* engagements that haven't been migrated.
@@ -65,6 +67,10 @@ public void migrate(boolean migrateUuids, boolean migrateParticipants, boolean m
65
67
66
68
getAllEngagements (); //hydrate before stream
67
69
70
+ if (LOGGER .isDebugEnabled ()) {
71
+ getAllEngagements ().values ().forEach (e -> LOGGER .debug ("To Migrate {}" , e .getUuid ()));
72
+ }
73
+
68
74
if (migrateUuids ) {
69
75
LOGGER .info ("Start Migrate uuids" );
70
76
migrateUuids ();
@@ -80,6 +86,7 @@ public void migrate(boolean migrateUuids, boolean migrateParticipants, boolean m
80
86
private void migrateAll (boolean migrateParticipants , boolean migrateArtifacts , boolean migrateHosting ,
81
87
boolean migrateEngagements , boolean overwrite , List <String > uuids ) {
82
88
int counter = 0 ;
89
+ LOGGER .debug ("Migration engagement count {}" , getAllEngagements ().size ());
83
90
for (Engagement e : getAllEngagements ().values ()) {
84
91
85
92
if (uuids .isEmpty () || uuids .contains (e .getUuid ())) {
@@ -91,6 +98,8 @@ private void migrateAll(boolean migrateParticipants, boolean migrateArtifacts, b
91
98
if (migrateEngagements ) {
92
99
content = migrateEngagement (e );
93
100
actions .add (createAction (content , ENGAGEMENT_JSON , overwrite , e .getProjectId ()));
101
+ content = migrateCategories (e );
102
+ actions .add (createAction (content , CATEGORY_JSON , overwrite , e .getProjectId ()));
94
103
}
95
104
96
105
if (migrateParticipants ) {
@@ -109,7 +118,7 @@ private void migrateAll(boolean migrateParticipants, boolean migrateArtifacts, b
109
118
}
110
119
111
120
if (!actions .isEmpty ()) {
112
- String commitMessage = "Migrating to v2" ;
121
+ String commitMessage = String . format ( "Migrating to v2 %s" , getEmoji ()) ;
113
122
114
123
CommitMultiple commit = CommitMultiple .builder ().id (e .getProjectId ()).branch (commitBranch ).commitMessage (commitMessage ).actions (actions )
115
124
.authorName (commitAuthor ).authorEmail (commitEmail ).build ();
@@ -142,35 +151,39 @@ private Action createAction(String content, String filePath, boolean overwrite,
142
151
*/
143
152
private void migrateUuids () {
144
153
List <Project > allProjects = projectService .getProjectsByGroup (engagementRepositoryId , true );
145
-
146
154
allProjects .parallelStream ().forEach (this ::updateProjectWithUuid );
147
155
}
148
156
149
- private String migrateEngagement (Engagement engagement ) {
157
+ private String migrateCategories (Engagement engagement ) {
150
158
Engagement copy = clone (engagement , Engagement .class );
151
159
152
- if (copy .getCategories () != null ) {
160
+ if (copy .getCategories () == null ) {
161
+ copy .setCategories (new ArrayList <>());
162
+ } else {
153
163
copy .getCategories ().forEach (cat -> {
154
164
try {
155
165
cat .setCreated (convertDateTime (cat .getCreated ()));
156
- cat .setUpdated (convertDateTime (cat .getUpdated ()));
166
+ cat .setEngagementUuid (engagement .getUuid ());
167
+ cat .setRegion (engagement .getRegion ());
157
168
} catch (Exception ex ) {
158
169
LOGGER .error ("dtf exception {}" , cat .getCreated (), ex );
159
170
}
160
171
});
161
172
}
162
173
163
- if (copy .getUseCases () != null ) {
164
- copy . getUseCases (). forEach (( use -> {
165
- try {
166
- use . setCreated ( convertDateTime ( use . getCreated ()));
167
- use . setUpdated ( convertDateTime ( use . getUpdated ()) );
168
- } catch ( RuntimeException ex ) {
169
- LOGGER . error ( "dtf exception {}" , use . getCreated (), ex );
170
- }
171
- } ));
174
+ return json . toJson (copy .getCategories ());
175
+ }
176
+
177
+ private String migrateEngagement ( Engagement engagement ) {
178
+ Engagement copy = clone ( engagement , Engagement . class );
179
+ Set < String > cats = new TreeSet <>();
180
+
181
+ if ( copy . getCategories () != null ) {
182
+ copy . getCategories (). forEach ( cat -> cats . add ( cat . getName () ));
172
183
}
173
184
185
+ copy .setMapCategories (cats );
186
+
174
187
if (copy .getLaunch () != null ) {
175
188
try {
176
189
Instant .parse (copy .getLaunch ().getLaunchedDateTime ());
@@ -180,6 +193,31 @@ private String migrateEngagement(Engagement engagement) {
180
193
}
181
194
}
182
195
196
+ if (copy .getUseCases () != null ) {
197
+ for (UseCase use : copy .getUseCases ()) {
198
+ try {
199
+ Instant .parse (use .getCreated ());
200
+ } catch (DateTimeParseException ex ) {
201
+ LOGGER .error ("No standard use case create date {}" , use .getCreated ());
202
+ use .setCreated (convertDateTime (use .getCreated ()));
203
+ }
204
+
205
+ try {
206
+ Instant .parse (use .getUpdated ());
207
+ } catch (DateTimeParseException ex ) {
208
+ LOGGER .error ("No standard use case update date {}" , use .getUpdated ());
209
+ use .setUpdated (convertDateTime (use .getUpdated ()));
210
+ }
211
+ }
212
+ }
213
+
214
+ copy .setMapRegion (copy .getRegion ());
215
+ copy .setMapType (copy .getType ());
216
+ copy .setName (copy .getProjectName ());
217
+ copy .setCategories (null );
218
+ copy .setRegion (null );
219
+ copy .setType (null );
220
+ copy .setProjectName (null );
183
221
184
222
copy .setHostingEnvironments (null );
185
223
copy .setEngagementUsers (null );
@@ -198,12 +236,12 @@ private String convertDateTime(String oldDateTime) {
198
236
199
237
String date = null ;
200
238
201
- for ( int i = 0 ; i < patterns . length ; i ++ ) {
239
+ for ( String pattern : patterns ) {
202
240
try {
203
- date = convertDateTime (oldDateTime , patterns [ i ] );
241
+ date = convertDateTime (oldDateTime , pattern );
204
242
break ;
205
243
} catch (DateTimeParseException ex ) {
206
- LOGGER .error ("No standard date {}, pattern {}" , oldDateTime , patterns [ i ] );
244
+ LOGGER .error ("No standard date {}, pattern {}" , oldDateTime , pattern );
207
245
}
208
246
}
209
247
@@ -302,4 +340,14 @@ private String migrateHostingToGitlab(Engagement engagement) {
302
340
return json .toJson (hosting );
303
341
}
304
342
343
+ private String getEmoji () {
344
+ String bear = "\ud83d \udc3b " ;
345
+
346
+ int bearCodePoint = bear .codePointAt (bear .offsetByCodePoints (0 , 0 ));
347
+ int mysteryAnimalCodePoint = bearCodePoint + new SecureRandom ().nextInt (144 );
348
+ char [] mysteryEmoji = { Character .highSurrogate (mysteryAnimalCodePoint ),
349
+ Character .lowSurrogate (mysteryAnimalCodePoint ) };
350
+
351
+ return String .valueOf (mysteryEmoji );
352
+ }
305
353
}
0 commit comments