From 625986dba7852518199ec002fd4cece02929f4ab Mon Sep 17 00:00:00 2001 From: Andrii Shyshkalov Date: Thu, 16 Mar 2017 00:24:37 +0100 Subject: [PATCH] git cl comment: implement adding comment for Gerrit. BUG=698236 Change-Id: Ia1a36af71c348be991d77083092c5043c2642c19 Reviewed-on: https://chromium-review.googlesource.com/455877 Reviewed-by: Aaron Gable Commit-Queue: Andrii Shyshkalov --- git_cl.py | 20 ++++++++++++++++++-- tests/git_cl_test.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/git_cl.py b/git_cl.py index c3e564fd5f..342d82c11c 100755 --- a/git_cl.py +++ b/git_cl.py @@ -1654,6 +1654,9 @@ class Changelist(object): # Forward methods to codereview specific implementation. + def AddComment(self, message): + return self._codereview_impl.AddComment(message) + def CloseIssue(self): return self._codereview_impl.CloseIssue() @@ -1752,6 +1755,10 @@ class _ChangelistCodereviewBase(object): """Update the description on codereview site.""" raise NotImplementedError() + def AddComment(self, message): + """Posts a comment to the codereview site.""" + raise NotImplementedError() + def CloseIssue(self): """Closes the issue.""" raise NotImplementedError() @@ -2488,6 +2495,10 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase): gerrit_util.SetCommitMessage(self._GetGerritHost(), self.GetIssue(), description, notify='NONE') + def AddComment(self, message): + gerrit_util.SetReview(self._GetGerritHost(), self.GetIssue(), + msg=message) + def CloseIssue(self): gerrit_util.AbandonChange(self._GetGerritHost(), self.GetIssue(), msg='') @@ -4198,11 +4209,13 @@ def CMDcomments(parser, args): parser.add_option('-a', '--add-comment', dest='comment', help='comment to add to an issue') parser.add_option('-i', dest='issue', - help="review issue id (defaults to current issue)") + help='review issue id (defaults to current issue)') parser.add_option('-j', '--json-file', help='File to write JSON summary to') auth.add_auth_options(parser) + _add_codereview_select_options(parser) options, args = parser.parse_args(args) + _process_codereview_select_options(parser, options) auth_config = auth.extract_auth_config_from_options(options) issue = None @@ -4212,7 +4225,10 @@ def CMDcomments(parser, args): except ValueError: DieWithError('A review issue id is expected to be a number') - cl = Changelist(issue=issue, codereview='rietveld', auth_config=auth_config) + cl = Changelist(issue=issue, + # TODO(tandrii): remove 'rietveld' default. + codereview=options.forced_codereview or 'rietveld', + auth_config=auth_config) if options.comment: cl.AddComment(options.comment) diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index 2af31749a4..7d8ee6d628 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -3089,6 +3089,34 @@ class TestGitCl(TestCase): sys.stdout.getvalue(), 'However, your configured .gitcookies file is missing.') + def test_git_cl_comment_add_default(self): + self.mock(git_cl._RietveldChangelistImpl, 'AddComment', + lambda _, message: self._mocked_call('AddComment', message)) + self.calls = [ + ((['git', 'config', 'rietveld.autoupdate'],), CERR1), + ((['git', 'config', 'rietveld.server'],), 'codereview.chromium.org'), + (('AddComment', 'msg'), ''), + ] + # TODO(tandrii): --rietveld should be specified here. + self.assertEqual(0, git_cl.main(['comment', '-i', '10', '-a', 'msg'])) + + def test_git_cl_comment_add_gerrit(self): + self.mock(git_cl.gerrit_util, 'SetReview', + lambda host, change, msg: + self._mocked_call('SetReview', host, change, msg)) + self.calls = [ + ((['git', 'symbolic-ref', 'HEAD'],), CERR1), + ((['git', 'symbolic-ref', 'HEAD'],), CERR1), + ((['git', 'config', 'rietveld.upstream-branch'],), CERR1), + ((['git', 'branch', '-r'],), 'origin/HEAD -> origin/master\n' + 'origin/master'), + ((['git', 'config', 'remote.origin.url'],), + 'https://chromium.googlesource.com/infra/infra'), + (('SetReview', 'chromium-review.googlesource.com', 10, 'msg'), None), + ] + self.assertEqual(0, git_cl.main(['comment', '--gerrit', '-i', '10', + '-a', 'msg'])) + if __name__ == '__main__': logging.basicConfig(