mozreview: add functions for retrieving diffstat information (Bug 1286017). r?glob draft
authorSteven MacLeod <smacleod@mozilla.com>
Mon, 11 Jul 2016 14:58:53 -0400
changeset 8935 3724e896f622d6a59830b41497ddb78164f2754b
parent 8934 d2140637eaf3f91fefa7c2f44cbaabf4c19faeb3
child 8936 6b12cacfa18e579528dbd77b3c9c60a01af0e476
push id1028
push usersmacleod@mozilla.com
push dateMon, 18 Jul 2016 19:05:53 +0000
reviewersglob
bugs1286017
mozreview: add functions for retrieving diffstat information (Bug 1286017). r?glob This commit ignores extra information Review Board has, such as information on how many lines were replaced, rather than just deleted and inserted. This is done to keep things simple as the extra information isn't always present and I haven't thought of a good way to conditionally pass things along. This also makes consuming this new data much simpler as two numbers which will always be present are used. These numbers are also pretty self explanatory. In the future we might want to expand this, but we can always keep the current numbers we provide and just add new information. MozReview-Commit-ID: JJSiQH51qm5
pylib/mozreview/mozreview/diffviewer/__init__.py
--- a/pylib/mozreview/mozreview/diffviewer/__init__.py
+++ b/pylib/mozreview/mozreview/diffviewer/__init__.py
@@ -0,0 +1,36 @@
+from reviewboard.diffviewer.models import DiffSet
+
+
+def diffstats(diffset):
+    """Return a dictionary of diffstat information for a diffset."""
+    counts = diffset.get_total_line_counts()
+
+    # TODO: Take into account the non raw counts review board might
+    # have to display information about replacement.
+    return {
+        'insert': counts.get('raw_insert_count', 0),
+        'delete': counts.get('raw_delete_count', 0),
+    }
+
+
+def get_diffstats(review_request, user, rev=None):
+    """Return a dictionary containing diffstat information.
+
+    If no rev is provided, use the latest diffset. If the requesting
+    user is the submitter, take any draft diffsets into account.
+    """
+    # Ensure we're working with the base review request, not a draft.
+    review_request = review_request.get_review_request()
+
+    if rev is None:
+        # If the user is the submitter we might want to use the draft diffset.
+        draft = review_request.get_draft(user=user)
+        diffset = ((draft and draft.diffset) or
+                   review_request.get_latest_diffset())
+    else:
+        diffset = (
+            DiffSet.objects
+            .filter(history__pk=review_request.diffset_history_id)
+            .filter(revision=rev)).latest()
+
+    return diffstats(diffset)