44import com .google .common .eventbus .EventBus ;
55import com .google .inject .Inject ;
66import com .google .inject .persist .Transactional ;
7+ import lombok .SneakyThrows ;
78import lombok .extern .slf4j .Slf4j ;
89import nl .tudelft .ewi .devhub .server .database .entities .Commit ;
910import nl .tudelft .ewi .devhub .server .database .entities .Commit .CommitId ;
1011import nl .tudelft .ewi .devhub .server .database .entities .RepositoryEntity ;
1112import 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
1516import javax .persistence .EntityManager ;
1617import java .util .Collection ;
1718import java .util .Date ;
1819import java .util .List ;
1920import java .util .Optional ;
21+ import java .util .stream .Collectors ;
2022import java .util .stream .Stream ;
2123
2224import static nl .tudelft .ewi .devhub .server .database .entities .QCommit .commit ;
2527public 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
0 commit comments