Skip to content

Add note error handler as entries for JH overriding

What does this MR do and why?

In JH we need to block illegal content when users are editing/creating/replying a note and give them a error flash with useful message, so a encapsulated error message method like https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/assets/javascripts/notes/components/comment_form.vue#L190-196 in vue component will be helpful for JH to override it.

Following the same way app/assets/javascripts/notes/components/comment_form.vue did, this MR is encapsulating the error handler to make the feature mentioned can work when users are editing/replying a note.

related JH MR: gitlab-jh/gitlab!194 (merged)

related JH issue: https://gitlab.com/gitlab-jh/gitlab/-/issues/358

Screenshots or screen recordings

How to set up and validate locally

  1. Checkout the branch in gitlab-jh/gitlab!194 (merged)
  2. paste this patch
diff --git a/app/controllers/concerns/notes_actions.rb b/app/controllers/concerns/notes_actions.rb
--- a/app/controllers/concerns/notes_actions.rb	(revision 3d6143e41260cac47289c35fafa651cab3efaf91)
+++ b/app/controllers/concerns/notes_actions.rb	(date 1638244961543)
@@ -81,6 +81,15 @@
       return
     end
 
+    if @note.invalid?
+      json = {
+        valid: false,
+        errors: note.errors.full_messages.join('. ')
+      }
+      render json: json, status: :forbidden
+      return
+    end
+
     prepare_notes_for_rendering([@note])
 
     respond_to do |format|

diff --git a/app/models/note.rb b/app/models/note.rb
--- a/app/models/note.rb	(revision 3d6143e41260cac47289c35fafa651cab3efaf91)
+++ b/app/models/note.rb	(date 1638180191454)
@@ -101,6 +101,9 @@
 
   validate :does_not_exceed_notes_limit?, on: :create, unless: [:system?, :importing?]
 
+  validates_each :note do |record, attr, value|
+    record.errors.add(attr, 'has invalid content')
+  end
   # @deprecated attachments are handled by the Upload model.
   #
   # https://gitlab.com/gitlab-org/gitlab/-/issues/20830
  1. create/edit/reply the note anywhere

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Kun Qian

Merge request reports

Loading