Skip to content

Commit

Permalink
create individual endpoints for detection and integrate existing bad …
Browse files Browse the repository at this point in the history
…practices #239
  • Loading branch information
iam-flo committed Feb 7, 2025
1 parent 5b25cbf commit 61c19da
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public ResponseEntity<List<PullRequestBadPracticeDTO>> detectBadPracticesByUser(
List<PullRequestBadPracticeDTO> badPractices = activityService.detectBadPractices(login);
return ResponseEntity.ok(badPractices);
}

@PostMapping("{prId}/badpractices/")
public ResponseEntity<List<PullRequestBadPracticeDTO>> detectBadPracticesByUserAndPr(@PathVariable Long prId) {
List<PullRequestBadPracticeDTO> badPractice = activityService.detectBadPractices(prId);
return ResponseEntity.ok(badPractice);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,20 @@ public List<PullRequestBadPracticeDTO> detectBadPractices(String login) {
}
return detectedBadPractices.stream().map(PullRequestBadPracticeDTO::fromPullRequestBadPractice).toList();
}

public List<PullRequestBadPracticeDTO> detectBadPractices(Long prId) {
logger.info("Detecting bad practices for PR: {}", prId);

PullRequest pullRequest = pullRequestRepository.findById(prId).orElse(null);
if (pullRequest == null) {
throw new IllegalArgumentException("Pull request " + prId + " not found");
}

List<PullRequestBadPractice> existingBadPractices = pullRequestBadPracticeRepository.findByPullRequestId(prId);
existingBadPractices.forEach(existingBadPractice -> existingBadPractice.setResolved(true));
pullRequestBadPracticeRepository.saveAll(existingBadPractices);

List<PullRequestBadPractice> detectedBadPractices = pullRequestBadPracticeDetector.detectAndSyncBadPractices(pullRequest);
return detectedBadPractices.stream().map(PullRequestBadPracticeDTO::fromPullRequestBadPractice).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,25 @@

from ..model import model


class PullRequest(BaseModel):
id: str
title: str
description: str

class BadPractice(BaseModel):
"""A detected bad practice in a pull request."""

title: str = Field(description="The title of the bad practice.")
description: str = Field(description="The description of the bad practice.")
resolved: bool = Field(description="Whether the bad practice has been resolved.")

class BadPracticeList(BaseModel):
"""A list of bad practices detected in a pull request."""

bad_practices: List[BadPractice] = Field(description="A list of bad practices detected in a pull request.")


def detectbadpractices(title, description) -> BadPracticeList:
def detectbadpractices(title, description, bad_practices) -> BadPracticeList:
prompt_path = Path(__file__).parent / "prompts" / "pullrequest_badpractice_detector.txt"
with open(prompt_path, "r", encoding="utf-8") as f:
prompt_text = f.read()
prompt_template = ChatPromptTemplate.from_template(prompt_text)
prompt = prompt_template.invoke({"title": title, "description": description})
prompt = prompt_template.invoke({"title": title, "description": description, "bad_practices": bad_practices})
structured_llm = model.with_structured_output(BadPracticeList)
response = structured_llm.invoke(prompt)
return response
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ PRIMARY TASK:
Detect and identify any bad practices in the provided pull request title and description.
- Review the title and description for any issues or violations of the guidelines.
- For each detected bad practice, provide a title and a brief description of the issue.
- Return a list of all detected bad practices in the pull request.
- Check the list of existing bad practices and add any new bad practices to the list.
- If the same bad practice is detected multiple times, return it consistently in the same way.
- If a bad practice was resolved, meaning it is no longer present in the title or description, mark it as resolved.
- If a bad practice was marked as resolved, but is detected again, mark it as detected again.
- Return a list of all detected and all resolved bad practices in the pull request.

GUIDELINES:
1. The title and description should be clear, concise, and descriptive.
Expand All @@ -29,4 +33,5 @@ REQUIREMENTS:
6. Multiple runs on the same title and description should return the same results if nothing has changed.

Pull Request Title: {title}
Pull Request Description: {description}
Pull Request Description: {description}
Existing Bad Practices: {bad_practices}
5 changes: 3 additions & 2 deletions server/intelligence-service/app/routers/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from fastapi import APIRouter
from openai import BaseModel

from ..detector.bad_practice_detector import PullRequest, detectbadpractices, BadPracticeList, BadPractice
from ..detector.bad_practice_detector import detectbadpractices, BadPractice

router = APIRouter(prefix="/detector", tags=["detector"])

class DetectorRequest(BaseModel):
title: str
description: str
bad_practices: List[BadPractice]

class DetectorResponse(BaseModel):
bad_practices: List[BadPractice]
Expand All @@ -20,4 +21,4 @@ class DetectorResponse(BaseModel):
summary="Detect bad practices for given pull request.",
)
def detect(request: DetectorRequest):
return detectbadpractices(request.title, request.description)
return detectbadpractices(request.title, request.description, request.bad_practices)

0 comments on commit 61c19da

Please sign in to comment.