Skip to content

Fix validation in UI when adding comments in MR diff

Hinam Mehra requested to merge 411329-fix-draft-note-ui-validation into master

What does this MR do and why?

  • Resolves sub-task 3 of https://gitlab.com/gitlab-org/gitlab/-/issues/411329
  • Displays error in the UI when there is an error adding a note in an MR diff - be it a draft note or a normal note
  • Currently, we don't have that many validations on notes but this is a blocker to start displaying if the comment being posted by a user is considered spam.
  • This MR only addresses adding comments, updating comments will be addressed in a separate MR

Screenshots or screen recordings

User Action Before After
Adding draft comment adding-draft-comment-before adding-draft-comment-after
Replying with a draft comment replying-draft-comment-before replying-draft-comment-after
Adding a comment adding-comment-before adding-comment-after
Adding a comment (image diff) adding-comment-image-diff-before adding-comment-image-diff-after

How to set up and validate locally

  1. Create a Merge Request with at least 1 file change and 1 image file added.
  2. Add a validator to the Note and DraftNote since we don't have a lot of validations for comments.
# Add it to class DraftNote < ApplicationRecord and 
# class Note < ApplicationRecord in models/

validate :test_ui_validation, on: [:create, :update]

def test_ui_validation
    if note.include? 'TEST_VALIDATION'
      self.errors.add(:base,  _('it failed validation'))
    end
end
  1. Try to add comment TEST_VALIDATION in the Changes section.

MR acceptance checklist

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

Related to #411329

Edited by Hinam Mehra

Merge request reports