Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficient Repository Updates instead of Refetching #103

Merged
merged 9 commits into from
Sep 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.kohsuke.github.GHObject;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.lang.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
Expand All @@ -16,6 +17,8 @@ public abstract class BaseGitServiceEntityConverter<S extends GHObject, T extend

private static final Logger logger = LoggerFactory.getLogger(BaseGitServiceEntityConverter.class);

abstract public T update(@NonNull S source, @NonNull T target);

protected void convertBaseFields(S source, T target) {
if (source == null || target == null) {
throw new IllegalArgumentException("Source and target must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@ToString(callSuper = true)
public abstract class Comment extends BaseGitServiceEntity {
@Column(columnDefinition = "TEXT")
@ToString.Exclude
protected String body;

@ManyToOne(fetch = FetchType.EAGER)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.tum.in.www1.hephaestus.codereview.comment;

import org.kohsuke.github.GHIssueComment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

Expand All @@ -9,9 +11,15 @@
@Component
public class IssueCommentConverter extends BaseGitServiceEntityConverter<GHIssueComment, IssueComment> {

protected static final Logger logger = LoggerFactory.getLogger(IssueCommentConverter.class);

@Override
public IssueComment convert(@NonNull GHIssueComment source) {
IssueComment comment = new IssueComment();
return update(source, new IssueComment());
}

@Override
public IssueComment update(@NonNull GHIssueComment source, @NonNull IssueComment comment) {
GODrums marked this conversation as resolved.
Show resolved Hide resolved
convertBaseFields(source, comment);
comment.setBody(source.getBody());
return comment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.tum.in.www1.hephaestus.codereview.comment.review;

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

Expand All @@ -10,9 +12,16 @@
public class PullRequestReviewCommentConverter
extends BaseGitServiceEntityConverter<GHPullRequestReviewComment, PullRequestReviewComment> {

protected static final Logger logger = LoggerFactory.getLogger(PullRequestReviewCommentConverter.class);

@Override
public PullRequestReviewComment convert(@NonNull GHPullRequestReviewComment source) {
PullRequestReviewComment comment = new PullRequestReviewComment();
return update(source, new PullRequestReviewComment());
}

@Override
public PullRequestReviewComment update(@NonNull GHPullRequestReviewComment source,
@NonNull PullRequestReviewComment comment) {
convertBaseFields(source, comment);
comment.setBody(source.getBody());
comment.setCommit(source.getCommitId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class PullRequest extends BaseGitServiceEntity {
@ToString.Exclude
private Set<IssueComment> comments = new HashSet<>();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ public class PullRequestConverter extends BaseGitServiceEntityConverter<GHPullRe

@Override
public PullRequest convert(@NonNull GHPullRequest source) {
IssueState state = convertState(source.getState());
PullRequest pullRequest = new PullRequest();
convertBaseFields(source, pullRequest);
pullRequest.setNumber(source.getNumber());
pullRequest.setTitle(source.getTitle());
pullRequest.setUrl(source.getHtmlUrl().toString());
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved
pullRequest.setState(state);
return update(source, pullRequest);
}

@Override
public PullRequest update(@NonNull GHPullRequest source, @NonNull PullRequest pullRequest) {
convertBaseFields(source, pullRequest);
pullRequest.setTitle(source.getTitle());
pullRequest.setState(convertState(source.getState()));
pullRequest.setPullRequestLabels(convertLabels(source.getLabels()));

try {
pullRequest.setAdditions(source.getAdditions());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package de.tum.in.www1.hephaestus.codereview.pullrequest;

import java.util.Optional;
import java.util.Set;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface PullRequestRepository extends JpaRepository<PullRequest, Long> {

Set<PullRequest> findByAuthor_Login(String authorLogin);

@Query("""
SELECT p
FROM PullRequest p
JOIN FETCH p.comments
JOIN FETCH p.reviews
WHERE p.id = :id
""")
Optional<PullRequest> findByIdWithEagerRelations(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class PullRequestReview extends BaseGitServiceEntity {

private OffsetDateTime submittedAt;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public class PullRequestReviewConverter extends BaseGitServiceEntityConverter<GH

@Override
public PullRequestReview convert(@NonNull GHPullRequestReview source) {
PullRequestReview review = new PullRequestReview();
return update(source, new PullRequestReview());
}

@Override
public PullRequestReview update(@NonNull GHPullRequestReview source, @NonNull PullRequestReview review) {
convertBaseFields(source, review);
review.setState(convertState(source.getState()));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public class Repository extends BaseGitServiceEntity {

String homepage;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "repository")
@OneToMany(cascade = CascadeType.REFRESH, mappedBy = "repository")
@ToString.Exclude
private Set<PullRequest> pullRequests = new HashSet<>();

public void addPullRequest(PullRequest pullRequest) {
pullRequests.add(pullRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@

import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHRepository.Visibility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

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

@Component
public class RepositoryConverter extends BaseGitServiceEntityConverter<GHRepository, Repository> {

protected static final Logger logger = LoggerFactory.getLogger(RepositoryConverter.class);

@Override
@Nullable
public Repository convert(@NonNull GHRepository source) {
Repository repository = new Repository();
return update(source, new Repository());
}

@Override
public Repository update(@NonNull GHRepository source, @NonNull Repository repository) {
convertBaseFields(source, repository);
repository.setName(source.getName());
repository.setNameWithOwner(source.getFullName());
repository.setDescription(source.getDescription());
repository.setUrl(source.getHtmlUrl().toString());
repository.setDescription(source.getDescription());
repository.setDefaultBranch(source.getDefaultBranch());
repository.setVisibility(convertVisibility(source.getVisibility()));
repository.setHomepage(source.getHomepage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;

import org.kohsuke.github.GHUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
Expand All @@ -15,21 +16,25 @@ public class UserConverter extends BaseGitServiceEntityConverter<org.kohsuke.git
protected static final Logger logger = LoggerFactory.getLogger(UserConverter.class);

@Override
public User convert(@NonNull org.kohsuke.github.GHUser source) {
User user = new User();
public User convert(@NonNull GHUser source) {
return update(source, new User());
}

@Override
public User update(@NonNull GHUser source, @NonNull User user) {
convertBaseFields(source, user);
user.setLogin(source.getLogin());
user.setUrl(source.getHtmlUrl().toString());
user.setAvatarUrl(source.getAvatarUrl());
try {
user.setName(source.getName());
user.setEmail(source.getEmail());
} catch (IOException e) {
logger.error("Failed to convert user name field for source {}: {}", source.getId(), e.getMessage());
logger.error("Failed to convert user email field for source {}: {}", source.getId(), e.getMessage());
}
try {
user.setEmail(source.getEmail());
user.setName(source.getName());
} catch (IOException e) {
logger.error("Failed to convert user email field for source {}: {}", source.getId(), e.getMessage());
logger.error("Failed to convert user name field for source {}: {}", source.getId(), e.getMessage());
}
try {
user.setType(convertUserType(source.getType()));
Expand Down
Loading