15
15
import org .slf4j .LoggerFactory ;
16
16
17
17
import com .redhat .labs .lodestar .config .JsonMarshaller ;
18
+ import com .redhat .labs .lodestar .models .Artifact ;
18
19
import com .redhat .labs .lodestar .models .Engagement ;
19
20
import com .redhat .labs .lodestar .models .EngagementUser ;
20
21
import com .redhat .labs .lodestar .models .gitlab .File ;
26
27
public class MigrationService {
27
28
private static final Logger LOGGER = LoggerFactory .getLogger (MigrationService .class );
28
29
29
- private static final String userJson = "users.json" ;
30
+ private static final String PARTICIPANT_JSON = "participants.json" ;
31
+ private static final String ARTIFACT_JSON = "artifacts.json" ;
30
32
31
33
@ Inject
32
34
EngagementService engagementService ;
@@ -43,42 +45,48 @@ public class MigrationService {
43
45
@ ConfigProperty (name = "engagements.repository.id" )
44
46
int engagementRepositoryId ;
45
47
46
- @ ConfigProperty (name = "migrate.users " )
47
- boolean migrateUsers ;
48
+ @ ConfigProperty (name = "commit.default.email " )
49
+ String commitEmail ;
48
50
49
- @ ConfigProperty (name = "migrate.uuid" )
50
- boolean migrateUuids ;
51
+ @ ConfigProperty (name = "commit.default.author" )
52
+ String commitAuthor ;
53
+
54
+ @ ConfigProperty (name = "commit.default.branch" )
55
+ String commitBranch ;
51
56
52
57
private Map <Integer , Engagement > allEngagements = new HashMap <>();
53
58
54
59
/**
55
- * Currently the migration will only occur if config properties are true.
56
60
* The migration is idempotent so no harm in rerunning. It will only update
57
- * engagements that haven't been migrated. As we get closer to migration time
58
- * we should evaluate whether this is the right approach.
59
- * Once the start up is complete this service can not be called.
60
- * @param ev
61
+ * engagements that haven't been migrated.
61
62
*/
62
- void onStart (@ Observes StartupEvent ev ) {
63
-
64
- if (migrateUsers ) {
65
- LOGGER .info ("Migrate users: {}" , migrateUsers );
66
- migrateUsers ();
67
- LOGGER .info ("End Migrate users" );
68
- }
69
-
63
+ public void migrate (boolean migrateUuids , boolean migrateParticipants , boolean migrateArtifacts ) {
70
64
if (migrateUuids ) {
71
- LOGGER .info ("Migrate uuids: {}" , migrateUuids );
65
+ LOGGER .info ("Start Migrate uuids: {}" , migrateUuids );
72
66
migrateUuids ();
73
67
LOGGER .info ("End Migrate uuids" );
74
- }
68
+ }
69
+
70
+ if (migrateParticipants ) {
71
+ LOGGER .info ("Start Migrate participants: {}" , migrateParticipants );
72
+ migrateParticipants ();
73
+ LOGGER .info ("End Migrate participants" );
74
+ }
75
75
76
+ if (migrateArtifacts ) {
77
+ LOGGER .info ("Start Migrate artifacts" );
78
+ migrateArtifacts ();
79
+ LOGGER .info ("End Migrate artifacts" );
80
+ }
76
81
}
82
+
77
83
/**
78
- * Get all projects and split for individual update
84
+ * Get all projects and split for individual update. This will add a description to any
85
+ * project that doesn't have one. The description will included the uuid of the engagement
79
86
*/
80
87
private void migrateUuids () {
81
88
List <Project > allProjects = projectService .getProjectsByGroup (engagementRepositoryId , true );
89
+ getAllEngagements (); //hydrate before stream
82
90
allProjects .parallelStream ().forEach (this ::updateProjectWithUuid );
83
91
}
84
92
@@ -94,21 +102,35 @@ private void updateProjectWithUuid(Project project) {
94
102
projectService .updateProject (project );
95
103
96
104
LOGGER .info ("Added uuid {} to project {} {}" , uuid , project .getId (), project );
105
+ } else {
106
+ LOGGER .info ("Skipped uuid update because description is already set or the project {} is not in the engagement map" , project .getId ());
97
107
}
98
108
}
99
109
100
110
/**
101
111
* Get all engagements (engagement.json) and split for individual update
102
112
*/
103
- private void migrateUsers () {
104
- getAllEngagements ().values ().parallelStream ().forEach (this ::migrateUsersToGitlab );
113
+ private void migrateParticipants () {
114
+ getAllEngagements ().values ().parallelStream ().forEach (this ::migrateParticipantsToGitlab );
115
+ }
116
+
117
+ private void migrateArtifacts () {
118
+ getAllEngagements ().values ().parallelStream ().forEach (this ::migrateArtifactsToGitlab );
119
+ }
120
+
121
+ private void migrateArtifactsToGitlab (Engagement engagement ) {
122
+ List <Artifact > artifacts = engagement .getArtifacts () == null ? Collections .emptyList () : engagement .getArtifacts ();
123
+ String content = json .toJson (artifacts );
124
+ migrateToGitlab (engagement , content , ARTIFACT_JSON , artifacts .size ());
125
+
105
126
}
106
127
107
128
/**
108
129
* Get All engagements. This is run by migrate users and uuids so only run once if both are active
109
130
* @return
110
131
*/
111
132
private Map <Integer , Engagement > getAllEngagements () {
133
+ LOGGER .debug ("Engagement count (pre-fetch) {}" , allEngagements .size ());
112
134
if (allEngagements .isEmpty ()) {
113
135
List <Engagement > engagements = engagementService .getAllEngagements (Optional .of (false ), Optional .of (false ));
114
136
engagements .parallelStream ().forEach (this ::addToMap );
@@ -131,20 +153,24 @@ private void addToMap(Engagement engagement) {
131
153
* fileService.deleteFile(engagement.getProjectId(), userJson);
132
154
* @param engagement
133
155
*/
134
- private void migrateUsersToGitlab (Engagement engagement ) {
135
-
136
- List <EngagementUser > users = engagement .getEngagementUsers ();
156
+ private void migrateParticipantsToGitlab (Engagement engagement ) {
137
157
138
- if (users == null ) {
139
- users = Collections .emptyList ();
140
- }
158
+ List <EngagementUser > participants = engagement .getEngagementUsers () == null ? Collections .emptyList () : engagement .getEngagementUsers ();
159
+ String content = json .toJson (participants );
160
+ migrateToGitlab (engagement , content , PARTICIPANT_JSON , participants .size ());
161
+ }
162
+
163
+ /**
164
+ * This will write the user json to gitlab. Should you wish to rollback or redo you could add this code
165
+ * fileService.deleteFile(engagement.getProjectId(), userJson);
166
+ * @param engagement
167
+ */
168
+ private void migrateToGitlab (Engagement engagement , String content , String fileName , int size ) {
141
169
142
- if (fileService .getFile (engagement .getProjectId (), userJson ).isEmpty ()) {
143
- String content = json .toJson (users );
144
- File file =
File .
builder ().
content (
content ).
authorEmail (
"[email protected] " ).
authorName (
"Jim Bot" ).
branch (
"master" ).
commitMessage (
"migrating users" ).
build ();
145
-
146
- fileService .createFile (engagement .getProjectId (), userJson , file );
147
- LOGGER .info ("Migrated {} users for engagement {}" , users .size (), engagement .getUuid ());
170
+ if (fileService .getFile (engagement .getProjectId (), fileName ).isEmpty ()) {
171
+ File file = File .builder ().content (content ).authorEmail (commitEmail ).authorName (commitAuthor ).branch (commitBranch ).commitMessage (String .format ("migrating %s" , fileName )).build ();
172
+ fileService .createFile (engagement .getProjectId (), fileName , file );
173
+ LOGGER .info ("Migrated {} {} for engagement {}" , size , fileName , engagement .getUuid ());
148
174
}
149
175
}
150
176
}
0 commit comments