Commit 6a675725 authored by Michael Kozono's avatar Michael Kozono

Merge branch '22388-limit-the-number-of-comments-on-a-noteable' into 'master'

Limit the number of comments on a noteable

See merge request gitlab-org/gitlab!18111
parents a90851c2 a3c4f961
......@@ -7,6 +7,8 @@ module Noteable
# avoiding n+1 queries and improving performance.
NoteableMeta = Struct.new(:user_notes_count)
MAX_NOTES_LIMIT = 5_000
class_methods do
# `Noteable` class names that support replying to individual notes.
def replyable_types
......
......@@ -104,6 +104,8 @@ class Note < ApplicationRecord
end
end
validate :does_not_exceed_notes_limit?, on: :create, unless: [:system?, :importing?]
# @deprecated attachments are handler by the MarkdownUploader
mount_uploader :attachment, AttachmentUploader
......@@ -525,6 +527,12 @@ class Note < ApplicationRecord
system_note_metadata&.cross_reference_types&.include?(system_note_metadata&.action)
end
def does_not_exceed_notes_limit?
return unless noteable
errors.add(:base, _('Maximum number of comments exceeded')) if noteable.notes.count >= Noteable::MAX_NOTES_LIMIT
end
end
Note.prepend_if_ee('EE::Note')
---
title: Limit the number of comments on an issue, MR, or commit
merge_request: 18111
author:
type: added
......@@ -24,6 +24,9 @@ You can also reply to a comment notification email to reply to the comment if
creates another standard comment. Replying to a threaded comment creates a reply in the thread. Email replies support
[Markdown] and [quick actions], just as if you replied from the web.
NOTE: **Note:**
There is a limit of 5,000 comments for every object.
## Resolvable comments and threads
> **Notes:**
......
......@@ -9941,6 +9941,9 @@ msgstr ""
msgid "Maximum job timeout has a value which could not be accepted"
msgstr ""
msgid "Maximum number of comments exceeded"
msgstr ""
msgid "Maximum number of mirrors that can be synchronizing at the same time."
msgstr ""
......
......@@ -70,6 +70,37 @@ describe Note do
is_expected.to be_valid
end
end
describe 'max notes limit' do
let_it_be(:noteable) { create(:issue) }
let_it_be(:existing_note) { create(:note, project: noteable.project, noteable: noteable) }
before do
stub_const('Noteable::MAX_NOTES_LIMIT', 1)
end
context 'when creating a system note' do
subject { build(:system_note, project: noteable.project, noteable: noteable) }
it { is_expected.to be_valid }
end
context 'when creating a user note' do
subject { build(:note, project: noteable.project, noteable: noteable) }
it { is_expected.not_to be_valid }
end
context 'when updating an existing note on a noteable that already exceeds the limit' do
subject { existing_note }
before do
create(:system_note, project: noteable.project, noteable: noteable)
end
it { is_expected.to be_valid }
end
end
end
describe "Commit notes" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment