Skip to content

Commit

Permalink
#144: add reviews to the activity service
Browse files Browse the repository at this point in the history
  • Loading branch information
iam-flo committed Nov 25, 2024
1 parent 79dbd72 commit bcc429d
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

@RestController
@RequestMapping("/activity")
public class ActivityDashboardController {
public class ActivityController {

@Autowired
private ActivityDashboardService activityDashboardService;
private ActivityService activityService;

@GetMapping("/{login}")
public ResponseEntity<ActivitiesDTO> getActivitiesByUser(@PathVariable String login) {
return ResponseEntity.ok(activityDashboardService.getActivities(login));
public ResponseEntity<ActivityDTO> getActivityByUser(@PathVariable String login) {
ActivityDTO activity = activityService.getActivity(login);
return ResponseEntity.ok(activity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import de.tum.in.www1.hephaestus.gitprovider.issue.IssueInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequestInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.pullrequestreview.PullRequestReviewInfoDTO;
import io.micrometer.common.lang.NonNull;

import java.util.List;

public record ActivitiesDTO(
public record ActivityDTO(
@NonNull List<PullRequestInfoDTO> pullRequests,
@NonNull List<IssueInfoDTO> issues,
@NonNull List<PullRequestReviewInfoDTO> reviews) {
@NonNull List<ReviewActivityDto> reviews) {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package de.tum.in.www1.hephaestus.activitydashboard;

import de.tum.in.www1.hephaestus.gitprovider.issue.Issue;
import de.tum.in.www1.hephaestus.gitprovider.issue.IssueInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.issue.IssueRepository;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequestInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequestRepository;
import de.tum.in.www1.hephaestus.gitprovider.pullrequestreview.PullRequestReview;
import de.tum.in.www1.hephaestus.gitprovider.pullrequestreview.PullRequestReviewInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.pullrequestreview.PullRequestReviewRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class ActivityService {

private PullRequestRepository pullRequestRepository;

private IssueRepository issueRepository;

private PullRequestReviewRepository pullRequestReviewRepository;

public ActivityService(PullRequestRepository pullRequestRepository, IssueRepository issueRepository, PullRequestReviewRepository pullRequestReviewRepository) {
this.pullRequestRepository = pullRequestRepository;
this.issueRepository = issueRepository;
this.pullRequestReviewRepository = pullRequestReviewRepository;
}

@Transactional
public ActivityDTO getActivity(String login) {

List<PullRequestInfoDTO> openPullRequests = pullRequestRepository
.findAssignedByLoginAndStates(login, Set.of(Issue.State.OPEN))
.stream()
.map(PullRequestInfoDTO::fromPullRequest)
.toList();

List<IssueInfoDTO> openIssues = issueRepository.findAssignedByLoginAndStates(login, Set.of(Issue.State.OPEN))
.stream()
.map(IssueInfoDTO::fromIssue)
.toList();

List<ReviewActivityDto> reviews = getReviewedOrRequestedPullRequests(login);

return new ActivityDTO(
openPullRequests,
openIssues,
reviews
);
}

private List<ReviewActivityDto> getReviewedOrRequestedPullRequests(String login) {
List<ReviewActivityDto> reviews = pullRequestReviewRepository.findAllOpenReviewsByAuthorLogin(login)
.stream()
.map(ReviewActivityDto::fromPullRequestReview)
.toList();

List<ReviewActivityDto> requestedReviews = pullRequestRepository.findReviewRequestedByLogin(login)
.stream()
.map(ReviewActivityDto::fromPullRequest)
.toList();

requestedReviews.addAll(reviews);
return requestedReviews;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.tum.in.www1.hephaestus.activitydashboard;

import java.time.OffsetDateTime;
import org.springframework.lang.NonNull;
import com.fasterxml.jackson.annotation.JsonInclude;

import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequestInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.pullrequestreview.PullRequestReview;
import de.tum.in.www1.hephaestus.gitprovider.user.UserInfoDTO;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public record ReviewActivityDto(
@NonNull Long id,
@NonNull Boolean isDismissed,
@NonNull ReviewActivityState state,
@NonNull Integer codeComments,
UserInfoDTO author,
PullRequestInfoDTO pullRequest) {

public static ReviewActivityDto fromPullRequestReview(PullRequestReview pullRequestReview) {
return new ReviewActivityDto(
pullRequestReview.getId(),
pullRequestReview.isDismissed(),
ReviewActivityDto.fromPullRequestReviewState(pullRequestReview.getState()),
pullRequestReview.getComments().size(),
UserInfoDTO.fromUser(pullRequestReview.getAuthor()),
PullRequestInfoDTO.fromPullRequest(pullRequestReview.getPullRequest())
);
}

public static ReviewActivityDto fromPullRequest(PullRequest pullRequest) {
return new ReviewActivityDto(
pullRequest.getId(),
false,
ReviewActivityState.REVIEW_REQUESTED,
0,
UserInfoDTO.fromUser(pullRequest.getAuthor()),
PullRequestInfoDTO.fromPullRequest(pullRequest)
);
}

public enum ReviewActivityState {
COMMENTED,
APPROVED,
CHANGES_REQUESTED,
REVIEW_REQUESTED,
UNKNOWN;
}

public static ReviewActivityState fromPullRequestReviewState(PullRequestReview.State state) {
return switch (state) {
case COMMENTED -> ReviewActivityState.COMMENTED;
case APPROVED -> ReviewActivityState.APPROVED;
case CHANGES_REQUESTED -> ReviewActivityState.CHANGES_REQUESTED;
default -> ReviewActivityState.UNKNOWN;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IssueRepository extends JpaRepository<Issue, Long> {
JOIN FETCH i.author
LEFT JOIN FETCH i.assignees
LEFT JOIN FETCH i.repository
WHERE (i.author.login ILIKE :assigneeLogin OR LOWER(:assigneeLogin) IN (SELECT LOWER(u.login) FROM i.assignees u)) AND i.state IN :states
WHERE LOWER(:assigneeLogin) IN (SELECT LOWER(u.login) FROM i.assignees u) AND i.state IN :states AND TYPE(i) <> PullRequest
ORDER BY i.createdAt DESC
""")
List<Issue> findAssignedByLoginAndStates(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ SELECT MIN(p.createdAt)
List<PullRequest> findAssignedByLoginAndStates(
@Param("assigneeLogin") String assigneeLogin,
@Param("states") Set<PullRequest.State> states);

@Query("""
SELECT p
FROM PullRequest p
LEFT JOIN FETCH p.labels
LEFT JOIN FETCH p.requestedReviewers
JOIN FETCH p.author
LEFT JOIN FETCH p.assignees
LEFT JOIN FETCH p.repository
WHERE LOWER(:reviewerLogin) IN (SELECT LOWER(u.login) FROM p.requestedReviewers u)
ORDER BY p.createdAt DESC
""")
List<PullRequest> findReviewRequestedByLogin(@Param("reviewerLogin") String reviewerLogin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@ List<PullRequestReview> findAllInTimeframe(
@Param("before") OffsetDateTime before,
@Param("repository") Optional<String> repository);

@Query("""
SELECT prr
FROM PullRequestReview prr
LEFT JOIN FETCH prr.author
LEFT JOIN FETCH prr.pullRequest
LEFT JOIN FETCH prr.pullRequest.repository
LEFT JOIN FETCH prr.comments
WHERE prr.author.login ILIKE :authorLogin AND prr.pullRequest.state = 'OPEN'
AND prr.submittedAt = (
SELECT MAX(subPrr.submittedAt)
FROM PullRequestReview subPrr
WHERE subPrr.pullRequest.id = prr.pullRequest.id
)
ORDER BY prr.submittedAt DESC
""")
List<PullRequestReview> findAllOpenReviewsByAuthorLogin(@Param("authorLogin") String authorLogin);

}

0 comments on commit bcc429d

Please sign in to comment.