From de6a9a9692db6667df3b7f972bcead57d6154e9f Mon Sep 17 00:00:00 2001 From: mlcui Date: Tue, 5 Mar 2024 23:47:57 +0000 Subject: [PATCH] git_cl: Add JSON output flag for `git cl presubmit` This allows scripts to use `git cl presubmit` to get JSON output from the presubmit hook. See https://crrev.com/c/5302055/comment/5b78090c_24935055/ for more context. Bug: None Change-Id: I5aaee016b74f8b7375eaf9c5d10cb0af8d86bad8 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5320109 Commit-Queue: Michael Cui Reviewed-by: Scott Lee --- git_cl.py | 25 +++++++++++++++---------- tests/git_cl_test.py | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/git_cl.py b/git_cl.py index 94fceed23..310d5f077 100755 --- a/git_cl.py +++ b/git_cl.py @@ -4665,6 +4665,9 @@ def CMDpresubmit(parser, args): help='Run presubmit checks in the ResultSink environment ' 'and send results to the ResultDB database.') parser.add_option('--realm', help='LUCI realm if reporting to ResultDB') + parser.add_option('-j', + '--json', + help='File to write JSON results to, or "-" for stdout') options, args = parser.parse_args(args) if not options.force and git_common.is_dirty_git_tree('presubmit'): @@ -4697,16 +4700,18 @@ def CMDpresubmit(parser, args): return 1 base_branch = 'HEAD' - cl.RunHook(committing=not options.upload, - may_prompt=False, - verbose=options.verbose, - parallel=options.parallel, - upstream=base_branch, - description=description, - all_files=options.all, - files=options.files, - resultdb=options.resultdb, - realm=options.realm) + result = cl.RunHook(committing=not options.upload, + may_prompt=False, + verbose=options.verbose, + parallel=options.parallel, + upstream=base_branch, + description=description, + all_files=options.all, + files=options.files, + resultdb=options.resultdb, + realm=options.realm) + if options.json: + write_json(options.json, result) return 0 diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index 6fbc3be62..11c72e087 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -4268,6 +4268,13 @@ class CMDTestCaseBase(unittest.TestCase): class CMDPresubmitTestCase(CMDTestCaseBase): + _RUN_HOOK_RETURN = { + 'errors': [], + 'more_cc': [], + 'notifications': [], + 'warnings': [] + } + def setUp(self): super(CMDPresubmitTestCase, self).setUp() mock.patch('git_cl.Changelist.GetCommonAncestorWithUpstream', @@ -4276,7 +4283,8 @@ class CMDPresubmitTestCase(CMDTestCaseBase): return_value='fetch description').start() mock.patch('git_cl._create_description_from_log', return_value='get description').start() - mock.patch('git_cl.Changelist.RunHook').start() + mock.patch('git_cl.Changelist.RunHook', + return_value=self._RUN_HOOK_RETURN).start() def testDefaultCase(self): self.assertEqual(0, git_cl.main(['presubmit'])) @@ -4340,6 +4348,12 @@ class CMDPresubmitTestCase(CMDTestCaseBase): resultdb=True, realm='chromium:public') + @mock.patch('git_cl.write_json') + def testJson(self, mock_write_json): + self.assertEqual(0, git_cl.main(['presubmit', '--json', 'file.json'])) + mock_write_json.assert_called_once_with('file.json', + self._RUN_HOOK_RETURN) + class CMDTryResultsTestCase(CMDTestCaseBase): _DEFAULT_REQUEST = {