Skip to content

Commit

Permalink
Serializer previous_diff_id
Browse files Browse the repository at this point in the history
  • Loading branch information
vrigal committed Dec 6, 2024
1 parent 74fa38a commit f77168b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
48 changes: 31 additions & 17 deletions backend/code_review_backend/issues/v2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,33 @@
from rest_framework.generics import ListAPIView

from code_review_backend.issues.models import Diff, IssueLink
from code_review_backend.issues.v2.serializers import IssueLinkHashSerializer
from code_review_backend.issues.v2.serializers import DiffIssuesSerializer


class IssueList(ListAPIView):
serializer_class = IssueLinkHashSerializer
"""
Returns issues that are linked to a diff, and depending on the selected mode:
* known: Issues that have also being detected from the base repository (e.g. mozilla-central).
`previous_diff_id` is always returned as null when listing known issues.
* unresolved: Issues that were linked to the previous diff of the same parent revision, and are
still present on the current diff. Filters out issues that are known by the backend.
* closed: Issues that were listed on the previous diff of the same parent revision, but does
not exist on the current diff.
This endpoint does not uses pagination.
"""

allowed_modes = ["unresolved", "known", "closed"]
pagination_class = None

def get_serializer(self, queryset, many=True):
return DiffIssuesSerializer(
{
"previous_diff_id": self.previous_diff,
"issues": queryset,
},
context=self.get_serializer_context(),
)

@cached_property
def diff(self):
Expand All @@ -26,13 +47,14 @@ def diff(self):

@cached_property
def previous_diff(self):
previous_diff = (
if self.mode == "known":
# No need to search for a previous diff for known issues
return None
return (
self.diff.revision.diffs.filter(created__lt=self.diff.created)
.order_by("created")
.last()
)
if not previous_diff:
raise BadRequest("No previous diff was found to compare issues")

@cached_property
def mode(self):
Expand All @@ -54,9 +76,6 @@ def distinct_issues(self, qs):

@property
def known_issues(self):
"""
Issues that have also being detected from the base repository (e.g. mozilla-central).
"""
return self.distinct_issues(
self.diff.issue_links.annotate(
known=Exists(
Expand All @@ -70,11 +89,8 @@ def known_issues(self):

@property
def unresolved_issues(self):
"""
Issues that were linked to the previous diff of the same
parent revision, and are still present on the current diff.
Filters out issues that are known by the backend.
"""
if not self.previous_diff:
raise BadRequest("No previous diff was found to compare issues")
return self.distinct_issues(
self.known_issues.filter(known=False).annotate(
unresolved=Exists(
Expand All @@ -88,10 +104,8 @@ def unresolved_issues(self):

@property
def closed_issues(self):
"""
Issues that were listed on the previous diff of the same
parent revision, but does not exist on the current diff.
"""
if not self.previous_diff:
raise BadRequest("No previous diff was found to compare issues")
return self.distinct_issues(
self.previous_diff.issue_links.annotate(
closed=~Exists(
Expand Down
5 changes: 5 additions & 0 deletions backend/code_review_backend/issues/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ class IssueLinkHashSerializer(serializers.ModelSerializer):
class Meta:
model = IssueLink
fields = ("id", "hash")


class DiffIssuesSerializer(serializers.Serializer):
previous_diff_id = serializers.UUIDField(allow_null=True, read_only=True)
issues = IssueLinkHashSerializer(many=True)

0 comments on commit f77168b

Please sign in to comment.