Repository: fedora-infra/bodhi

Issue:
empty comment when accidentally hit "add comment & feedback" button
Hi,

The other day, I accidentally hit the "add comment & feedback" button, and it resulted in an empty comment with all karma 0s: https://bodhi.fedoraproject.org/updates/wget-1.19.2-1.fc25#comment-692806

I believe this should be disallowed since it just adds noise. Maybe check if the comments are empty and all karma are 0s, and show a popup dialog to notify when submitting?


Discussion:
I would like to work on this issue , any help given will be much appreciated
@mrjilc The comment handling code is [here](https://github.com/fedora-infra/bodhi/blob/3.2.0/bodhi/server/models.py#L2644-L2756).
I think that requiring a comment to be set is not the best thing. A user should be able to post only karma with an empty comment, or a comment with empty karma.

In my opinion a better way to proceed would be to check if both the comment field AND karma are void then warn the user and do nothing.

Resolving patch:
diff --git a/bodhi/server/models.py b/bodhi/server/models.py
--- a/bodhi/server/models.py
+++ b/bodhi/server/models.py
@@ -3012,12 +3012,21 @@ def comment(self, session, text, karma=0, author=None, anonymous=False,
         if not author:
             raise ValueError('You must provide a comment author')
 
-        caveats = []
-
         # Listify these
         bug_feedback = bug_feedback or []
         testcase_feedback = testcase_feedback or []
 
+        got_feedback = False
+        for feedback_dict in (bug_feedback + testcase_feedback):
+            if feedback_dict['karma'] != 0:
+                got_feedback = True
+                break
+
+        if (not text and not karma and not karma_critpath and not got_feedback):
+            raise ValueError('You must provide either some text or feedback')
+
+        caveats = []
+
         if self.user.name == author:
             if karma != 0:
                 karma = 0
diff --git a/bodhi/server/services/comments.py b/bodhi/server/services/comments.py
--- a/bodhi/server/services/comments.py
+++ b/bodhi/server/services/comments.py
@@ -228,6 +228,9 @@ def new_comment(request):
     try:
         comment, caveats = update.comment(
             session=request.db, author=author, anonymous=anonymous, **data)
+    except ValueError as e:
+        request.errors.add('body', 'comment', str(e))
+        return
     except Exception as e:
         log.exception(e)
         request.errors.add('body', 'comment', 'Unable to create comment')


Test patch:
diff --git a/bodhi/tests/server/services/test_comments.py b/bodhi/tests/server/services/test_comments.py
--- a/bodhi/tests/server/services/test_comments.py
+++ b/bodhi/tests/server/services/test_comments.py
@@ -260,6 +260,19 @@ def test_anonymous_commenting_with_no_author(self):
         self.assertEqual(res.json_body['errors'][0]['description'],
                          "You must provide an author")
 
+    def test_empty_comment(self):
+        """Ensure that a comment without text or feedback is not permitted."""
+        comment = self.make_comment(text='')
+        comment['bug_feedback.0.bug_id'] = 12345
+        comment['bug_feedback.0.karma'] = 0
+        comment['testcase_feedback.0.testcase_name'] = "Wat"
+        comment['testcase_feedback.0.karma'] = 0
+        res = self.app.post_json('/comments/', comment, status=400)
+
+        self.assertEqual(res.json_body['status'], 'error')
+        self.assertEqual(res.json_body['errors'][0]['description'],
+                         u'You must provide either some text or feedback')
+
     @mock.patch('bodhi.server.services.comments.Update.comment',
                 side_effect=IOError('IOError. oops!'))
     def test_unexpected_exception(self, exception):
diff --git a/bodhi/tests/server/test_models.py b/bodhi/tests/server/test_models.py
--- a/bodhi/tests/server/test_models.py
+++ b/bodhi/tests/server/test_models.py
@@ -3276,6 +3276,13 @@ def test_comment_no_author(self):
 
         self.assertEqual(str(exc.exception), 'You must provide a comment author')
 
+    def test_comment_empty(self):
+        """A comment with no text or feedback should raise a ValueError."""
+        with self.assertRaises(ValueError) as exc:
+            self.obj.comment(self.db, '', author=u'bowlofeggs')
+
+        self.assertEqual(str(exc.exception), 'You must provide either some text or feedback')
+
     def test_get_url(self):
         self.assertEqual(self.obj.get_url(), u'updates/TurboGears-1.0.8-3.fc11')
         idx = 'a3bbe1a8f2'