Commit e328cf9c authored by phajdan.jr's avatar phajdan.jr Committed by Commit bot

git cl issue: add --json switch for machine-readable output

This was requested in https://chromium-review.googlesource.com/c/373499

BUG=none

Review-Url: https://codereview.chromium.org/2263103002
parent b8f8dd79
......@@ -3446,6 +3446,11 @@ def colorize_CMDstatus_doc():
CMDstatus.__doc__ = '\n'.join(colorize_line(l) for l in lines)
def write_json(path, contents):
with open(path, 'w') as f:
json.dump(contents, f)
@subcommand.usage('[issue_number]')
def CMDissue(parser, args):
"""Sets or displays the current code review issue number.
......@@ -3456,6 +3461,7 @@ def CMDissue(parser, args):
help='Lookup the branch(es) for the specified issues. If '
'no issues are specified, all branches with mapped '
'issues will be listed.')
parser.add_option('--json', help='Path to JSON output file.')
_add_codereview_select_options(parser)
options, args = parser.parse_args(args)
_process_codereview_select_options(parser, options)
......@@ -3471,11 +3477,15 @@ def CMDissue(parser, args):
issue_branch_map.setdefault(cl.GetIssue(), []).append(branch)
if not args:
args = sorted(issue_branch_map.iterkeys())
result = {}
for issue in args:
if not issue:
continue
result[int(issue)] = issue_branch_map.get(int(issue))
print('Branch for issue number %s: %s' % (
issue, ', '.join(issue_branch_map.get(int(issue)) or ('None',))))
if options.json:
write_json(options.json, result)
else:
cl = Changelist(codereview=options.forced_codereview)
if len(args) > 0:
......@@ -3486,6 +3496,11 @@ def CMDissue(parser, args):
'Maybe you want to run git cl status?')
cl.SetIssue(issue)
print('Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL()))
if options.json:
write_json(options.json, {
'issue': cl.GetIssue(),
'issue_url': cl.GetIssueURL(),
})
return 0
......
......@@ -251,6 +251,8 @@ class TestGitCl(TestCase):
self.mock(git_cl, 'BranchExists', lambda _: True)
self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
self.mock(git_cl, 'ask_for_data', self._mocked_call)
self.mock(git_cl, 'write_json', lambda path, contents:
self._mocked_call('write_json', path, contents))
self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
......@@ -1759,6 +1761,22 @@ class TestGitCl(TestCase):
]
self.assertEqual(0, git_cl.main(['issue', '0']))
def test_cmd_issue_json(self):
out = StringIO.StringIO()
self.mock(git_cl.sys, 'stdout', out)
self.calls = [
((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
((['git', 'config', '--int', 'branch.feature.rietveldissue'],), '123'),
((['git', 'config', 'rietveld.autoupdate'],), ''),
((['git', 'config', 'rietveld.server'],),
'https://codereview.chromium.org'),
((['git', 'config', 'branch.feature.rietveldserver'],), ''),
(('write_json', 'output.json',
{'issue': 123, 'issue_url': 'https://codereview.chromium.org/123'}),
''),
]
self.assertEqual(0, git_cl.main(['issue', '--json', 'output.json']))
def test_git_cl_try_default(self):
self.mock(git_cl.Changelist, 'GetChange',
lambda _, *a: (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment