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 <mlcui@google.com>
Reviewed-by: Scott Lee <ddoman@chromium.org>
changes/09/5320109/2
mlcui 1 year ago committed by LUCI CQ
parent 1a92126938
commit de6a9a9692

@ -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

@ -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 = {

Loading…
Cancel
Save