Browse Source

Filter file comments for existing files

Remove file comments for files which do not appear in the change
and leave a warning.

Change-Id: Ib5f3d5580a263c37812a797eae96b8470ce16d7b
changes/61/613161/2
James E. Blair 3 years ago
parent
commit
9efe95ff3c
4 changed files with 27 additions and 2 deletions
  1. +3
    -0
      tests/fixtures/config/gerrit-file-comments/git/org_project/playbooks/file-comments.yaml
  2. +7
    -1
      tests/unit/test_gerrit.py
  3. +2
    -1
      zuul/driver/gerrit/gerritreporter.py
  4. +15
    -0
      zuul/reporter/__init__.py

+ 3
- 0
tests/fixtures/config/gerrit-file-comments/git/org_project/playbooks/file-comments.yaml View File

@ -15,3 +15,6 @@
This is a much longer message.
With multiple paragraphs.
missingfile.txt:
- line: 89
message: does not exist

+ 7
- 1
tests/unit/test_gerrit.py View File

@ -192,7 +192,11 @@ class TestFileComments(AnsibleZuulTestCase):
tenant_config_file = 'config/gerrit-file-comments/main.yaml'
def test_file_comments(self):
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
A = self.fake_gerrit.addFakeChange(
'org/project', 'master', 'A',
files={'path/to/file.py': 'test1',
'otherfile.txt': 'test2',
})
A.addApproval('Code-Review', 2)
self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
self.waitUntilSettled()
@ -229,3 +233,5 @@ class TestFileComments(AnsibleZuulTestCase):
)
self.assertIn('expected a dictionary', A.messages[0],
"A should have a validation error reported")
self.assertIn('invalid file missingfile.txt', A.messages[0],
"A should have file error reported")

+ 2
- 1
zuul/driver/gerrit/gerritreporter.py View File

@ -50,8 +50,9 @@ class GerritReporter(BaseReporter):
self.connection.canonical_hostname:
return
message = self._formatItemReport(item)
comments = self._getFileComments(item)
self.filterComments(item, comments)
message = self._formatItemReport(item)
self.log.debug("Report change %s, params %s,"
" message: %s, comments: %s" %


+ 15
- 0
zuul/reporter/__init__.py View File

@ -82,6 +82,21 @@ class BaseReporter(object, metaclass=abc.ABCMeta):
end_line=mark.end_line,
end_character=mark.end_column)))
def filterComments(self, item, comments):
"""Filter comments for files in change
Remove any comments for files which do not appear in the
item's change. Leave warning messages if this happens.
:arg QueueItem item: The queue item
:arg dict comments: a file comments dictionary (modified in place)
"""
for fn in list(comments.keys()):
if fn not in item.change.files:
del comments[fn]
item.warning("Comments left for invalid file %s" % (fn,))
def _getFormatter(self):
format_methods = {
'start': self._formatItemReportStart,


Loading…
Cancel
Save