Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql): Add 2 new graphql endpoints to get results for analyzers #852

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Optional<AnalyzerResult> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ public class BadSmellGraphQL {
@Inject
ProjectRepository projectRepository;

@Query("allBadSmells")
@Description("Gets all bad smells from the database")
public List<BadSmellGraphQLDto> getAllBadSmells() {
return badSmellRepository.getAll().map(BadSmellGraphQLDto::new).toList();
}

@Query("byRuleID")
@Description("Gets all bad smells from the database by ruleID")
public List<BadSmellGraphQLDto> getAllBadSmellsByRuleID(@Name("ruleID") String ruleID) {
Expand All @@ -41,6 +35,24 @@ public List<BadSmellGraphQLDto> getAllBadSmellsByRuleID(@Name("ruleID") String r
.toList();
}

@Query("byRuleIDAndAnalyzerAndCommitHash")
@Description("Gets all bad smells from the database by ruleID and analyzer and commitHash")
public List<BadSmellGraphQLDto> 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<BadSmellGraphQLDto> 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<BadSmellGraphQLDto> getAllBadSmellsByProjectName(@Name("projectName") String projectName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<BadSmellDao> {
Expand Down Expand Up @@ -65,4 +69,23 @@ public BadSmell save(BadSmell badSmell) {
public Stream<BadSmell> getAll() {
return streamAll().map(badSmellConverter::convertToEntity);
}

@Override
public List<BadSmell> 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<BadSmell> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public interface BadSmellRepository {

List<BadSmell> findByIdentifier(String identifier);

List<BadSmell> findByCommitHash(String commitHash, String analyzerName);

List<BadSmell> findByCommitHash(String commitHash, String analyzerName, String ruleId);

long deleteByIdentifier(String identifier);

BadSmell save(BadSmell badSmell);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down