[depot_tools] Make try-results fetch recent dry-runs

Running git cl try-results fetches the latest patchset which may be a trivial change, e.g. rebase or commit message change.
This change allows try-results to get the result of patchsets equivalent to the latest and returns their results.

Bug: 774179
Change-Id: Iec2fea102597773e24c2e887f19282f14c123e61
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2532899
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
diff --git a/git_cl.py b/git_cl.py
index 67d3499..d2fb3fa 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -1722,6 +1722,30 @@
     self.SetPatchset(patchset)
     return patchset
 
+  def GetMostRecentDryRunPatchset(self):
+    """Get patchsets equivalent to the most recent patchset and return
+    the patchset with the latest dry run. If none have been dry run, return
+    the latest patchset."""
+    if not self.GetIssue():
+      return None
+
+    data = self._GetChangeDetail(['ALL_REVISIONS'])
+    patchset = data['revisions'][data['current_revision']]['_number']
+    dry_run = set([int(m['_revision_number'])
+        for m in data.get('messages', [])
+        if m.get('tag', '').endswith('dry-run')])
+
+    for revision_info in sorted(data.get('revisions', {}).values(),
+        key=lambda c: c['_number'], reverse=True):
+      if revision_info['_number'] in dry_run:
+        patchset = revision_info['_number']
+        break
+      if revision_info.get('kind', '') not in \
+          ('NO_CHANGE', 'NO_CODE_CHANGE', 'TRIVIAL_REBASE'):
+        break
+    self.SetPatchset(patchset)
+    return patchset
+
   def AddComment(self, message, publish=None):
     gerrit_util.SetReview(
         self._GetGerritHost(), self._GerritChangeIdentifier(),
@@ -4533,7 +4557,7 @@
 
   patchset = options.patchset
   if not patchset:
-    patchset = cl.GetMostRecentPatchset()
+    patchset = cl.GetMostRecentDryRunPatchset()
     if not patchset:
       parser.error('Code review host doesn\'t know about issue %s. '
                    'No access to issue or wrong issue number?\n'