diff options
Diffstat (limited to 'app/graphql/mutations')
-rw-r--r-- | app/graphql/mutations/notes/update.rb | 38 | ||||
-rw-r--r-- | app/graphql/mutations/notes/update/base.rb | 48 | ||||
-rw-r--r-- | app/graphql/mutations/notes/update/image_diff_note.rb | 60 | ||||
-rw-r--r-- | app/graphql/mutations/notes/update/note.rb | 22 |
4 files changed, 130 insertions, 38 deletions
diff --git a/app/graphql/mutations/notes/update.rb b/app/graphql/mutations/notes/update.rb deleted file mode 100644 index ebf57b800c0..00000000000 --- a/app/graphql/mutations/notes/update.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -module Mutations - module Notes - class Update < Base - graphql_name 'UpdateNote' - - authorize :admin_note - - argument :id, - GraphQL::ID_TYPE, - required: true, - description: 'The global id of the note to update' - - argument :body, - GraphQL::STRING_TYPE, - required: true, - description: copy_field_description(Types::Notes::NoteType, :body) - - def resolve(args) - note = authorized_find!(id: args[:id]) - - check_object_is_note!(note) - - note = ::Notes::UpdateService.new( - note.project, - current_user, - { note: args[:body] } - ).execute(note) - - { - note: note.reset, - errors: errors_on_object(note) - } - end - end - end -end diff --git a/app/graphql/mutations/notes/update/base.rb b/app/graphql/mutations/notes/update/base.rb new file mode 100644 index 00000000000..9a53337f253 --- /dev/null +++ b/app/graphql/mutations/notes/update/base.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module Mutations + module Notes + module Update + # This is a Base class for the Note update mutations and is not + # mounted as a GraphQL mutation itself. + class Base < Mutations::Notes::Base + authorize :admin_note + + argument :id, + GraphQL::ID_TYPE, + required: true, + description: 'The global id of the note to update' + + def resolve(args) + note = authorized_find!(id: args[:id]) + + pre_update_checks!(note, args) + + updated_note = ::Notes::UpdateService.new( + note.project, + current_user, + note_params(note, args) + ).execute(note) + + # It's possible for updated_note to be `nil`, in the situation + # where the note is deleted within `Notes::UpdateService` due to + # the body of the note only containing Quick Actions. + { + note: updated_note&.reset, + errors: updated_note ? errors_on_object(updated_note) : [] + } + end + + private + + def pre_update_checks!(_note, _args) + raise NotImplementedError + end + + def note_params(_note, args) + { note: args[:body] }.compact + end + end + end + end +end diff --git a/app/graphql/mutations/notes/update/image_diff_note.rb b/app/graphql/mutations/notes/update/image_diff_note.rb new file mode 100644 index 00000000000..7aad3af1e04 --- /dev/null +++ b/app/graphql/mutations/notes/update/image_diff_note.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module Mutations + module Notes + module Update + class ImageDiffNote < Mutations::Notes::Update::Base + graphql_name 'UpdateImageDiffNote' + + argument :body, + GraphQL::STRING_TYPE, + required: false, + description: copy_field_description(Types::Notes::NoteType, :body) + + argument :position, + Types::Notes::UpdateDiffImagePositionInputType, + required: false, + description: copy_field_description(Types::Notes::NoteType, :position) + + def ready?(**args) + # As both arguments are optional, validate here that one of the + # arguments are present. + # + # This may be able to be done using InputUnions in the future + # if this RFC is merged: + # https://github.com/graphql/graphql-spec/blob/master/rfcs/InputUnion.md + if args.values_at(:body, :position).compact.blank? + raise Gitlab::Graphql::Errors::ArgumentError, + 'body or position arguments are required' + end + + super(args) + end + + private + + def pre_update_checks!(note, args) + unless note.is_a?(DiffNote) && note.position.on_image? + raise Gitlab::Graphql::Errors::ResourceNotAvailable, + 'Resource is not an ImageDiffNote' + end + end + + def note_params(note, args) + super(note, args).merge( + position: position_params(note, args) + ).compact + end + + def position_params(note, args) + new_position = args[:position]&.to_h&.compact + return unless new_position + + original_position = note.position.to_h + + Gitlab::Diff::Position.new(original_position.merge(new_position)) + end + end + end + end +end diff --git a/app/graphql/mutations/notes/update/note.rb b/app/graphql/mutations/notes/update/note.rb new file mode 100644 index 00000000000..03a174fc8d9 --- /dev/null +++ b/app/graphql/mutations/notes/update/note.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Mutations + module Notes + module Update + class Note < Mutations::Notes::Update::Base + graphql_name 'UpdateNote' + + argument :body, + GraphQL::STRING_TYPE, + required: true, + description: copy_field_description(Types::Notes::NoteType, :body) + + private + + def pre_update_checks!(note, _args) + check_object_is_note!(note) + end + end + end + end +end |