Gerrit: add support for patchset level comments

Gerrit 3.3.0 introduced patchset level comments instead of change
messages [1]. Such comments have been added to comment-added event [2]

[1] https://www.gerritcodereview.com/3.3.html#new-features
[2] https://bugs.chromium.org/p/gerrit/issues/detail?id=13800

Change-Id: I5cb84fca5062a129ed461934bb564365371ecc54
This commit is contained in:
Guillaume Chauvel 2020-12-11 15:55:55 +01:00
parent f3e8c809dc
commit 33ac9f6472
5 changed files with 59 additions and 3 deletions

View File

@ -553,7 +553,23 @@ class FakeGerritChange(object):
"reason": ""}
return event
def getChangeCommentEvent(self, patchset):
def getChangeCommentEvent(self, patchset, comment=None,
patchsetcomment=None):
if comment is None and patchsetcomment is None:
comment = "Patch Set %d:\n\nThis is a comment" % patchset
elif comment:
comment = "Patch Set %d:\n\n%s" % (patchset, comment)
else: # patchsetcomment is not None
comment = "Patch Set %d:\n\n(1 comment)" % patchset
commentevent = {"comment": comment}
if patchsetcomment:
commentevent.update(
{'patchSetComments':
{"/PATCHSET_LEVEL": [{"message": patchsetcomment}]}
}
)
event = {"type": "comment-added",
"change": {"project": self.project,
"branch": self.branch,
@ -566,8 +582,8 @@ class FakeGerritChange(object):
"author": {"name": "User Name"},
"approvals": [{"type": "Code-Review",
"description": "Code-Review",
"value": "0"}],
"comment": "This is a comment"}
"value": "0"}]}
event.update(commentevent)
return event
def getChangeMergedEvent(self):

View File

@ -4,6 +4,8 @@
trigger:
gerrit:
- event: patchset-created
- event: comment-added
comment: '^(Patch Set [0-9]+:\n\n)?(?i:recheck)$'
success:
gerrit:
Verified: 1

View File

@ -228,6 +228,31 @@ class TestGerritWeb(ZuulTestCase):
self.assertEqual([], tested_change_ids)
def test_recheck(self):
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
self.waitUntilSettled()
self.assertEqual(3, len(self.history))
self.fake_gerrit.addEvent(A.getChangeCommentEvent(1))
self.waitUntilSettled()
self.assertEqual(3, len(self.history))
self.fake_gerrit.addEvent(A.getChangeCommentEvent(1,
'recheck'))
self.waitUntilSettled()
self.assertEqual(6, len(self.history))
self.fake_gerrit.addEvent(A.getChangeCommentEvent(1,
patchsetcomment='recheck'))
self.waitUntilSettled()
self.assertEqual(9, len(self.history))
self.fake_gerrit.addEvent(A.getChangeCommentEvent(1,
patchsetcomment='do not recheck'))
self.waitUntilSettled()
self.assertEqual(9, len(self.history))
class TestFileComments(AnsibleZuulTestCase):
config_file = 'zuul-gerrit-web.conf'

View File

@ -186,6 +186,13 @@ class GerritEventConnector(threading.Thread):
event.ref = patchset.get('ref')
event.approvals = data.get('approvals', [])
event.comment = data.get('comment')
patchsetcomments = data.get('patchSetComments', {}).get(
"/PATCHSET_LEVEL")
if patchsetcomments:
event.patchsetcomments = []
for patchsetcomment in patchsetcomments:
event.patchsetcomments.append(
patchsetcomment.get('message'))
refupdate = data.get('refUpdate')
if refupdate:
event.project_name = refupdate.get('project')

View File

@ -160,6 +160,7 @@ class GerritTriggerEvent(TriggerEvent):
self.approvals = []
self.uuid = None
self.scheme = None
self.patchsetcomments = None
def __repr__(self):
ret = '<GerritTriggerEvent %s %s' % (self.type,
@ -381,6 +382,11 @@ class GerritEventFilter(EventFilter, GerritApprovalFilter):
if (event.comment is not None and
comment_re.search(event.comment)):
matches_comment_re = True
if event.patchsetcomments is not None:
for comment in event.patchsetcomments:
if (comment is not None and
comment_re.search(comment)):
matches_comment_re = True
if self.comments and not matches_comment_re:
return False