Skip to content

Commit

Permalink
Update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vrigal committed Nov 22, 2024
1 parent 6b3da5c commit 276e8a2
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions backend/code_review_backend/issues/api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from django.db.models import Exists, OuterRef
from django.conf import settings
from django.db.models import Exists, OuterRef, Value
from django.shortcuts import get_object_or_404
from django.urls import path
from django.utils.functional import cached_property
Expand Down Expand Up @@ -32,47 +33,62 @@ def mode(self):
return mode

def get_queryset(self):
return getattr(self, f"get_{self.mode}")()
return getattr(self, f"{self.mode}_issues").filter(**{self.mode: True})

def distinct_issues(self, qs):
"""
Convert a list of issue links to unique couples of (issue_id, issue_hash)
"""
attributes = ("issue_id", "issue__hash")
if "postgresql" in settings.DATABASES["default"]["ENGINE"]:
return qs.order_by(*attributes).values(*attributes).distinct(*attributes)
return qs.order_by(*attributes).values(*attributes).distinct()

def get_unresolved(self):
@property
def known_issues(self):
"""
Issues that were linked to a previous diff of the same
parent revision, and are still present on the current diff.
Issues that have also being detected from mozilla-central.
"""
return self.distinct_issues(
self.diff.revision.issue_links.annotate(
unresolved=Exists(
IssueLink.objects.exclude(diff=self.diff).filter(
revision=self.diff.revision,
self.diff.issue_links.annotate(
known=Exists(
IssueLink.objects.filter(
revision__base_repository__slug="mozilla-central",
issue=OuterRef("issue"),
)
)
).filter(unresolved=True)
)
)

def get_known(self):
@property
def unresolved_issues(self):
"""
Issues that have also being detected from mozilla-central.
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.
"""
previous_diff = (
self.diff.revision.diffs.filter(created__lt=self.diff.created)
.order_by("created")
.last()
)
if not previous_diff:
return self.known_issues.filter(known=False).annotate(
unresolved=Value(True)
)
return self.distinct_issues(
self.diff.issue_links.annotate(
known=Exists(
self.known_issues.filter(known=False).annotate(
unresolved=Exists(
IssueLink.objects.filter(
revision__base_repository__slug="mozilla-central",
diff=previous_diff,
issue=OuterRef("issue"),
)
)
).filter(known=True)
)
)

def get_closed(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.
Expand All @@ -84,16 +100,16 @@ def get_closed(self):
.last()
)
if not previous_diff:
return []
return IssueLink.objects.none().annotate(closed=Value("True"))
return self.distinct_issues(
previous_diff.issue_links.annotate(
closed=Exists(
closed=~Exists(
IssueLink.objects.filter(
diff=self.diff,
issue=OuterRef("issue"),
)
)
).filter(closed=True)
)
)


Expand Down

0 comments on commit 276e8a2

Please sign in to comment.