Bug 1270317 - Add query_pushinfo to MercurialVCS; r=jlund draft
authorGregory Szorc <gps@mozilla.com>
Fri, 06 May 2016 10:52:52 -0700
changeset 368017 0b915260c86b0dbda02b6ac564d82939950bfb63
parent 368016 2ebd1a2a43bf6c1e675d99ebca3861fb63aac2d4
child 368018 6440a28b1b1fc14dc62dfef524392db511e008b2
push id18412
push userbmo:gps@mozilla.com
push dateTue, 17 May 2016 23:09:21 +0000
reviewersjlund
bugs1270317
milestone49.0a1
Bug 1270317 - Add query_pushinfo to MercurialVCS; r=jlund MercurialVCS doesn't currently implement the VCSMixin interface. This commit copies the implementation of query_pushinfo() from HgtoolVCS to MercurialVCS so it implements the interface. MozReview-Commit-ID: LKpLVhQoKww
testing/mozharness/mozharness/base/vcs/mercurial.py
--- a/testing/mozharness/mozharness/base/vcs/mercurial.py
+++ b/testing/mozharness/mozharness/base/vcs/mercurial.py
@@ -8,24 +8,26 @@
 
 Largely copied/ported from
 https://hg.mozilla.org/build/tools/file/cf265ea8fb5e/lib/python/util/hg.py .
 """
 
 import os
 import re
 import subprocess
+from collections import namedtuple
 from urlparse import urlsplit
 
 import sys
 sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.dirname(sys.path[0]))))
 
 from mozharness.base.errors import HgErrorList, VCSException
 from mozharness.base.log import LogMixin
 from mozharness.base.script import ScriptMixin
+from mozharness.base.transfer import TransferMixin
 
 HG_OPTIONS = ['--config', 'ui.merge=internal:merge']
 
 # MercurialVCS {{{1
 # TODO Make the remaining functions more mozharness-friendly.
 # TODO Add the various tag functionality that are currently in
 # build/tools/scripts to MercurialVCS -- generic tagging logic belongs here.
 REVISION, BRANCH = 0, 1
@@ -45,17 +47,17 @@ def make_hg_url(hg_host, repo_path, prot
             return repo
         else:
             return '/'.join([p.strip('/') for p in [repo, 'rev', revision]])
     else:
         assert revision
         return '/'.join([p.strip('/') for p in [repo, 'raw-file', revision, filename]])
 
 
-class MercurialVCS(ScriptMixin, LogMixin, object):
+class MercurialVCS(ScriptMixin, LogMixin, TransferMixin):
     # For the most part, scripts import mercurial, update,
     # hgtool uses mercurial, share, out
     # tag-release.py imports
     #  apply_and_push, update, get_revision, out, BRANCH, REVISION,
     #  get_branches, cleanOutgoingRevs
 
     def __init__(self, log_obj=None, config=None, vcs_config=None,
                  script_obj=None):
@@ -527,12 +529,47 @@ class MercurialVCS(ScriptMixin, LogMixin
         # TODO retry
         self.info("Wiping outgoing local changes from %s to %s." % (reponame, remote))
         outgoingRevs = self.out(src=reponame, remote=remote,
                                 ssh_username=username, ssh_key=sshKey)
         for r in reversed(outgoingRevs):
             self.run_command(self.hg + ['strip', '-n', r[REVISION]],
                              cwd=reponame, error_list=HgErrorList)
 
+    def query_pushinfo(self, repository, revision):
+        """Query the pushdate and pushid of a repository/revision.
+        This is intended to be used on hg.mozilla.org/mozilla-central and
+        similar. It may or may not work for other hg repositories.
+        """
+        PushInfo = namedtuple('PushInfo', ['pushid', 'pushdate'])
+
+        try:
+            url = '%s/json-pushes?changeset=%s' % (repository, revision)
+            self.info('Pushdate URL is: %s' % url)
+            contents = self.retry(self.load_json_from_url, args=(url,))
+
+            # The contents should be something like:
+            # {
+            #   "28537": {
+            #    "changesets": [
+            #     "1d0a914ae676cc5ed203cdc05c16d8e0c22af7e5",
+            #    ],
+            #    "date": 1428072488,
+            #    "user": "user@mozilla.com"
+            #   }
+            # }
+            #
+            # So we grab the first element ("28537" in this case) and then pull
+            # out the 'date' field.
+            pushid = contents.iterkeys().next()
+            self.info('Pushid is: %s' % pushid)
+            pushdate = contents[pushid]['date']
+            self.info('Pushdate is: %s' % pushdate)
+            return PushInfo(pushid, pushdate)
+
+        except Exception:
+            self.exception("Failed to get push info from hg.mozilla.org")
+            raise
+
 
 # __main__ {{{1
 if __name__ == '__main__':
     pass