Skip to content

Commit

Permalink
create activity endpoint returning pull requests with bad practices(#199
Browse files Browse the repository at this point in the history
)
  • Loading branch information
iam-flo committed Dec 2, 2024
1 parent 6f8ff92 commit 9bff755
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.tum.in.www1.hephaestus.activity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Autowired
private ActivityService activityService;

@GetMapping("/{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
@@ -0,0 +1,9 @@
package de.tum.in.www1.hephaestus.activity;

import io.micrometer.common.lang.NonNull;

import java.util.List;

public record ActivityDTO(
@NonNull List<PullRequestWithBadPracticesDTO> pullRequests) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.tum.in.www1.hephaestus.activity;

import de.tum.in.www1.hephaestus.gitprovider.issue.Issue;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequestRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ActivityService {

@Autowired
private PullRequestRepository pullRequestRepository;

@Autowired
private PullRequestBadPracticeRepository pullRequestBadPracticeRepository;

@Transactional
public ActivityDTO getActivity(String login) {

List<PullRequest> pullRequests = pullRequestRepository
.findAssignedByLoginAndStates(login, Set.of(Issue.State.OPEN));

List<PullRequestBadPractice> openPulLRequestBadPractices = pullRequestBadPracticeRepository.findByLogin(login);

Map<PullRequest, List<PullRequestBadPracticeDTO>> pullRequestBadPracticesMap = openPulLRequestBadPractices.stream()
.collect(Collectors.groupingBy(
PullRequestBadPractice::getPullrequest,
Collectors.collectingAndThen(
Collectors.toList(),
list -> list.stream()
.map(PullRequestBadPractice::getType)
.distinct()
.map(PullRequestBadPracticeDTO::fromPullRequestBadPracticeType)
.collect(Collectors.toList())
)
));

List<PullRequestWithBadPracticesDTO> openPullRequestsWithBadPractices = pullRequests.stream()
.map(pullRequest -> PullRequestWithBadPracticesDTO.fromPullRequest(pullRequest,
pullRequestBadPracticesMap.getOrDefault(pullRequest, List.of()))
)
.collect(Collectors.toList());

return new ActivityDTO(openPullRequestsWithBadPractices);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.tum.in.www1.hephaestus.activity;

import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequest;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
public class PullRequestBadPractice {

@Id
private Long id;

private PullRequestBadPracticeType type;

@ManyToOne
@JoinColumn(name = "pullrequest_id")
private PullRequest pullrequest;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.tum.in.www1.hephaestus.activity;

public record PullRequestBadPracticeDTO(String title, String description) {

public static PullRequestBadPracticeDTO fromPullRequestBadPracticeType(PullRequestBadPracticeType type) {
return new PullRequestBadPracticeDTO(type.title, type.description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.tum.in.www1.hephaestus.activity;

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

import java.util.List;

@Repository
public interface PullRequestBadPracticeRepository extends JpaRepository<PullRequestBadPractice, Long> {

@Query("""
SELECT prbp
FROM PullRequestBadPractice prbp
WHERE LOWER(:assigneeLogin) IN (SELECT LOWER(u.login) FROM prbp.pullrequest.assignees u) AND prbp.pullrequest.state = 'OPEN'
""")
List<PullRequestBadPractice> findByLogin(@Param("assigneeLogin") String assigneeLogin);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.tum.in.www1.hephaestus.activity;

public enum PullRequestBadPracticeType {

EMPTY_DESCRIPTION("Empty Description", "The description of your pull request is empty."),
EMPTY_DESCRIPTION_SECTION("Empty Description Section", "The description of your pull request contains an empty section."),
UNCHECKED_CHECKBOX("Unchecked Checkbox", "A checkbox in the description of your pull request is unchecked.");

public final String title;

public final String description;

PullRequestBadPracticeType(String title, String description) {
this.title = title;
this.description = description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.tum.in.www1.hephaestus.activity;

import de.tum.in.www1.hephaestus.gitprovider.issue.Issue;
import de.tum.in.www1.hephaestus.gitprovider.label.LabelInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequest;
import de.tum.in.www1.hephaestus.gitprovider.repository.RepositoryInfoDTO;
import org.springframework.lang.NonNull;

import java.time.OffsetDateTime;
import java.util.Comparator;
import java.util.List;

public record PullRequestWithBadPracticesDTO(
@NonNull Long id,
@NonNull Integer number,
@NonNull String title,
@NonNull Issue.State state,
@NonNull Boolean isDraft,
@NonNull Boolean isMerged,
List<LabelInfoDTO> labels,
RepositoryInfoDTO repository,
@NonNull Integer additions,
@NonNull Integer deletions,
@NonNull String htmlUrl,
OffsetDateTime createdAt,
List<PullRequestBadPracticeDTO> badPractices
) {

public static PullRequestWithBadPracticesDTO fromPullRequest(PullRequest pullRequest, List<PullRequestBadPracticeDTO> badPractices) {
return new PullRequestWithBadPracticesDTO(
pullRequest.getId(),
pullRequest.getNumber(),
pullRequest.getTitle(),
pullRequest.getState(),
pullRequest.isDraft(),
pullRequest.isMerged(),
pullRequest.getLabels()
.stream()
.map(LabelInfoDTO::fromLabel)
.sorted(Comparator.comparing(LabelInfoDTO::name))
.toList(),
RepositoryInfoDTO.fromRepository(pullRequest.getRepository()),
pullRequest.getAdditions(),
pullRequest.getDeletions(),
pullRequest.getHtmlUrl(),
pullRequest.getCreatedAt(),
badPractices
);
}
}

0 comments on commit 9bff755

Please sign in to comment.