From fb5d5187633193f41ee9c17de5255683d8ca4a4b Mon Sep 17 00:00:00 2001 From: GODrums Date: Fri, 6 Sep 2024 04:38:32 +0200 Subject: [PATCH] Add ReviewComments and refactor IssueComments --- .../hephaestus/codereview/base/Comment.java | 31 ++++++++++++++ .../codereview/comment/IssueComment.java | 21 +--------- .../comment/review/ReviewComment.java | 30 ++++++++++++++ .../review/ReviewCommentConverter.java | 21 ++++++++++ .../review/ReviewCommentRepository.java | 7 ++++ .../codereview/pullrequest/PullRequest.java | 4 +- .../pullrequest/review/PullRequestReview.java | 13 ++++++ .../www1/hephaestus/codereview/user/User.java | 14 +++++-- .../scheduler/GitHubDataSyncService.java | 41 +++++++++++++++++-- 9 files changed, 155 insertions(+), 27 deletions(-) create mode 100644 server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/base/Comment.java create mode 100644 server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewComment.java create mode 100644 server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentConverter.java create mode 100644 server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentRepository.java diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/base/Comment.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/base/Comment.java new file mode 100644 index 00000000..d0b8ce46 --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/base/Comment.java @@ -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; +} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/IssueComment.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/IssueComment.java index fa89fa43..32d7adfe 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/IssueComment.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/IssueComment.java @@ -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; @@ -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 diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewComment.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewComment.java new file mode 100644 index 00000000..487c4eae --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewComment.java @@ -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; +} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentConverter.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentConverter.java new file mode 100644 index 00000000..68c0a651 --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentConverter.java @@ -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 { + + @Override + public ReviewComment convert(@NonNull GHPullRequestReviewComment source) { + ReviewComment comment = new ReviewComment(); + convertBaseFields(source, comment); + comment.setBody(source.getBody()); + comment.setCommit(source.getCommitId()); + return comment; + } + +} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentRepository.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentRepository.java new file mode 100644 index 00000000..012a5b75 --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/review/ReviewCommentRepository.java @@ -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 { + +} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/PullRequest.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/PullRequest.java index 35871ed1..114b8073 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/PullRequest.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/PullRequest.java @@ -52,11 +52,11 @@ public class PullRequest extends BaseGitServiceEntity { @OneToMany(cascade = CascadeType.ALL, mappedBy = "pullRequest") @ToString.Exclude - private Set comments = new HashSet<>();; + private Set comments = new HashSet<>(); @OneToMany(cascade = CascadeType.ALL, mappedBy = "pullRequest") @ToString.Exclude - private Set reviews = new HashSet<>();; + private Set reviews = new HashSet<>(); @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "repository_id", referencedColumnName = "id") diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/review/PullRequestReview.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/review/PullRequestReview.java index fe757591..3a9b3253 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/review/PullRequestReview.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/review/PullRequestReview.java @@ -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; @@ -39,8 +44,16 @@ public class PullRequestReview extends BaseGitServiceEntity { private OffsetDateTime updatedAt; + @OneToMany(cascade = CascadeType.ALL, mappedBy = "review") + @ToString.Exclude + private Set 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); + } } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/user/User.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/user/User.java index 6f08e353..7ba4b3b0 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/user/User.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/user/User.java @@ -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; @@ -60,13 +61,20 @@ public class User extends BaseGitServiceEntity { private Set pullRequests = new HashSet<>(); @OneToMany(cascade = CascadeType.ALL, mappedBy = "author") - private Set comments = new HashSet<>(); + private Set issueComments = new HashSet<>(); + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "author") + private Set reviewComments = new HashSet<>(); @OneToMany(cascade = CascadeType.ALL, mappedBy = "author") private Set 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) { diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/scheduler/GitHubDataSyncService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/scheduler/GitHubDataSyncService.java index 6fa13b69..4bfd62ae 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/scheduler/GitHubDataSyncService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/scheduler/GitHubDataSyncService.java @@ -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; @@ -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; } @@ -166,6 +174,29 @@ private Set 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()); } @@ -188,7 +219,7 @@ private Set 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; @@ -198,6 +229,10 @@ private Set 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) {