Skip to content

Commit

Permalink
create rule endpoints(#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
iam-flo committed Jan 2, 2025
1 parent 3c67209 commit b3b646c
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import de.tum.in.www1.hephaestus.activity.model.ActivityDTO;
import de.tum.in.www1.hephaestus.activity.model.PullRequestBadPracticeDTO;
import java.util.List;

import de.tum.in.www1.hephaestus.activity.model.PullRequestBadPracticeRuleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -25,4 +27,16 @@ public ResponseEntity<List<PullRequestBadPracticeDTO>> detectBadPracticesByUser(
List<PullRequestBadPracticeDTO> badPractices = activityService.detectBadPractices(login);
return ResponseEntity.ok(badPractices);
}

@GetMapping("/rules/{repository")
public ResponseEntity<List<PullRequestBadPracticeRuleDTO>> getRulesByRepository(@PathVariable String repository) {
List<PullRequestBadPracticeRuleDTO> rules = activityService.getRules(repository);
return ResponseEntity.ok(rules);
}

@PostMapping("/rules")
public ResponseEntity<PullRequestBadPracticeRuleDTO> updateOrCreateRule(@RequestBody PullRequestBadPracticeRuleDTO rule) {
PullRequestBadPracticeRuleDTO createdRule = activityService.createOrUpdateRule(rule);
return ResponseEntity.ok(createdRule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 de.tum.in.www1.hephaestus.gitprovider.repository.RepositoryRepository;
import de.tum.in.www1.hephaestus.gitprovider.user.User;
import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -35,6 +36,12 @@ public class ActivityService {
@Autowired
private UserRepository userRepository;

@Autowired
private PullRequestBadPracticeRuleRepository pullRequestBadPracticeRuleRepository;

@Autowired
private RepositoryRepository repositoryRepository;

@Transactional
public ActivityDTO getActivity(String login) {
logger.info("Getting activity for user with login: {}", login);
Expand All @@ -55,10 +62,8 @@ public ActivityDTO getActivity(String login) {
Collectors.collectingAndThen(Collectors.toList(), list ->
list
.stream()
.map(PullRequestBadPractice::getType)
.distinct()
.map(PullRequestBadPracticeDTO::fromPullRequestBadPracticeType)
.collect(Collectors.toList())
.map(PullRequestBadPracticeDTO::fromPullRequestBadPractice)
.toList()
)
)
);
Expand All @@ -71,9 +76,7 @@ public ActivityDTO getActivity(String login) {
pullRequestBadPracticesMap.getOrDefault(
pullRequest,
List.of(
PullRequestBadPracticeDTO.fromPullRequestBadPracticeType(
PullRequestBadPracticeType.UNCHECKED_CHECKBOX
)
new PullRequestBadPracticeDTO("Unchecked checkbox.", "The checkbox is not checked.", false)
)
)
)
Expand Down Expand Up @@ -105,4 +108,32 @@ public List<PullRequestBadPracticeDTO> detectBadPractices(String login) {
.map(PullRequestBadPracticeDTO::fromPullRequestBadPractice)
.collect(Collectors.toList());
}

@Transactional
public List<PullRequestBadPracticeRuleDTO> getRules(String repository) {
logger.info("Getting rules for repository: {}", repository);

return pullRequestBadPracticeRuleRepository.findByRepositoryName(repository)
.stream()
.map(PullRequestBadPracticeRuleDTO::fromPullRequestBadPracticeRule)
.toList();
}

@Transactional
public PullRequestBadPracticeRuleDTO createOrUpdateRule(PullRequestBadPracticeRuleDTO rule) {
logger.info("Creating rule: {}", rule);

PullRequestBadPracticeRule existingOrNewRule = pullRequestBadPracticeRuleRepository.findById(rule.id()).orElse(null);
if (existingOrNewRule == null) {
existingOrNewRule = new PullRequestBadPracticeRule();
repositoryRepository.findByNameWithOwner(rule.repository().nameWithOwner()).ifPresent(existingOrNewRule::setRepository);
}
existingOrNewRule.setTitle(rule.title());
existingOrNewRule.setDescription(rule.description());
existingOrNewRule.setConditions(rule.conditions());
existingOrNewRule.setActive(rule.active());

return PullRequestBadPracticeRuleDTO.fromPullRequestBadPracticeRule(
pullRequestBadPracticeRuleRepository.save(existingOrNewRule));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.tum.in.www1.hephaestus.activity;

import de.tum.in.www1.hephaestus.activity.model.PullRequestBadPracticeRule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PullRequestBadPracticeRuleRepository extends JpaRepository<PullRequestBadPracticeRule, Long> {

@Query(
"""
SELECT prbp
FROM PullRequestBadPracticeRule prbp
WHERE prbp.repository.name = :repositoryName
"""
)
List<PullRequestBadPracticeRule> findByRepositoryName(String repositoryName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import de.tum.in.www1.hephaestus.activity.PullRequestBadPracticeRepository;
import de.tum.in.www1.hephaestus.activity.model.PullRequestBadPractice;
import de.tum.in.www1.hephaestus.activity.model.PullRequestBadPracticeType;
import de.tum.in.www1.hephaestus.gitprovider.pullrequest.PullRequest;
import java.util.List;
import org.slf4j.Logger;
Expand All @@ -20,17 +19,20 @@ public class PullRequestBadPracticeDetector {

public List<PullRequestBadPractice> detectAndSyncBadPractices(PullRequest pullRequest) {
logger.info(
"Detecting bad practices for pull request: {}.",
pullRequest.getId()
"Detecting bad practices for pull request: {}.",
pullRequest.getId()
);

List<PullRequestBadPractice> existingBadPractices = pullRequestBadPracticeRepository.findByPullRequestId(
pullRequest.getId()
pullRequest.getId()
);

//TODO connect LLM based detection
List<PullRequestBadPractice> newBadPractices = List.of();

return newBadPractices;
}
/*
updateExistingBadPractices(existingBadPractices, newBadPractices);
return saveBadPractices(existingBadPractices, newBadPractices);
Expand All @@ -40,14 +42,9 @@ private List<PullRequestBadPractice> saveBadPractices(
List<PullRequestBadPractice> existingBadPractices,
List<PullRequestBadPractice> detectedBadPractices
) {
List<PullRequestBadPracticeType> existingTypes = existingBadPractices
.stream()
.map(PullRequestBadPractice::getType)
.toList();

List<PullRequestBadPractice> newBadPractices = detectedBadPractices
.stream()
.filter(badPractice -> !existingTypes.contains(badPractice.getType()))
.filter(badPractice -> !existingBadPractices.contains(badPractice))
.toList();
return pullRequestBadPracticeRepository.saveAll(newBadPractices);
Expand All @@ -69,5 +66,5 @@ private void updateExistingBadPractices(
pullRequestBadPracticeRepository.save(existingBadPractice);
}
}
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class PullRequestBadPractice {
@Id
private Long id;

private PullRequestBadPracticeType type;
@ManyToOne
@JoinColumn(name = "rule_id")
private PullRequestBadPracticeRule rule;

@ManyToOne
@JoinColumn(name = "pullrequest_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package de.tum.in.www1.hephaestus.activity.model;

public record PullRequestBadPracticeDTO(String title, String description) {
public static PullRequestBadPracticeDTO fromPullRequestBadPracticeType(PullRequestBadPracticeType type) {
return new PullRequestBadPracticeDTO(type.title, type.description);
}
public record PullRequestBadPracticeDTO(String title, String description, boolean resolved) {

public static PullRequestBadPracticeDTO fromPullRequestBadPractice(PullRequestBadPractice badPractice) {
return new PullRequestBadPracticeDTO(badPractice.getType().title, badPractice.getType().description);
return new PullRequestBadPracticeDTO(badPractice.getRule().getTitle(), badPractice.getRule().getDescription(), badPractice.isResolved());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.tum.in.www1.hephaestus.activity.model;

import de.tum.in.www1.hephaestus.gitprovider.repository.Repository;
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
@Setter
@Getter
@NoArgsConstructor
@ToString
public class PullRequestBadPracticeRule {

@Id
private Long id;

private String title;

private String description;

private String conditions;

@ManyToOne
@JoinColumn(name = "repository_id")
private Repository repository;

private boolean active;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.tum.in.www1.hephaestus.activity.model;


import de.tum.in.www1.hephaestus.gitprovider.repository.RepositoryInfoDTO;

public record PullRequestBadPracticeRuleDTO(
Long id,
String title,
String description,
String conditions,
RepositoryInfoDTO repository,
boolean active
) {
public static PullRequestBadPracticeRuleDTO fromPullRequestBadPracticeRule(PullRequestBadPracticeRule rule) {
return new PullRequestBadPracticeRuleDTO(
rule.getId(),
rule.getTitle(),
rule.getDescription(),
rule.getConditions(),
RepositoryInfoDTO.fromRepository(rule.getRepository()),
rule.isActive()
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

import java.time.OffsetDateTime;
import java.util.HashSet;
import java.util.Set;
import lombok.Getter;
Expand Down Expand Up @@ -102,6 +104,8 @@ public class User extends BaseGitServiceEntity {
// Current ranking points for the leaderboard leagues
private int leaguePoints;

private OffsetDateTime lastSyncBadPracticeAt;

public enum Type {
USER,
ORGANIZATION,
Expand Down

0 comments on commit b3b646c

Please sign in to comment.