From 5a0cf20452b7dac174dd0377f5b030379ba543ef Mon Sep 17 00:00:00 2001 From: Andrii Shyshkalov Date: Fri, 17 Mar 2017 16:14:59 +0100 Subject: [PATCH] git cl comment: implement fetching comments for Gerrit. R=agable@chromium.org,machenbach@chromium.org BUG=698236 Change-Id: I36dcd2f10d0518a9f04a766e09c1ab00f2ce4d3b Reviewed-on: https://chromium-review.googlesource.com/456699 Commit-Queue: Andrii Shyshkalov Reviewed-by: Michael Achenbach --- git_cl.py | 24 +++++++++++-- tests/git_cl_test.py | 83 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/git_cl.py b/git_cl.py index 1a67a458d..b63639f75 100755 --- a/git_cl.py +++ b/git_cl.py @@ -2531,8 +2531,25 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase): msg=message) def GetCommentsSummary(self): - # TODO(tandrii): implement in follow up CL (http://crbug.com/698236). - raise NotImplementedError() + # DETAILED_ACCOUNTS is to get emails in accounts. + data = self._GetChangeDetail(options=['MESSAGES', 'DETAILED_ACCOUNTS']) + summary = [] + for msg in data.get('messages', []): + # Gerrit spits out nanoseconds. + assert len(msg['date'].split('.')[-1]) == 9 + date = datetime.datetime.strptime(msg['date'][:-3], + '%Y-%m-%d %H:%M:%S.%f') + summary.append(_CommentSummary( + date=date, + message=msg['message'], + sender=msg['author']['email'], + # These could be inferred from the text messages and correlated with + # Code-Review label maximum, however this is not reliable. + # Leaving as is until the need arises. + approval=False, + disapproval=False, + )) + return summary def CloseIssue(self): gerrit_util.AbandonChange(self._GetGerritHost(), self.GetIssue(), msg='') @@ -4262,7 +4279,8 @@ def CMDcomments(parser, args): cl = Changelist(issue=issue, # TODO(tandrii): remove 'rietveld' default. - codereview=options.forced_codereview or 'rietveld', + codereview=options.forced_codereview or ( + 'rietveld' if issue else None), auth_config=auth_config) if options.comment: diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index e8a84880a..c30d688b5 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -3171,6 +3171,89 @@ class TestGitCl(TestCase): self.assertEqual(read[1]['date'], '2017-03-13 20:49:34.515270') self.assertEqual(read[2]['date'], '2017-03-13 21:50:34.515270') + def test_git_cl_comments_fetch_gerrit(self): + self.mock(sys, 'stdout', StringIO.StringIO()) + 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'), + (('GetChangeDetail', 'chromium-review.googlesource.com', '1', + ['MESSAGES', 'DETAILED_ACCOUNTS']), { + 'owner': {'email': 'owner@example.com'}, + 'messages': [ + { + u'_revision_number': 1, + u'author': { + u'_account_id': 1111084, + u'email': u'commit-bot@chromium.org', + u'name': u'Commit Bot' + }, + u'date': u'2017-03-15 20:08:45.000000000', + u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046dc50b', + u'message': u'Patch Set 1:\n\nDry run: CQ is trying da patch...', + u'tag': u'autogenerated:cq:dry-run' + }, + { + u'_revision_number': 2, + u'author': { + u'_account_id': 11151243, + u'email': u'owner@example.com', + u'name': u'owner' + }, + u'date': u'2017-03-16 20:00:41.000000000', + u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234', + u'message': u'PTAL', + }, + { + u'_revision_number': 2, + u'author': { + u'_account_id': 148512 , + u'email': u'reviewer@example.com', + u'name': u'reviewer' + }, + u'date': u'2017-03-17 05:19:37.500000000', + u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d4568', + u'message': u'Patch Set 2: Code-Review+1', + }, + ] + }) + ] * 2 + expected_comments_summary = [ + git_cl._CommentSummary( + message=u'Patch Set 1:\n\nDry run: CQ is trying da patch...', + date=datetime.datetime(2017, 3, 15, 20, 8, 45, 0), + disapproval=False, approval=False, sender=u'commit-bot@chromium.org'), + git_cl._CommentSummary( + message=u'PTAL', + date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0), + disapproval=False, approval=False, sender=u'owner@example.com'), + git_cl._CommentSummary( + message=u'Patch Set 2: Code-Review+1', + date=datetime.datetime(2017, 3, 17, 5, 19, 37, 500000), + disapproval=False, approval=False, sender=u'reviewer@example.com'), + ] + cl = git_cl.Changelist(codereview='gerrit', issue=1) + self.assertEqual(cl.GetCommentsSummary(), expected_comments_summary) + + with git_cl.gclient_utils.temporary_directory() as tempdir: + out_file = os.path.abspath(os.path.join(tempdir, 'out.json')) + self.assertEqual(0, git_cl.main(['comment', '--gerrit', '-i', '1', + '-j', out_file])) + with open(out_file) as f: + read = json.load(f) + self.assertEqual(len(read), 3) + self.assertEqual(read[0]['date'], u'2017-03-15 20:08:45.000000') + self.assertEqual(read[1]['date'], u'2017-03-16 20:00:41.000000') + self.assertEqual(read[2], { + u'date': u'2017-03-17 05:19:37.500000', + u'message': u'Patch Set 2: Code-Review+1', + u'approval': False, + u'disapproval': False, + u'sender': u'reviewer@example.com'}) if __name__ == '__main__': logging.basicConfig(