Skip to content

Commit

Permalink
Add ReviewComments and refactor IssueComments
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Sep 6, 2024
1 parent 7ff900b commit fb5d518
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.tum.in.www1.hephaestus.codereview.base;

import de.tum.in.www1.hephaestus.codereview.user.User;
import jakarta.persistence.Basic;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
public abstract class Comment extends BaseGitServiceEntity {
@Lob
@Basic(fetch = FetchType.EAGER)
protected String body;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
@ToString.Exclude
protected User author;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package de.tum.in.www1.hephaestus.codereview.comment;

import jakarta.persistence.Table;

import org.springframework.lang.NonNull;

import de.tum.in.www1.hephaestus.codereview.base.BaseGitServiceEntity;
import de.tum.in.www1.hephaestus.codereview.base.Comment;
import de.tum.in.www1.hephaestus.codereview.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.codereview.user.User;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -24,17 +17,7 @@
@Setter
@NoArgsConstructor
@ToString(callSuper = true)
public class IssueComment extends BaseGitServiceEntity {
@NonNull
@Lob
@Basic(fetch = FetchType.EAGER)
private String body;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
@ToString.Exclude
private User author;

public class IssueComment extends Comment {
@ManyToOne(optional = false)
@JoinColumn(name = "pullrequest_id", referencedColumnName = "id")
@ToString.Exclude
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.tum.in.www1.hephaestus.codereview.comment.review;

import org.springframework.lang.NonNull;

import de.tum.in.www1.hephaestus.codereview.base.Comment;
import de.tum.in.www1.hephaestus.codereview.pullrequest.review.PullRequestReview;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Entity
@Table(name = "review_comment")
@Getter
@Setter
@NoArgsConstructor
@ToString(callSuper = true)
public class ReviewComment extends Comment {
@ManyToOne(optional = false)
@JoinColumn(name = "review_id", referencedColumnName = "id")
@ToString.Exclude
private PullRequestReview review;

@NonNull
private String commit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.tum.in.www1.hephaestus.codereview.comment.review;

import org.kohsuke.github.GHPullRequestReviewComment;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import de.tum.in.www1.hephaestus.codereview.base.BaseGitServiceEntityConverter;

@Component
public class ReviewCommentConverter extends BaseGitServiceEntityConverter<GHPullRequestReviewComment, ReviewComment> {

@Override
public ReviewComment convert(@NonNull GHPullRequestReviewComment source) {
ReviewComment comment = new ReviewComment();
convertBaseFields(source, comment);
comment.setBody(source.getBody());
comment.setCommit(source.getCommitId());
return comment;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.tum.in.www1.hephaestus.codereview.comment.review;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ReviewCommentRepository extends JpaRepository<ReviewComment, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public class PullRequest extends BaseGitServiceEntity {

@OneToMany(cascade = CascadeType.ALL, mappedBy = "pullRequest")
@ToString.Exclude
private Set<IssueComment> comments = new HashSet<>();;
private Set<IssueComment> comments = new HashSet<>();

@OneToMany(cascade = CascadeType.ALL, mappedBy = "pullRequest")
@ToString.Exclude
private Set<PullRequestReview> reviews = new HashSet<>();;
private Set<PullRequestReview> reviews = new HashSet<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "repository_id", referencedColumnName = "id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package de.tum.in.www1.hephaestus.codereview.pullrequest.review;

import java.time.OffsetDateTime;
import java.util.HashSet;
import java.util.Set;

import org.springframework.lang.NonNull;

import de.tum.in.www1.hephaestus.codereview.base.BaseGitServiceEntity;
import de.tum.in.www1.hephaestus.codereview.comment.review.ReviewComment;
import de.tum.in.www1.hephaestus.codereview.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.codereview.user.User;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -39,8 +44,16 @@ public class PullRequestReview extends BaseGitServiceEntity {

private OffsetDateTime updatedAt;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "review")
@ToString.Exclude
private Set<ReviewComment> comments = new HashSet<>();

@ManyToOne(optional = false)
@JoinColumn(name = "pullrequest_id", referencedColumnName = "id")
@ToString.Exclude
private PullRequest pullRequest;

public void addComment(ReviewComment comment) {
comments.add(comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import de.tum.in.www1.hephaestus.codereview.base.BaseGitServiceEntity;
import de.tum.in.www1.hephaestus.codereview.comment.IssueComment;
import de.tum.in.www1.hephaestus.codereview.comment.review.ReviewComment;
import de.tum.in.www1.hephaestus.codereview.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.codereview.pullrequest.review.PullRequestReview;
import jakarta.persistence.CascadeType;
Expand Down Expand Up @@ -60,13 +61,20 @@ public class User extends BaseGitServiceEntity {
private Set<PullRequest> pullRequests = new HashSet<>();

@OneToMany(cascade = CascadeType.ALL, mappedBy = "author")
private Set<IssueComment> comments = new HashSet<>();
private Set<IssueComment> issueComments = new HashSet<>();

@OneToMany(cascade = CascadeType.ALL, mappedBy = "author")
private Set<ReviewComment> reviewComments = new HashSet<>();

@OneToMany(cascade = CascadeType.ALL, mappedBy = "author")
private Set<PullRequestReview> reviews = new HashSet<>();

public void addComment(IssueComment comment) {
comments.add(comment);
public void addIssueComment(IssueComment comment) {
issueComments.add(comment);
}

public void addReviewComment(ReviewComment comment) {
reviewComments.add(comment);
}

public void addPullRequest(PullRequest pullRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import de.tum.in.www1.hephaestus.codereview.comment.IssueComment;
import de.tum.in.www1.hephaestus.codereview.comment.IssueCommentConverter;
import de.tum.in.www1.hephaestus.codereview.comment.IssueCommentRepository;
import de.tum.in.www1.hephaestus.codereview.comment.review.ReviewComment;
import de.tum.in.www1.hephaestus.codereview.comment.review.ReviewCommentConverter;
import de.tum.in.www1.hephaestus.codereview.comment.review.ReviewCommentRepository;
import de.tum.in.www1.hephaestus.codereview.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.codereview.pullrequest.PullRequestConverter;
import de.tum.in.www1.hephaestus.codereview.pullrequest.PullRequestRepository;
Expand All @@ -44,32 +47,37 @@ public class GitHubDataSyncService {
private final PullRequestRepository pullRequestRepository;
private final PullRequestReviewRepository prReviewRepository;
private final IssueCommentRepository commentRepository;
private final ReviewCommentRepository reviewCommentRepository;
private final UserRepository userRepository;

private final RepositoryConverter repositoryConverter;
private final PullRequestConverter pullRequestConverter;
private final PullRequestReviewConverter reviewConverter;
private final IssueCommentConverter commentConverter;
private final ReviewCommentConverter reviewCommentConverter;
private final UserConverter userConverter;

public GitHubDataSyncService(RepositoryRepository repositoryRepository, PullRequestRepository pullRequestRepository,
PullRequestReviewRepository prReviewRepository,
IssueCommentRepository commentRepository, UserRepository userRepository,
IssueCommentRepository commentRepository, ReviewCommentRepository reviewCommentRepository,
UserRepository userRepository,
RepositoryConverter repositoryConverter, PullRequestConverter pullRequestConverter,
PullRequestReviewConverter reviewConverter, IssueCommentConverter commentConverter,
UserConverter userConverter) {
ReviewCommentConverter reviewCommentConverter, UserConverter userConverter) {
logger.info("Hello from GitHubDataSyncService!");

this.repositoryRepository = repositoryRepository;
this.pullRequestRepository = pullRequestRepository;
this.prReviewRepository = prReviewRepository;
this.commentRepository = commentRepository;
this.reviewCommentRepository = reviewCommentRepository;
this.userRepository = userRepository;

this.repositoryConverter = repositoryConverter;
this.pullRequestConverter = pullRequestConverter;
this.reviewConverter = reviewConverter;
this.commentConverter = commentConverter;
this.reviewCommentConverter = reviewCommentConverter;
this.userConverter = userConverter;
}

Expand Down Expand Up @@ -166,6 +174,29 @@ private Set<PullRequest> getPullRequestsFromGHRepository(GHRepository ghRepo, Re
pullRequest.setReviews(new HashSet<>());
}

try {
pr.listReviewComments().toList().stream().forEach(comment -> {
ReviewComment c = reviewCommentConverter.convert(comment);
// First save the comment, so that it is referencable
reviewCommentRepository.save(c);

PullRequestReview review = getPullRequestReviewByReviewId(comment.getPullRequestReviewId());
c.setReview(review);
User commentAuthor;
try {
commentAuthor = getUserFromGHUser(comment.getUser());
commentAuthor.addReviewComment(c);
} catch (IOException e) {
logger.error("Error while fetching author!");
commentAuthor = null;
}
c.setAuthor(commentAuthor);
review.addComment(c);
});
} catch (IOException e) {
logger.error("Error while fetching PR review comments!");
}

return pullRequest;
}).collect(Collectors.toSet());
}
Expand All @@ -188,7 +219,7 @@ private Set<IssueComment> getCommentsFromGHPullRequest(GHPullRequest pr, PullReq
User commentAuthor;
try {
commentAuthor = getUserFromGHUser(comment.getUser());
commentAuthor.addComment(c);
commentAuthor.addIssueComment(c);
} catch (IOException e) {
logger.error("Error while fetching author!");
commentAuthor = null;
Expand All @@ -198,6 +229,10 @@ private Set<IssueComment> getCommentsFromGHPullRequest(GHPullRequest pr, PullReq
}).collect(Collectors.toSet());
}

private PullRequestReview getPullRequestReviewByReviewId(Long reviewId) {
return prReviewRepository.findById(reviewId).orElse(null);
}

private User getUserFromGHUser(org.kohsuke.github.GHUser user) {
User ghUser = userRepository.findUser(user.getLogin()).orElse(null);
if (ghUser == null) {
Expand Down

0 comments on commit fb5d518

Please sign in to comment.