Bug 1270317 - Add query_pushinfo to MercurialVCS; r?jlund draft
authorGregory Szorc <gps@mozilla.com>
Wed, 04 May 2016 16:50:52 -0700
changeset 363538 eaaf73a0b0e451e95e3cc9dcd2d670654572be02
parent 363537 38ecca3529fc6cc648972ceba17e1e1b177693a9
child 363539 d295a0db61e5bea77b5e632f89aac203dd22f6c9
push id17234
push usergszorc@mozilla.com
push dateThu, 05 May 2016 00:53:38 +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