Skip to content

Commit

Permalink
Add PR Number
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Sep 18, 2024
1 parent 6da0d83 commit 2707cc7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
@NoArgsConstructor
@ToString(callSuper = true)
public class PullRequest extends BaseGitServiceEntity {

private int number;

@NonNull
private String title;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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());
pullRequest.setState(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,15 @@ public List<LeaderboardEntry> createLeaderboard() {
if (user.getType() != UserType.USER) {
return null;
}
AtomicInteger comments = new AtomicInteger(
user.getIssueComments().size() + user.getReviewComments().size());
logger.info("User: " + user.getLogin());
AtomicInteger comments = new AtomicInteger(0);
AtomicInteger changesRequested = new AtomicInteger(0);
AtomicInteger changesApproved = new AtomicInteger(0);
AtomicInteger score = new AtomicInteger(0);
user.getReviews().stream()
.filter(review -> (review.getCreatedAt() != null && review.getCreatedAt().isAfter(cutOffTime))
|| (review.getUpdatedAt() != null && review.getUpdatedAt().isAfter(cutOffTime)))
.forEach(review -> {
if (user.getLogin().equals("SimonEntholzer")) {
logger.info("State: " + review.getState() + review.toString());
}
if (review.getPullRequest().getAuthor().getLogin().equals(user.getLogin())) {
return;
}
Expand All @@ -70,13 +67,11 @@ public List<LeaderboardEntry> createLeaderboard() {
break;
default:
comments.incrementAndGet();
logger.info("Commented Review: " + review.getPullRequest().getNumber());
break;
}
score.addAndGet(calculateScore(review.getPullRequest()));
});
logger.info("User " + user.getLogin() + " has " + user.getReviews().size() + " reviews and " + comments
+ " comments" + " and " + changesRequested + " changes requested and " + changesApproved
+ " approved");
return new LeaderboardEntry(user.getLogin(), user.getAvatarUrl(), user.getName(), user.getType(),
score.get(),
0, // preliminary rank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import de.tum.in.www1.hephaestus.codereview.comment.IssueComment;
import de.tum.in.www1.hephaestus.codereview.comment.IssueCommentConverter;
Expand Down Expand Up @@ -162,7 +161,7 @@ public Repository fetchRepository(String nameWithOwner) throws IOException {
repository = repositoryRepository.save(repository);

Set<PullRequest> prs = getPullRequestsFromGHRepository(ghRepo, repository);
logger.info("Found " + prs.size() + " PRs");
logger.info("Found total of " + prs.size() + " PRs");
repository.setPullRequests(prs);

pullRequestRepository.saveAll(prs);
Expand Down Expand Up @@ -195,22 +194,12 @@ private Set<PullRequest> getPullRequestsFromGHRepository(GHRepository ghRepo, Re
}
PullRequest pullRequest = pullRequestRepository.save(pullRequestConverter.convert(pr));
pullRequest.setRepository(repository);
try {
Collection<IssueComment> comments = getCommentsFromGHPullRequest(pr, pullRequest);
comments = commentRepository.saveAll(comments);
for (IssueComment c : comments) {
pullRequest.addComment(c);
}
} catch (IOException e) {
logger.error("Error while fetching PR comments!");
pullRequest.setComments(new HashSet<>());
}
try {
User prAuthor = getUserFromGHUser(pr.getUser());
prAuthor.addPullRequest(pullRequest);
pullRequest.setAuthor(prAuthor);
} catch (IOException e) {
// logger.error("Error while fetching PR author!");
// Dont mind this error as it occurs only for bots
pullRequest.setAuthor(null);
}

Expand All @@ -224,7 +213,6 @@ private Set<PullRequest> getPullRequestsFromGHRepository(GHRepository ghRepo, Re
prReview.setAuthor(reviewAuthor);
} catch (IOException e) {
// Dont mind this error as it occurs only for bots
// logger.error("Error while fetching review owner!");
}
prReview.setPullRequest(pullRequest);
return prReview;
Expand All @@ -233,10 +221,9 @@ private Set<PullRequest> getPullRequestsFromGHRepository(GHRepository ghRepo, Re
pullRequest.addReview(prReview);
reviews.add(prReview);
}
logger.info("Found " + newReviews.size() + " reviews for PR " + pullRequest.getId());
logger.info("Found " + newReviews.size() + " reviews for PR #" + pullRequest.getNumber());
} catch (IOException e) {
logger.error("Error while fetching PR reviews!");
pullRequest.setReviews(new HashSet<>());
}

try {
Expand All @@ -249,6 +236,16 @@ private Set<PullRequest> getPullRequestsFromGHRepository(GHRepository ghRepo, Re
logger.error("Error while fetching PR review comments!");
}

try {
Collection<IssueComment> comments = getCommentsFromGHPullRequest(pr, pullRequest);
// comments = commentRepository.saveAll(comments);
for (IssueComment c : comments) {
pullRequest.addComment(c);
}
} catch (IOException e) {
logger.error("Error while fetching PR comments!");
}

return pullRequest;
}).filter(Objects::nonNull).collect(Collectors.toSet()));
}
Expand All @@ -266,8 +263,6 @@ private PullRequestReviewComment handleSinglePullRequestReviewComment(GHPullRequ
commentAuthor.addReviewComment(prrc);
} catch (IOException e) {
// Dont mind this error as it occurs only for bots
// logger.error("Error while fetching author!" + comment.getPullRequestUrl() + "
// " + comment.getCreatedAt());
commentAuthor = null;
}
prrc.setAuthor(commentAuthor);
Expand All @@ -281,9 +276,8 @@ private PullRequestReviewComment handleSinglePullRequestReviewComment(GHPullRequ
* @param pr The GH pull request.
* @param pullRequest Stored PR to which the comments belong.
* @return The comments of the given pull request.
* @throws IOException
* @throws IOException when fetching the comments fails
*/
@Transactional
private Set<IssueComment> getCommentsFromGHPullRequest(GHPullRequest pr, PullRequest pullRequest)
throws IOException {
return pr.queryComments().list().withPageSize(100).toList().stream()
Expand All @@ -296,20 +290,27 @@ private Set<IssueComment> getCommentsFromGHPullRequest(GHPullRequest pr, PullReq
commentAuthor.addIssueComment(c);
} catch (IOException e) {
// Dont mind this error as it occurs only for bots
// logger.error("Error while fetching author!" + comment.getHtmlUrl() + " " +
// comment.getCreatedAt());
commentAuthor = null;
}
c.setAuthor(commentAuthor);
return c;
}).collect(Collectors.toSet());
}

/**
* Gets the corresponding User entity instance: cache -> database -> create new
* user
*
* @param user GHUser instance
* @return entity instance
*/
private User getUserFromGHUser(org.kohsuke.github.GHUser user) {
// Try to find user in cache
Optional<User> ghUser = users.stream().filter(u -> u.getLogin().equals(user.getLogin())).findFirst();
if (ghUser.isPresent()) {
return ghUser.get();
}
// Try to find user in database
ghUser = userRepository.findUserEagerly(user.getLogin());
if (ghUser.isPresent()) {
User u = ghUser.get();
Expand All @@ -318,12 +319,19 @@ private User getUserFromGHUser(org.kohsuke.github.GHUser user) {
}
return u;
}
logger.info("Creating user " + user.getLogin());
// Otherwise create new user
User u = userRepository.save(userConverter.convert(user));
users.add(u);
return u;
}

/**
* Gets the corresponding PullRequestReview from the cache.
*
* @implNote Assumes that it's executed after the review has been fetched from
* Github already
* @param reviewId
*/
private PullRequestReview getPRRFromReviewId(Long reviewId) {
Optional<PullRequestReview> prReview = reviews.stream().filter(prr -> prr.getId().equals(reviewId)).findFirst();
if (prReview.isPresent()) {
Expand All @@ -334,7 +342,7 @@ private PullRequestReview getPRRFromReviewId(Long reviewId) {
}

/**
* Checks if the resource has been created within the last week.
* Checks if the resource has been created within the timeframe.
*
* @param obj
* @return
Expand Down

0 comments on commit 2707cc7

Please sign in to comment.