Skip to content

Commit

Permalink
Merge branch 'develop' into feature/table-component
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixTJDietrich authored Sep 13, 2024
2 parents 91df287 + 2f28091 commit 78eb706
Show file tree
Hide file tree
Showing 27 changed files with 628 additions and 111 deletions.
54 changes: 54 additions & 0 deletions server/application-server/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/UserDTO"
/user/{login}/full:
get:
tags:
- user
operationId: getFullUser
parameters:
- name: login
in: path
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/User"
/pullrequest/{id}:
get:
tags:
Expand Down Expand Up @@ -101,6 +119,20 @@ paths:
type: array
items:
$ref: "#/components/schemas/PullRequest"
/leaderboard:
get:
tags:
- leaderboard
operationId: getLeaderboard
responses:
"200":
description: OK
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/LeaderboardEntry"
/admin:
get:
tags:
Expand Down Expand Up @@ -421,3 +453,25 @@ components:
type: array
items:
$ref: "#/components/schemas/PullRequestReview"
LeaderboardEntry:
type: object
properties:
githubName:
type: string
name:
type: string
score:
type: integer
format: int32
total:
type: integer
format: int32
changesRequested:
type: integer
format: int32
approvals:
type: integer
format: int32
comments:
type: integer
format: int32
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package de.tum.in.www1.hephaestus.codereview.base;

import de.tum.in.www1.hephaestus.codereview.user.User;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
Expand All @@ -20,11 +19,10 @@
@AllArgsConstructor
@ToString(callSuper = true)
public abstract class Comment extends BaseGitServiceEntity {
@Lob
@Basic(fetch = FetchType.EAGER)
@Column(columnDefinition = "TEXT")
protected String body;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "author_id")
@ToString.Exclude
protected User author;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@NoArgsConstructor
@ToString(callSuper = true)
public class IssueComment extends Comment {
@ManyToOne(optional = false)
@ManyToOne
@JoinColumn(name = "pullrequest_id", referencedColumnName = "id")
@ToString.Exclude
private PullRequest pullRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
Expand All @@ -20,7 +21,7 @@
@NoArgsConstructor
@ToString(callSuper = true)
public class PullRequestReviewComment extends Comment {
@ManyToOne(optional = false)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "review_id", referencedColumnName = "id")
@ToString.Exclude
private PullRequestReview review;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class PullRequest extends BaseGitServiceEntity {

private OffsetDateTime mergedAt;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "author_id")
@ToString.Exclude
private User author;
Expand All @@ -58,7 +58,7 @@ public class PullRequest extends BaseGitServiceEntity {
@ToString.Exclude
private Set<PullRequestReview> reviews = new HashSet<>();

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne
@JoinColumn(name = "repository_id", referencedColumnName = "id")
@ToString.Exclude
private Repository repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@ToString(callSuper = true)
public class PullRequestReview extends BaseGitServiceEntity {

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "author_id")
@ToString.Exclude
private User author;
Expand All @@ -44,7 +44,7 @@ public class PullRequestReview extends BaseGitServiceEntity {
@ToString.Exclude
private Set<PullRequestReviewComment> comments = new HashSet<>();

@ManyToOne(optional = false)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "pullrequest_id", referencedColumnName = "id")
@ToString.Exclude
private PullRequest pullRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;

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

@Repository
Expand All @@ -11,4 +12,12 @@ public interface PullRequestReviewRepository extends JpaRepository<PullRequestRe
Optional<PullRequestReview> findByAuthor_Login(String authorLogin);

Optional<PullRequestReview> findByPullRequest(PullRequestReview pullRequest);

@Query("""
SELECT pr
FROM PullRequestReview pr
JOIN FETCH pr.comments
WHERE pr.id = :reviewId
""")
Optional<PullRequestReview> findByIdWithEagerComments(Long reviewId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.micrometer.common.lang.Nullable;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Getter;
Expand Down Expand Up @@ -45,7 +44,7 @@ public class Repository extends BaseGitServiceEntity {

String homepage;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "repository", fetch = FetchType.EAGER)
@OneToMany(cascade = CascadeType.ALL, mappedBy = "repository")
@ToString.Exclude
private Set<PullRequest> pullRequests = new HashSet<>();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package de.tum.in.www1.hephaestus.codereview.repository;

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

@org.springframework.stereotype.Repository
public interface RepositoryRepository
extends JpaRepository<Repository, Long> {

Repository findByNameWithOwner(String nameWithOwner);

@Query("""
SELECT r
FROM Repository r
JOIN FETCH r.pullRequests
WHERE r.nameWithOwner = :nameWithOwner
""")
Repository findByNameWithOwnerWithEagerPullRequests(String nameWithOwner);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public ResponseEntity<UserDTO> getUser(@PathVariable String login) {
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@GetMapping("/{login}/full")
public ResponseEntity<User> getFullUser(@PathVariable String login) {
Optional<User> user = userService.getUser(login);
System.out.println(user);
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public User convert(@NonNull org.kohsuke.github.GHUser source) {
user.setAvatarUrl(source.getAvatarUrl());
try {
user.setName(source.getName());
} catch (IOException e) {
logger.error("Failed to convert user name field for source {}: {}", source.getId(), e.getMessage());
}
try {
user.setEmail(source.getEmail());
} catch (IOException e) {
logger.error("Failed to convert user fields for source {}: {}", source.getId(), e.getMessage());
logger.error("Failed to convert user email field for source {}: {}", source.getId(), e.getMessage());
}
return user;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.in.www1.hephaestus.codereview.user;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -11,10 +12,31 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.login = :login")
Optional<User> findUser(@Param("login") String login);

@Query("""
SELECT u
FROM User u
JOIN FETCH u.pullRequests
JOIN FETCH u.issueComments
JOIN FETCH u.reviewComments
JOIN FETCH u.reviews
WHERE u.login = :login
""")
Optional<User> findUserEagerly(@Param("login") String login);

@Query("""
SELECT new UserDTO(u.id, u.login, u.email, u.name, u.url)
FROM User u
WHERE u.login = :login
""")
Optional<UserDTO> findByLogin(@Param("login") String login);

@Query("""
SELECT u
FROM User u
JOIN FETCH u.pullRequests
JOIN FETCH u.issueComments
JOIN FETCH u.reviewComments
JOIN FETCH u.reviews
""")
List<User> findAllWithRelations();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.in.www1.hephaestus.codereview.user;

import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
Expand All @@ -25,4 +26,9 @@ public Optional<UserDTO> getUserDTO(String login) {
logger.info("Getting userDTO with login: " + login);
return userRepository.findByLogin(login);
}

public List<User> getAllUsers() {
logger.info("Getting all users");
return userRepository.findAll().stream().toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.tum.in.www1.hephaestus.leaderboard;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/leaderboard")
public class LeaderboardController {

private final LeaderboardService leaderboardService;

public LeaderboardController(LeaderboardService leaderboardService) {
this.leaderboardService = leaderboardService;
}

@GetMapping
public ResponseEntity<List<LeaderboardEntry>> getLeaderboard() {
return ResponseEntity.ok(leaderboardService.createLeaderboard());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.tum.in.www1.hephaestus.leaderboard;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public record LeaderboardEntry(String githubName, String name, int score, int total, int changesRequested,
int approvals, int comments) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.tum.in.www1.hephaestus.leaderboard;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import de.tum.in.www1.hephaestus.codereview.user.User;
import de.tum.in.www1.hephaestus.codereview.user.UserService;

@Service
public class LeaderboardService {
private static final Logger logger = LoggerFactory.getLogger(LeaderboardService.class);

private final UserService userService;

public LeaderboardService(UserService userService) {
this.userService = userService;
}

public List<LeaderboardEntry> createLeaderboard() {
logger.info("Creating leaderboard dataset");

List<User> users = userService.getAllUsers();
logger.info("Found " + users.size() + " users");

List<LeaderboardEntry> leaderboard = users.stream().map(user -> {
logger.info("Creating leaderboard entry for user: " + user.getLogin());
int comments = user.getIssueComments().size();
AtomicInteger changesRequested = new AtomicInteger(0);
AtomicInteger changesApproved = new AtomicInteger(0);
user.getReviews().stream().forEach(review -> {
switch (review.getState()) {
case CHANGES_REQUESTED:
changesRequested.incrementAndGet();
break;
case APPROVED:
changesApproved.incrementAndGet();
break;
default:
break;
}
});
return new LeaderboardEntry(user.getLogin(), user.getName(), 0, 0, changesRequested.get(),
changesApproved.get(), comments);
}).toList();

return leaderboard;
}
}
Loading

0 comments on commit 78eb706

Please sign in to comment.