Skip to content

Commit

Permalink
fix(spoon): Also persist analyzerresults in database (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Jul 18, 2023
1 parent 3b3b95a commit 6e588ea
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class AnalyzerStatus implements Serializable {
private String commitHash;
private LocalDateTime localDateTime;

public AnalyzerStatus() {
// for JPA
}

AnalyzerStatus(
String analyzerName, Status status, int numberOfIssues, String commitHash, LocalDateTime localDateTime) {
this.analyzerName = analyzerName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ public class GitHubCommit implements Serializable {
private String commitHash;
private List<AnalyzerStatus> analyzerStatuses;

public GitHubCommit() {
// for JPA
}
/**
* @param commitHash
* @param localDateTime
* @param analyzerStatuses
*/
public GitHubCommit(String commitHash, List<AnalyzerStatus> analyzerStatuses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ private void addOrUpdateCommitHash(Project project, CodeAnalyzerResult spoonResu
RemoteProject newProject = new RemoteProject(name, project.url());
newProject.addCommitHash(commitHash);
List<GitHubCommit> commits = newProject.getCommits();
commits.stream()
var selectedCommit = commits.stream()
.filter(v -> v.getCommitHash().equals(commitHash))
.findFirst()
.ifPresent(v -> v.addAnalyzerStatus(analyzerStatus));
.findFirst();
if (selectedCommit.isPresent()) {
selectedCommit.get().addAnalyzerStatus(analyzerStatus);
logger.atInfo().log(
"Adding new commit hash for %s with status %s for analyzer %s",
name, analyzerStatus.getStatus(), analyzerStatus.getAnalyzerName());
}
projectRepository.create(newProject);
} else {
logger.atInfo().log("Updating commit hash for %s", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.google.common.flogger.FluentLogger;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import io.github.martinwitt.laughing_train.domain.entity.AnalyzerStatus;
import io.github.martinwitt.laughing_train.domain.entity.GitHubCommit;
import io.github.martinwitt.laughing_train.domain.entity.ProjectConfig;
import io.github.martinwitt.laughing_train.domain.entity.RemoteProject;
import io.github.martinwitt.laughing_train.persistence.impl.MongoBadSmellRepository;
Expand Down Expand Up @@ -78,7 +76,6 @@ private void migrateDataBase(Promise<Object> promise) {
removeDuplicatedProjects();
removeRuleIdsWithSpaces();
removeBadSmellsWithWrongFolder();
removeAnalyzerStatusWithoutLocalDataTime();
logger.atInfo().log("Finished migrating database");
promise.complete();
}
Expand Down Expand Up @@ -149,23 +146,4 @@ private void removeBadSmellsWithWrongFolder() {
.deleteMany(Filters.and(Filters.regex("filePath", ".*/tmp/.*"), Filters.eq("analyzer", "Spoon")));
logger.atInfo().log("Removed %d bad smells with ruleId containing spaces", deleteMany.getDeletedCount());
}

private void removeAnalyzerStatusWithoutLocalDataTime() {
logger.atInfo().log("Removing analyzer status without local data");
for (RemoteProject remoteProject : projectRepository.getAll()) {
for (GitHubCommit commits : remoteProject.getCommits()) {
List<AnalyzerStatus> list = new ArrayList<>();
for (AnalyzerStatus analyzerStatus : commits.getAnalyzerStatuses()) {
if (analyzerStatus.getLocalDateTime() != null) {
list.add(analyzerStatus);
}
}
if (list.size() != commits.getAnalyzerStatuses().size()) {
commits.setAnalyzerStatuses(list);
projectRepository.deleteByProjectUrl(remoteProject.getProjectUrl());
projectRepository.save(remoteProject);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ public ProjectDao convertToDao(RemoteProject entity) {
dao.setProjectName(entity.getProjectName());
dao.setProjectUrl(entity.getProjectUrl());
dao.setCommitHashes(entity.getCommitHashes());
dao.setCommits(entity.getCommits());
return dao;
}

@Override
public RemoteProject convertToEntity(ProjectDao dao) {
var project = new RemoteProject(dao.getProjectName(), dao.getProjectUrl());
dao.getCommitHashes().forEach(project::addCommitHash);
dao.getCommits().forEach(project::addCommitHash);
return project;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.martinwitt.laughing_train.persistence.dao;

import io.github.martinwitt.laughing_train.domain.entity.GitHubCommit;
import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
import java.util.List;
Expand All @@ -10,6 +11,7 @@ public class ProjectDao extends PanacheMongoEntity {
private String projectName;
private String projectUrl;
private List<String> commitHashes;
private List<GitHubCommit> commits;

public ProjectDao() {
// for JPA
Expand Down Expand Up @@ -57,6 +59,19 @@ public void setCommitHashes(List<String> commitHashes) {
this.commitHashes = commitHashes;
}

/**
* @return the commits
*/
public List<GitHubCommit> getCommits() {
return commits;
}
/**
* @param commits the commits to set
*/
public void setCommits(List<GitHubCommit> commits) {
this.commits = commits;
}

@Override
public String toString() {
return "{" + " projectName='"
Expand Down

0 comments on commit 6e588ea

Please sign in to comment.