From 5e9a048a4b522979cd14e5235e4681b76116b9ec Mon Sep 17 00:00:00 2001 From: Andrew Halberstadt Date: Mon, 18 Sep 2017 12:43:03 -0400 Subject: [PATCH] Bug 1400469 - Add ability to specify commit message to |mach try|, r=armenzg MozReview-Commit-ID: LWkAEDWn8NC --HG-- extra : rebase_source : 24fd977d23f9f09859aa9f9a44c53ffd6c1f4673 --- tools/tryselect/cli.py | 23 ++++++++++++ tools/tryselect/selectors/fuzzy.py | 5 +-- tools/tryselect/selectors/syntax.py | 2 +- tools/tryselect/test/cram.ini | 1 + tools/tryselect/test/setup.sh | 6 ++++ tools/tryselect/test/test_fuzzy.t | 34 +++++++++--------- tools/tryselect/test/test_message.t | 55 +++++++++++++++++++++++++++++ tools/tryselect/test/test_preset.t | 42 ++++++++-------------- tools/tryselect/vcs.py | 9 ++--- 9 files changed, 124 insertions(+), 53 deletions(-) create mode 100644 tools/tryselect/test/test_message.t diff --git a/tools/tryselect/cli.py b/tools/tryselect/cli.py index 471aeb66e472..f284daa7c775 100644 --- a/tools/tryselect/cli.py +++ b/tools/tryselect/cli.py @@ -4,6 +4,9 @@ from __future__ import absolute_import, print_function, unicode_literals +import os +import subprocess +import tempfile from argparse import ArgumentParser from .templates import all_templates @@ -12,6 +15,13 @@ from .templates import all_templates class BaseTryParser(ArgumentParser): name = 'try' common_arguments = [ + [['-m', '--message'], + {'const': 'editor', + 'default': '{msg}', + 'nargs': '?', + 'help': 'Use the specified commit message, or create it in your ' + '$EDITOR if blank. Defaults to computed message.', + }], [['--no-push'], {'dest': 'push', 'action': 'store_false', @@ -52,8 +62,21 @@ class BaseTryParser(ArgumentParser): for template in self.templates.values(): template.add_arguments(group) + def validate(self, args): + if args.message == 'editor': + if 'EDITOR' not in os.environ: + self.error("must set the $EDITOR environment variable to use blank --message") + + with tempfile.NamedTemporaryFile(mode='r') as fh: + subprocess.call([os.environ['EDITOR'], fh.name]) + args.message = fh.read().strip() + + if '{msg}' not in args.message: + args.message = '{}\n\n{}'.format(args.message, '{msg}') + def parse_known_args(self, *args, **kwargs): args, remainder = ArgumentParser.parse_known_args(self, *args, **kwargs) + self.validate(args) if self.templates: args.templates = {} diff --git a/tools/tryselect/selectors/fuzzy.py b/tools/tryselect/selectors/fuzzy.py index e4c1d9b2e948..89e539ea86d7 100644 --- a/tools/tryselect/selectors/fuzzy.py +++ b/tools/tryselect/selectors/fuzzy.py @@ -198,7 +198,8 @@ def format_header(): def run_fuzzy_try(update=False, query=None, templates=None, full=False, parameters=None, - save=False, preset=None, list_presets=False, push=True, **kwargs): + save=False, preset=None, list_presets=False, push=True, message='{msg}', + **kwargs): if list_presets: return pset.list_presets(section='fuzzy') @@ -248,4 +249,4 @@ def run_fuzzy_try(update=False, query=None, templates=None, full=False, paramete query = " with query: {}".format(query) if query else "" msg = "Fuzzy{}".format(query) - return vcs.push_to_try('fuzzy', msg, selected, templates, push=push) + return vcs.push_to_try('fuzzy', message.format(msg=msg), selected, templates, push=push) diff --git a/tools/tryselect/selectors/syntax.py b/tools/tryselect/selectors/syntax.py index c5655411133b..8a2746fb3ae0 100644 --- a/tools/tryselect/selectors/syntax.py +++ b/tools/tryselect/selectors/syntax.py @@ -638,7 +638,7 @@ class AutoTry(object): if kwargs["verbose"]: print('The following try syntax was calculated:\n%s' % msg) - self.vcs.push_to_try('syntax', msg, push=kwargs['push']) + self.vcs.push_to_try('syntax', kwargs["message"].format(msg=msg), push=kwargs['push']) if kwargs["save"]: assert msg.startswith("try: ") diff --git a/tools/tryselect/test/cram.ini b/tools/tryselect/test/cram.ini index 2625bdfee1d9..e43f360dd05a 100644 --- a/tools/tryselect/test/cram.ini +++ b/tools/tryselect/test/cram.ini @@ -1,2 +1,3 @@ [test_fuzzy.t] +[test_message.t] [test_preset.t] diff --git a/tools/tryselect/test/setup.sh b/tools/tryselect/test/setup.sh index e5633cda4139..b6112bd5f75f 100644 --- a/tools/tryselect/test/setup.sh +++ b/tools/tryselect/test/setup.sh @@ -1,6 +1,12 @@ export topsrcdir=$TESTDIR/../../../ export MOZBUILD_STATE_PATH=$TMP/mozbuild +export MACHRC=$TMP/machrc +cat > $MACHRC << EOF +[try] +default=syntax +EOF + cachedir=$MOZBUILD_STATE_PATH/cache/taskgraph mkdir -p $cachedir diff --git a/tools/tryselect/test/test_fuzzy.t b/tools/tryselect/test/test_fuzzy.t index f06fc89e899b..47caaba0e35f 100644 --- a/tools/tryselect/test/test_fuzzy.t +++ b/tools/tryselect/test/test_fuzzy.t @@ -4,7 +4,11 @@ Test fuzzy selector $ ./mach try fuzzy $testargs -q "'foo" - Calculated try selector: + Commit message: + Fuzzy with query: 'foo + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: { "tasks":[ "test/foo-debug", @@ -12,14 +16,14 @@ Test fuzzy selector ] } - Commit message: - Fuzzy with query: 'foo - - Pushed via `mach try fuzzy` $ ./mach try fuzzy $testargs -q "'bar" no tasks selected $ ./mach try fuzzy $testargs --full -q "'bar" - Calculated try selector: + Commit message: + Fuzzy with query: 'bar + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: { "tasks":[ "test/bar-debug", @@ -27,15 +31,15 @@ Test fuzzy selector ] } - Commit message: - Fuzzy with query: 'bar - - Pushed via `mach try fuzzy` Test templates $ ./mach try fuzzy --no-push --artifact -q "'foo" - Calculated try selector: + Commit message: + Fuzzy with query: 'foo + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: { "templates":{ "artifact":{ @@ -48,12 +52,12 @@ Test templates ] } + $ ./mach try fuzzy $testargs --env FOO=1 --env BAR=baz -q "'foo" Commit message: Fuzzy with query: 'foo Pushed via `mach try fuzzy` - $ ./mach try fuzzy $testargs --env FOO=1 --env BAR=baz -q "'foo" - Calculated try selector: + Calculated try_task_config.json: { "templates":{ "env":{ @@ -67,7 +71,3 @@ Test templates ] } - Commit message: - Fuzzy with query: 'foo - - Pushed via `mach try fuzzy` diff --git a/tools/tryselect/test/test_message.t b/tools/tryselect/test/test_message.t new file mode 100644 index 000000000000..553898f4c9dc --- /dev/null +++ b/tools/tryselect/test/test_message.t @@ -0,0 +1,55 @@ + $ . $TESTDIR/setup.sh + $ cd $topsrcdir + +Test custom commit messages with fuzzy selector + + $ ./mach try fuzzy $testargs -q foo --message "Foobar" + Commit message: + Foobar + + Fuzzy with query: foo + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: + { + "tasks":[ + "test/foo-debug", + "test/foo-opt" + ] + } + + $ ./mach try fuzzy $testargs -q foo -m "Foobar: {msg}" + Commit message: + Foobar: Fuzzy with query: foo + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: + { + "tasks":[ + "test/foo-debug", + "test/foo-opt" + ] + } + + $ unset EDITOR + $ ./mach try fuzzy $testargs -q foo -m > /dev/null 2>&1 + [2] + + +Test custom commit messages with syntax selector + + $ ./mach try syntax $testargs -p linux -u mochitests --message "Foobar" + Commit message: + Foobar + + try: -b do -p linux -u mochitests + + Pushed via `mach try syntax` + $ ./mach try syntax $testargs -p linux -u mochitests -m "Foobar: {msg}" + Commit message: + Foobar: try: -b do -p linux -u mochitests + + Pushed via `mach try syntax` + $ unset EDITOR + $ ./mach try syntax $testargs -p linux -u mochitests -m > /dev/null 2>&1 + [2] diff --git a/tools/tryselect/test/test_preset.t b/tools/tryselect/test/test_preset.t index 9f084f41dc42..8dbc06f42f4c 100644 --- a/tools/tryselect/test/test_preset.t +++ b/tools/tryselect/test/test_preset.t @@ -4,23 +4,17 @@ Test preset with no subcommand $ ./mach try $testargs --save foo -b do -p linux -u mochitests -t none --tag foo - Calculated try selector: - try: -b do -p linux -u mochitests -t none --tag foo Commit message: try: -b do -p linux -u mochitests -t none --tag foo Pushed via `mach try syntax` preset saved, run with: --preset=foo $ ./mach try $testargs --preset foo - Calculated try selector: - try: -b do -p linux -u mochitests -t none --tag foo Commit message: try: -b do -p linux -u mochitests -t none --tag foo Pushed via `mach try syntax` $ ./mach try syntax $testargs --preset foo - Calculated try selector: - try: -b do -p linux -u mochitests -t none --tag foo Commit message: try: -b do -p linux -u mochitests -t none --tag foo @@ -31,23 +25,17 @@ Test preset with no subcommand Test preset with syntax subcommand $ ./mach try syntax $testargs --save bar -b do -p win32 -u none -t all --tag bar - Calculated try selector: - try: -b do -p win32 -u none -t all --tag bar Commit message: try: -b do -p win32 -u none -t all --tag bar Pushed via `mach try syntax` preset saved, run with: --preset=bar $ ./mach try syntax $testargs --preset bar - Calculated try selector: - try: -b do -p win32 -u none -t all --tag bar Commit message: try: -b do -p win32 -u none -t all --tag bar Pushed via `mach try syntax` $ ./mach try $testargs --preset bar - Calculated try selector: - try: -b do -p win32 -u none -t all --tag bar Commit message: try: -b do -p win32 -u none -t all --tag bar @@ -60,40 +48,40 @@ Test preset with fuzzy subcommand $ ./mach try fuzzy $testargs --save baz -q "'baz" preset saved, run with: --preset=baz - Calculated try selector: + Commit message: + Fuzzy with query: 'baz + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: { "tasks":[ "build-baz" ] } - Commit message: - Fuzzy with query: 'baz - - Pushed via `mach try fuzzy` $ ./mach try fuzzy $testargs --preset baz - Calculated try selector: + Commit message: + Fuzzy with query: 'baz + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: { "tasks":[ "build-baz" ] } - Commit message: - Fuzzy with query: 'baz - - Pushed via `mach try fuzzy` $ ./mach try $testargs --preset baz - Calculated try selector: + Commit message: + Fuzzy with query: 'baz + + Pushed via `mach try fuzzy` + Calculated try_task_config.json: { "tasks":[ "build-baz" ] } - Commit message: - Fuzzy with query: 'baz - - Pushed via `mach try fuzzy` $ ./mach try fuzzy $testargs --list-presets baz: 'baz diff --git a/tools/tryselect/vcs.py b/tools/tryselect/vcs.py index 95632c1109fe..fdb43cdedbd3 100644 --- a/tools/tryselect/vcs.py +++ b/tools/tryselect/vcs.py @@ -108,15 +108,12 @@ class VCSHelper(object): try: if not push: - print("Calculated try selector:") + print("Commit message:") + print(commit_message) if config: + print("Calculated try_task_config.json:") with open(config) as fh: print(fh.read()) - else: - print(msg) - - print('Commit message:') - print(commit_message) return self._push_to_try(commit_message, config)