From 98fb0a291af178850ecc1cdeba54c4a5c9e77f50 Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Sun, 16 Jul 2023 00:47:47 +0200 Subject: [PATCH] feat(graphql): Add 2 new graphql endpoints to get results for analyzers --- .../analyzer/spoon/AnalyzerResultVisitor.java | 2 +- .../graphql/endpoints/BadSmellGraphQL.java | 24 ++++++++++++++----- .../impl/MongoBadSmellRepository.java | 23 ++++++++++++++++++ .../repository/BadSmellRepository.java | 4 ++++ .../persistence/DatabaseTest.java | 10 ++++++++ 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java index e0e39d98d..774ad4759 100644 --- a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java +++ b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java @@ -38,7 +38,7 @@ public static Optional toAnalyzerResult(BadSmell badSmell) { return Optional.ofNullable(badSmell.accept(analyzerResultVisitor)); } catch (Exception e) { - logger.atWarning().withCause(e).withStackTrace(StackSize.NONE).log( + logger.atWarning().withStackTrace(StackSize.NONE).log( "Could not convert bad smell to analyzer result %s", badSmell.getClass().getCanonicalName()); return Optional.empty(); diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/api/graphql/endpoints/BadSmellGraphQL.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/api/graphql/endpoints/BadSmellGraphQL.java index 2215ad7ee..bb5c1fc74 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/api/graphql/endpoints/BadSmellGraphQL.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/api/graphql/endpoints/BadSmellGraphQL.java @@ -27,12 +27,6 @@ public class BadSmellGraphQL { @Inject ProjectRepository projectRepository; - @Query("allBadSmells") - @Description("Gets all bad smells from the database") - public List getAllBadSmells() { - return badSmellRepository.getAll().map(BadSmellGraphQLDto::new).toList(); - } - @Query("byRuleID") @Description("Gets all bad smells from the database by ruleID") public List getAllBadSmellsByRuleID(@Name("ruleID") String ruleID) { @@ -41,6 +35,24 @@ public List getAllBadSmellsByRuleID(@Name("ruleID") String r .toList(); } + @Query("byRuleIDAndAnalyzerAndCommitHash") + @Description("Gets all bad smells from the database by ruleID and analyzer and commitHash") + public List getBadSmellsByRuleIdAndAnalyzer( + @Name("ruleID") String ruleID, @Name("analyzer") String analyzer, @Name("commitHash") String commitHash) { + return badSmellRepository.findByCommitHash(commitHash, analyzer, ruleID).stream() + .map(this::mapToDto) + .toList(); + } + + @Query("byAndAnalyzerAndCommitHash") + @Description("Gets all bad smells from the database by analyzer and commitHash") + public List getBadSmellsByRuleIdAndAnalyzer( + @Name("analyzer") String analyzer, @Name("commitHash") String commitHash) { + return badSmellRepository.findByCommitHash(commitHash, analyzer).stream() + .map(this::mapToDto) + .toList(); + } + @Query("byProjectName") @Description("Gets all bad smells from the database by projectName") public List getAllBadSmellsByProjectName(@Name("projectName") String projectName) { diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/impl/MongoBadSmellRepository.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/impl/MongoBadSmellRepository.java index 10ec61240..f4a252774 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/impl/MongoBadSmellRepository.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/impl/MongoBadSmellRepository.java @@ -1,6 +1,7 @@ package io.github.martinwitt.laughing_train.persistence.impl; import com.google.common.flogger.FluentLogger; +import com.mongodb.client.model.Filters; import io.github.martinwitt.laughing_train.domain.value.RuleId; import io.github.martinwitt.laughing_train.persistence.BadSmell; import io.github.martinwitt.laughing_train.persistence.converter.BadSmellDaoConverter; @@ -9,7 +10,10 @@ import io.quarkus.mongodb.panache.PanacheMongoRepository; import jakarta.enterprise.context.ApplicationScoped; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.stream.StreamSupport; +import org.bson.conversions.Bson; @ApplicationScoped public class MongoBadSmellRepository implements BadSmellRepository, PanacheMongoRepository { @@ -65,4 +69,23 @@ public BadSmell save(BadSmell badSmell) { public Stream getAll() { return streamAll().map(badSmellConverter::convertToEntity); } + + @Override + public List findByCommitHash(String commitHash, String analyzerName) { + Bson filter = Filters.and(Filters.eq("commitHash", commitHash), Filters.eq("analyzer", analyzerName)); + return StreamSupport.stream(mongoCollection().find(filter).spliterator(), false) + .map(badSmellConverter::convertToEntity) + .collect(Collectors.toList()); + } + + @Override + public List findByCommitHash(String commitHash, String analyzerName, String ruleId) { + Bson filter = Filters.and( + Filters.eq("commitHash", commitHash), + Filters.eq("analyzer", analyzerName), + Filters.eq("ruleID", ruleId)); + return StreamSupport.stream(mongoCollection().find(filter).spliterator(), false) + .map(badSmellConverter::convertToEntity) + .collect(Collectors.toList()); + } } diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/repository/BadSmellRepository.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/repository/BadSmellRepository.java index 8d1cc0886..e3c568fbb 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/repository/BadSmellRepository.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/repository/BadSmellRepository.java @@ -17,6 +17,10 @@ public interface BadSmellRepository { List findByIdentifier(String identifier); + List findByCommitHash(String commitHash, String analyzerName); + + List findByCommitHash(String commitHash, String analyzerName, String ruleId); + long deleteByIdentifier(String identifier); BadSmell save(BadSmell badSmell); diff --git a/github-bot/src/test/java/io/github/martinwitt/laughing_train/persistence/DatabaseTest.java b/github-bot/src/test/java/io/github/martinwitt/laughing_train/persistence/DatabaseTest.java index 11f009301..a3b8be940 100644 --- a/github-bot/src/test/java/io/github/martinwitt/laughing_train/persistence/DatabaseTest.java +++ b/github-bot/src/test/java/io/github/martinwitt/laughing_train/persistence/DatabaseTest.java @@ -72,6 +72,16 @@ void filterForAnyContains() { .isNotNull(); } + @Test + void testMultiSearchQuery() { + cleanDB(); + var badSmell = createWithMessage("PointLessBoolean"); + badSmellRepository.save(badSmell); + var badSmell2 = createWithMessage("PointLessBoolean"); + badSmellRepository.save(badSmell2); + assertThat(badSmellRepository.findByCommitHash("test", "JUNIT")).size().isEqualTo(2); + } + /** * Cleans the database before each test. */