Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 29e9043

Browse files
Merge pull request #484 from devhub-tud/event-bus
Moved enhancer back to commits controller
2 parents 09ce96d + f059ef0 commit 29e9043

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

src/main/java/nl/tudelft/ewi/devhub/server/database/controllers/Commits.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
import com.google.common.eventbus.EventBus;
55
import com.google.inject.Inject;
66
import com.google.inject.persist.Transactional;
7+
import lombok.SneakyThrows;
78
import lombok.extern.slf4j.Slf4j;
89
import nl.tudelft.ewi.devhub.server.database.entities.Commit;
910
import nl.tudelft.ewi.devhub.server.database.entities.Commit.CommitId;
1011
import nl.tudelft.ewi.devhub.server.database.entities.RepositoryEntity;
1112
import nl.tudelft.ewi.devhub.server.events.CreateCommitEvent;
12-
import org.hibernate.BaseSessionEventListener;
13-
import org.hibernate.Session;
13+
import nl.tudelft.ewi.git.models.CommitModel;
14+
import nl.tudelft.ewi.git.web.api.RepositoriesApi;
1415

1516
import javax.persistence.EntityManager;
1617
import java.util.Collection;
1718
import java.util.Date;
1819
import java.util.List;
1920
import java.util.Optional;
21+
import java.util.stream.Collectors;
2022
import java.util.stream.Stream;
2123

2224
import static nl.tudelft.ewi.devhub.server.database.entities.QCommit.commit;
@@ -25,11 +27,13 @@
2527
public class Commits extends Controller<Commit> {
2628

2729
private final EventBus eventBus;
30+
private final RepositoriesApi repositories;
2831

2932
@Inject
30-
public Commits(final EntityManager entityManager, final EventBus eventBus) {
33+
public Commits(final EntityManager entityManager, final RepositoriesApi repositoriesApi, final EventBus eventBus) {
3134
super(entityManager);
3235
this.eventBus = eventBus;
36+
this.repositories = repositoriesApi;
3337
}
3438

3539
@Transactional
@@ -60,6 +64,7 @@ public Commit ensureExists(RepositoryEntity repositoryEntity, String commitId) {
6064
commit.setRepository(repositoryEntity);
6165
commit.setComments(Lists.newArrayList());
6266
commit.setPushTime(new Date());
67+
enhanceCommitSafely(commit);
6368

6469
CreateCommitEvent createCommitEvent = new CreateCommitEvent();
6570
createCommitEvent.setCommitId(commitId);
@@ -69,6 +74,37 @@ public Commit ensureExists(RepositoryEntity repositoryEntity, String commitId) {
6974
});
7075
}
7176

77+
/**
78+
* Enhance a commit with details from the git server, such as commit time, author information and parents.
79+
*
80+
* @param commit Commit object to modify.
81+
*/
82+
public void enhanceCommitSafely(Commit commit) {
83+
try {
84+
log.info("Enhance {} {}", commit.getRepository().getRepositoryName(), commit.getCommitId());
85+
RepositoryEntity repositoryEntity = commit.getRepository();
86+
String commitId = commit.getCommitId();
87+
final CommitModel gitCommit = retrieveCommit(repositoryEntity, commitId);
88+
commit.setCommitTime(new Date(gitCommit.getTime() * 1000));
89+
commit.setAuthor(gitCommit.getAuthor());
90+
commit.setParents(
91+
Stream.of(gitCommit.getParents()).sequential()
92+
.map(c -> ensureExists(repositoryEntity, c))
93+
.collect(Collectors.toList())
94+
);
95+
}
96+
catch (Exception e) {
97+
log.warn("Failed to retrieve commit details: " + e.getMessage(), e);
98+
}
99+
}
100+
101+
@SneakyThrows
102+
protected CommitModel retrieveCommit(RepositoryEntity repositoryEntity, String commitId) {
103+
return repositories.getRepository(repositoryEntity.getRepositoryName())
104+
.getCommit(commitId)
105+
.get();
106+
}
107+
72108
/**
73109
* Check if a commit exists
74110
* @param repository RepositoryEntity

src/main/java/nl/tudelft/ewi/devhub/server/eventhandlers/CommitEnhancer.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
import nl.tudelft.ewi.devhub.server.database.entities.Commit;
1111
import nl.tudelft.ewi.devhub.server.database.entities.RepositoryEntity;
1212
import nl.tudelft.ewi.devhub.server.events.CreateCommitEvent;
13-
import nl.tudelft.ewi.git.models.CommitModel;
1413
import nl.tudelft.ewi.git.models.DiffModel;
1514
import nl.tudelft.ewi.git.web.api.RepositoriesApi;
1615

1716
import javax.inject.Inject;
1817
import javax.inject.Provider;
19-
import java.util.Date;
20-
import java.util.stream.Collectors;
21-
import java.util.stream.Stream;
2218

2319
/**
2420
* @author Jan-Willem Gmelig Meyling
@@ -54,24 +50,9 @@ protected void enhanceCommitWithinTransaction(CreateCommitEvent createCommitEven
5450

5551
log.info("Enhance {} {}", commit.getRepository().getRepositoryName(), commit.getCommitId());
5652

57-
final CommitModel gitCommit = retrieveCommit(repositoryEntity, createCommitEvent.getCommitId());
5853
final DiffModel diffModel = retrieveDiffModel(repositoryEntity, createCommitEvent.getCommitId());
5954
commit.setLinesAdded(diffModel.getLinesAdded());
6055
commit.setLinesRemoved(diffModel.getLinesRemoved());
61-
commit.setCommitTime(new Date(gitCommit.getTime() * 1000));
62-
commit.setAuthor(gitCommit.getAuthor());
63-
commit.setParents(
64-
Stream.of(gitCommit.getParents()).sequential()
65-
.map(c -> commitsProvider.get().ensureExists(repositoryEntity, c))
66-
.collect(Collectors.toList())
67-
);
68-
}
69-
70-
@SneakyThrows
71-
protected CommitModel retrieveCommit(RepositoryEntity repositoryEntity, String commitId) {
72-
return repositoriesApiProvider.get().getRepository(repositoryEntity.getRepositoryName())
73-
.getCommit(commitId)
74-
.get();
7556
}
7657

7758
@SneakyThrows

0 commit comments

Comments
 (0)