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
--- 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)