|
9 | 9 | import javax.json.bind.Jsonb;
|
10 | 10 | import javax.ws.rs.WebApplicationException;
|
11 | 11 |
|
| 12 | +import com.google.gson.Gson; |
| 13 | +import com.google.gson.GsonBuilder; |
| 14 | +import com.google.gson.JsonElement; |
| 15 | +import com.google.gson.JsonObject; |
| 16 | +import com.redhat.labs.lodestar.model.gitlab.Action; |
| 17 | +import com.redhat.labs.lodestar.model.gitlab.Commit; |
12 | 18 | import io.quarkus.panache.common.Sort;
|
13 | 19 | import org.eclipse.microprofile.config.inject.ConfigProperty;
|
14 | 20 | import org.eclipse.microprofile.rest.client.inject.RestClient;
|
@@ -59,6 +65,8 @@ public class ArtifactService {
|
59 | 65 | @Inject
|
60 | 66 | Jsonb jsonb;
|
61 | 67 |
|
| 68 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 69 | + |
62 | 70 | private static final Javers JAVERS = JaversBuilder.javers()
|
63 | 71 | .withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE).build();
|
64 | 72 |
|
@@ -150,7 +158,7 @@ public void updateArtifacts(String engagementUuid, String region, List<Artifact>
|
150 | 158 | processObjectChange(cbo, requestArtifacts);
|
151 | 159 |
|
152 | 160 | commitMessage.append(cbo.toString());
|
153 |
| - updateArtifactsFile(engagementUuid, authorEmail, authorName, Optional.ofNullable(commitMessage.toString())); |
| 161 | + updateArtifactsFile(engagementUuid, authorEmail.orElse(defaultAuthorEmail), authorName.orElse(defaultAuthorName), Optional.ofNullable(commitMessage.toString())); |
154 | 162 | if(existing.size() != requestArtifacts.size()) {
|
155 | 163 | engagementRestClient.updateEngagement(engagementUuid, requestArtifacts.size());
|
156 | 164 | }
|
@@ -336,45 +344,41 @@ void updateArtifact(Artifact artifact, Artifact existing) {
|
336 | 344 | * @param authorName
|
337 | 345 | * @param commitMessage
|
338 | 346 | */
|
339 |
| - public void updateArtifactsFile(String engagementUuid, Optional<String> authorEmail, |
340 |
| - Optional<String> authorName, Optional<String> commitMessage) { |
| 347 | + public void updateArtifactsFile(String engagementUuid, String authorEmail, |
| 348 | + String authorName, Optional<String> commitMessage) { |
341 | 349 |
|
342 | 350 | // find project by engagement
|
343 | 351 | Engagement project = engagementRestClient.getEngagementByUuid(engagementUuid);
|
344 | 352 |
|
345 | 353 | List<Artifact> artifacts = Artifact.findAllByEngagementUuid(engagementUuid);
|
346 | 354 | String content = jsonb.toJson(artifacts);
|
347 | 355 |
|
348 |
| - // create |
349 |
| - File file = createArtifactsFile(content, defaultBranch, authorName, authorEmail, commitMessage); |
| 356 | + List<Action> actions = List.of( |
| 357 | + Action.builder().filePath(artifactsFile).content(content).build(), |
| 358 | + createLegacyEngagementAction(project.getProjectId(), content) |
| 359 | + ); |
| 360 | + |
| 361 | + Commit commit = Commit.builder().commitMessage(commitMessage.orElse("Artifact Update")).branch(defaultBranch) |
| 362 | + .authorEmail(authorEmail).authorName(authorName).actions(actions).build(); |
350 | 363 |
|
351 | 364 | // update in git
|
352 |
| - gitlabRestClient.updateFile(project.getProjectId(), artifactsFile, file); |
| 365 | + gitlabRestClient.createCommit(project.getProjectId(), commit); |
353 | 366 | }
|
354 | 367 |
|
355 |
| - /** |
356 |
| - * Creates and returns a {@link File} with the given parameters and encodes the |
357 |
| - * content. |
358 |
| - * |
359 |
| - * @param content |
360 |
| - * @param branch |
361 |
| - * @param authorName |
362 |
| - * @param authorEmail |
363 |
| - * @param commitMessage |
364 |
| - * @return |
365 |
| - */ |
366 |
| - File createArtifactsFile(String content, String branch, Optional<String> authorName, Optional<String> authorEmail, |
367 |
| - Optional<String> commitMessage) { |
| 368 | + Action createLegacyEngagementAction(long projectId, String artifactContent) { |
| 369 | + File f = gitlabRestClient.getFile(projectId, "engagement.json", defaultBranch); |
| 370 | + f.decodeFileAttributes(); |
| 371 | + |
| 372 | + JsonElement element = gson.fromJson(f.getContent(), JsonElement.class); |
| 373 | + JsonObject engagement = element.getAsJsonObject(); |
368 | 374 |
|
369 |
| - // create file |
370 |
| - File artifactFile = File.builder().filePath(artifactsFile).content(content) |
371 |
| - .authorEmail(authorEmail.orElse(defaultAuthorEmail)).authorName(authorName.orElse(defaultAuthorName)) |
372 |
| - .branch(branch).commitMessage(commitMessage.orElse(defaultCommitMessage)).build(); |
| 375 | + element = gson.fromJson(artifactContent, JsonElement.class); |
373 | 376 |
|
374 |
| - // encode before sending |
375 |
| - artifactFile.encodeFileAttributes(); |
| 377 | + engagement.add("artifacts", element); |
| 378 | + JsonObject sorted = new JsonObject(); |
| 379 | + engagement.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(es -> sorted.add(es.getKey(), es.getValue())); |
376 | 380 |
|
377 |
| - return artifactFile; |
| 381 | + return Action.builder().filePath("engagement.json").content(gson.toJson(sorted)).build(); |
378 | 382 |
|
379 | 383 | }
|
380 | 384 |
|
|
0 commit comments