2019-07-01 04:34:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Notes
|
|
|
|
module Create
|
|
|
|
# This is a Base class for the Note creation Mutations and is not
|
|
|
|
# mounted as a GraphQL mutation itself.
|
|
|
|
class Base < Mutations::Notes::Base
|
|
|
|
authorize :create_note
|
|
|
|
|
|
|
|
argument :noteable_id,
|
2020-10-08 03:08:39 +00:00
|
|
|
::Types::GlobalIDType[::Noteable],
|
2019-07-01 04:34:34 +00:00
|
|
|
required: true,
|
2021-08-18 00:11:18 +00:00
|
|
|
description: 'Global ID of the resource to add a note to.'
|
2019-07-01 04:34:34 +00:00
|
|
|
|
|
|
|
argument :body,
|
2021-07-22 21:09:40 +00:00
|
|
|
GraphQL::Types::String,
|
2019-07-01 04:34:34 +00:00
|
|
|
required: true,
|
|
|
|
description: copy_field_description(Types::Notes::NoteType, :body)
|
|
|
|
|
2020-07-15 09:09:34 +00:00
|
|
|
argument :confidential,
|
2021-07-22 21:09:40 +00:00
|
|
|
GraphQL::Types::Boolean,
|
2020-07-15 09:09:34 +00:00
|
|
|
required: false,
|
2022-08-15 12:11:43 +00:00
|
|
|
description: 'Confidentiality flag of a note. Default is false.',
|
|
|
|
deprecated: { reason: :renamed, replacement: 'internal', milestone: '15.3' }
|
|
|
|
|
|
|
|
argument :internal,
|
|
|
|
GraphQL::Types::Boolean,
|
|
|
|
required: false,
|
|
|
|
description: 'Internal flag for a note. Default is false.'
|
2020-07-15 09:09:34 +00:00
|
|
|
|
2019-07-01 04:34:34 +00:00
|
|
|
def resolve(args)
|
|
|
|
noteable = authorized_find!(id: args[:noteable_id])
|
2021-02-09 21:09:19 +00:00
|
|
|
verify_rate_limit!(current_user)
|
2019-07-01 04:34:34 +00:00
|
|
|
|
|
|
|
note = ::Notes::CreateService.new(
|
|
|
|
noteable.project,
|
|
|
|
current_user,
|
|
|
|
create_note_params(noteable, args)
|
|
|
|
).execute
|
|
|
|
|
|
|
|
{
|
|
|
|
note: (note if note.persisted?),
|
|
|
|
errors: errors_on_object(note)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-04 17:42:48 +00:00
|
|
|
private
|
|
|
|
|
2020-10-08 03:08:39 +00:00
|
|
|
def find_object(id:)
|
|
|
|
GitlabSchema.find_by_gid(id)
|
|
|
|
end
|
|
|
|
|
2019-07-01 04:34:34 +00:00
|
|
|
def create_note_params(noteable, args)
|
|
|
|
{
|
|
|
|
noteable: noteable,
|
2020-07-15 09:09:34 +00:00
|
|
|
note: args[:body],
|
2022-08-15 12:11:43 +00:00
|
|
|
internal: args[:internal] || args[:confidential]
|
2019-07-01 04:34:34 +00:00
|
|
|
}
|
|
|
|
end
|
2021-02-09 21:09:19 +00:00
|
|
|
|
|
|
|
def verify_rate_limit!(current_user)
|
2021-02-11 12:08:52 +00:00
|
|
|
return unless rate_limit_throttled?
|
2021-02-09 21:09:19 +00:00
|
|
|
|
|
|
|
raise Gitlab::Graphql::Errors::ResourceNotAvailable,
|
|
|
|
'This endpoint has been requested too many times. Try again later.'
|
|
|
|
end
|
2021-02-11 12:08:52 +00:00
|
|
|
|
|
|
|
def rate_limit_throttled?
|
|
|
|
rate_limiter = ::Gitlab::ApplicationRateLimiter
|
|
|
|
allowlist = Gitlab::CurrentSettings.current_application_settings.notes_create_limit_allowlist
|
|
|
|
|
|
|
|
rate_limiter.throttled?(:notes_create, scope: [current_user], users_allowlist: allowlist)
|
|
|
|
end
|
2019-07-01 04:34:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|