From 0eec9ecbe8f552d6e24f86ec4ff0d07219d3d7ee Mon Sep 17 00:00:00 2001 From: Nicolas Delsaux Date: Mon, 30 Sep 2024 17:46:22 +0200 Subject: [PATCH] Fixes #63 A json schema is generated alongside artifacts file. When augmenting history, this file is also copied (when present, which will be the case starting this evening). This way we can guarantee --- .../workflows/get_mvnrepository_infos.yaml | 1 + .github/workflows/get_npmjs_infos.yaml | 1 + .github/workflows/get_pypi_infos.yaml | 1 + model/pom.xml | 7 ++++++ ...InterestingArtifactsDetailsDownloader.java | 17 +++++++++++++ .../detector/history/BaseHistoryBuilder.java | 2 ++ .../detector/history/HistoryAugmenter.java | 24 ++++++++++++++++--- 7 files changed, 50 insertions(+), 3 deletions(-) diff --git a/.github/workflows/get_mvnrepository_infos.yaml b/.github/workflows/get_mvnrepository_infos.yaml index 684c184..19e9bca 100644 --- a/.github/workflows/get_mvnrepository_infos.yaml +++ b/.github/workflows/get_mvnrepository_infos.yaml @@ -101,6 +101,7 @@ jobs: --maven-path $MAVEN ls -la mkdir target + mv schema.json target/schema.json mv artifacts.json target/artifacts.json env: VERSION: "${{steps.version.outputs.release}}" diff --git a/.github/workflows/get_npmjs_infos.yaml b/.github/workflows/get_npmjs_infos.yaml index 1a32f85..55e2a0c 100644 --- a/.github/workflows/get_npmjs_infos.yaml +++ b/.github/workflows/get_npmjs_infos.yaml @@ -85,6 +85,7 @@ jobs: java -jar npmjs-$VERSION-jar-with-dependencies.jar ls -la mkdir target + mv schema.json target/schema.json mv artifacts.json target/artifacts.json env: VERSION: "${{steps.version.outputs.release}}" diff --git a/.github/workflows/get_pypi_infos.yaml b/.github/workflows/get_pypi_infos.yaml index 381c015..6f0319d 100644 --- a/.github/workflows/get_pypi_infos.yaml +++ b/.github/workflows/get_pypi_infos.yaml @@ -95,6 +95,7 @@ jobs: --techempower-frameworks-local-clone FrameworkBenchmarks/frameworks ls -la mkdir target + mv schema.json target/schema.json mv artifacts.json target/artifacts.json env: VERSION: "${{steps.version.outputs.release}}" diff --git a/model/pom.xml b/model/pom.xml index 871fa8e..da437f0 100644 --- a/model/pom.xml +++ b/model/pom.xml @@ -14,6 +14,13 @@ com.fasterxml.jackson.core jackson-databind + + + com.fasterxml.jackson.module + jackson-module-jsonSchema-jakarta + 2.17.2 + + com.github.fge throwing-lambdas diff --git a/model/src/main/java/org/ndx/aadarchi/technology/detector/helper/InterestingArtifactsDetailsDownloader.java b/model/src/main/java/org/ndx/aadarchi/technology/detector/helper/InterestingArtifactsDetailsDownloader.java index 9c97752..07b7f9d 100644 --- a/model/src/main/java/org/ndx/aadarchi/technology/detector/helper/InterestingArtifactsDetailsDownloader.java +++ b/model/src/main/java/org/ndx/aadarchi/technology/detector/helper/InterestingArtifactsDetailsDownloader.java @@ -4,9 +4,11 @@ import java.net.http.HttpClient; import java.nio.file.Path; import java.util.Collection; +import java.util.List; import java.util.concurrent.Callable; import java.util.logging.Logger; +import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.errors.GitAPIException; import org.ndx.aadarchi.technology.detector.history.BaseHistoryBuilder; import org.ndx.aadarchi.technology.detector.loader.ArtifactLoader; @@ -16,6 +18,13 @@ import org.ndx.aadarchi.technology.detector.mappers.MappingGenerator; import org.ndx.aadarchi.technology.detector.model.ArtifactDetails; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.type.CollectionType; +import com.fasterxml.jackson.databind.type.SimpleType; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.module.jsonSchema.jakarta.JsonSchema; +import com.fasterxml.jackson.module.jsonSchema.jakarta.JsonSchemaGenerator; + import picocli.CommandLine.Option; /** @@ -44,6 +53,9 @@ public abstract class InterestingArtifactsDetailsDownloader artifactDetails) { private void writeDetails(Collection artifactDetails) { try { + JsonSchemaGenerator generator = new JsonSchemaGenerator(FileHelper.getObjectMapper()); + JsonSchema jsonSchema = generator.generateSchema(CollectionType.construct(artifactDetails.getClass(), SimpleType.construct(ArtifactDetails.class))); + FileUtils.write(schema.toFile(), + FileHelper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema), + "UTF-8"); FileHelper.writeToFile(artifactDetails, output.toFile()); } catch (IOException e) { throw new RuntimeException(e); diff --git a/model/src/main/java/org/ndx/aadarchi/technology/detector/history/BaseHistoryBuilder.java b/model/src/main/java/org/ndx/aadarchi/technology/detector/history/BaseHistoryBuilder.java index f44a7f2..f32fd88 100644 --- a/model/src/main/java/org/ndx/aadarchi/technology/detector/history/BaseHistoryBuilder.java +++ b/model/src/main/java/org/ndx/aadarchi/technology/detector/history/BaseHistoryBuilder.java @@ -51,6 +51,7 @@ public abstract class BaseHistoryBuilder { public final File artifactsFile; public final String artifactsQualifier; public final String gitBranch; + public final File schemaFile; static { String path = BaseHistoryBuilder.class.getClassLoader() @@ -68,6 +69,7 @@ public BaseHistoryBuilder(Path cache, String gitUsername, String gitEmail, Strin this.email = gitEmail; this.artifactsQualifier = artifactQualifierName; this.artifactsFile = new File(gitHistory.toFile(), artifactQualifierName + "/artifacts.json"); + this.schemaFile = new File(gitHistory.toFile(), artifactQualifierName + "/schema.json"); gitBranch = "reports_" + artifactsQualifier; } diff --git a/model/src/main/java/org/ndx/aadarchi/technology/detector/history/HistoryAugmenter.java b/model/src/main/java/org/ndx/aadarchi/technology/detector/history/HistoryAugmenter.java index 580a6db..9378ff2 100644 --- a/model/src/main/java/org/ndx/aadarchi/technology/detector/history/HistoryAugmenter.java +++ b/model/src/main/java/org/ndx/aadarchi/technology/detector/history/HistoryAugmenter.java @@ -2,13 +2,18 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.nio.file.Path; import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.eclipse.jgit.api.AddCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Status; import org.eclipse.jgit.api.errors.CheckoutConflictException; @@ -37,12 +42,14 @@ public class HistoryAugmenter { public final String gitBranchName; public final String replacerBranchName; public final File artifactsFile; + public final File schemaFile; public final Path gitHistory; public HistoryAugmenter(BaseHistoryBuilder baseHistoryBuilder) { this.gitHistory = baseHistoryBuilder.gitHistory; this.gitBranchName = baseHistoryBuilder.gitBranch; this.artifactsFile = baseHistoryBuilder.artifactsFile; + this.schemaFile = baseHistoryBuilder.schemaFile; replacerBranchName = gitBranchName + "_REPLACER"; } @@ -100,6 +107,11 @@ private void augmentCommit(Context context, Git git, RevCommit commit) throws Re commit.getCommitterIdent().getWhen(), commit.getShortMessage())); git.checkout().setName(commit.getName()).call(); + // Don't forget to read schema! + Optional schema = schemaFile.exists() + ? Optional.of(FileUtils.readFileToString(schemaFile, "UTF-8")) + : Optional.empty() + ; // Now load artifacts into data structure List artifacts = FileHelper.readFromFile(artifactsFile); // Augment each artifact @@ -119,18 +131,24 @@ private void augmentCommit(Context context, Git git, RevCommit commit) throws Re .call(); // Write the file FileHelper.writeToFile(augmented, artifactsFile); + // And the schema + schema.ifPresent(Throwing.consumer(s -> + FileUtils.writeStringToFile(schemaFile, s, Charset.forName("UTF-8")))); // Commit with all values read from initial commit PersonIdent commiter = commit.getAuthorIdent(); String commitMessage = commit.getFullMessage(); - String commitedFile = gitHistory.relativize(artifactsFile.toPath()).toString(); Status response = git.status().call(); if(!response.getAdded().isEmpty()) { - git.add().addFilepattern(commitedFile).call(); + AddCommand addCommand = git.add(); + for(String s : response.getAdded()) { + addCommand.addFilepattern(s); + } + addCommand.call(); } git.commit() .setAuthor(commiter) .setCommitter(commiter) - .setOnly(commitedFile) +// .setOnly(commitedFile) .setAllowEmpty(true) .setMessage(commitMessage) .call();