diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-17 15:09:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-17 15:09:20 +0300 |
commit | b84eeb256c4a780d902faee1f99ca9a711b3214a (patch) | |
tree | 32918aadbea9210eace50efbce9afbfb8cd3ba84 /app/graphql | |
parent | 53ae6b7e3f83591ad251a3f771f5bf3b8cf087ba (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/graphql')
-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 | ||||
-rw-r--r-- | app/graphql/types/mutation_type.rb | 11 | ||||
-rw-r--r-- | app/graphql/types/notes/diff_position_type.rb | 4 | ||||
-rw-r--r-- | app/graphql/types/notes/update_diff_image_position_input_type.rb | 29 |
7 files changed, 170 insertions, 42 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 diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index fc0a2a099df..ee0f4dbb05f 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -4,7 +4,7 @@ module Types class MutationType < BaseObject include Gitlab::Graphql::MountMutation - graphql_name "Mutation" + graphql_name 'Mutation' mount_mutation Mutations::AwardEmojis::Add mount_mutation Mutations::AwardEmojis::Remove @@ -20,7 +20,14 @@ module Types mount_mutation Mutations::Notes::Create::Note, calls_gitaly: true mount_mutation Mutations::Notes::Create::DiffNote, calls_gitaly: true mount_mutation Mutations::Notes::Create::ImageDiffNote, calls_gitaly: true - mount_mutation Mutations::Notes::Update + mount_mutation Mutations::Notes::Update::Note, + description: 'Updates a Note. If the body of the Note contains only quick actions, ' \ + 'the Note will be destroyed during the update, and no Note will be ' \ + 'returned' + mount_mutation Mutations::Notes::Update::ImageDiffNote, + description: 'Updates a DiffNote on an image (a `Note` where the `position.positionType` is `"image"`). ' \ + 'If the body of the Note contains only quick actions, the Note will be ' \ + 'destroyed during the update, and no Note will be returned' mount_mutation Mutations::Notes::Destroy mount_mutation Mutations::Todos::MarkDone mount_mutation Mutations::Todos::Restore diff --git a/app/graphql/types/notes/diff_position_type.rb b/app/graphql/types/notes/diff_position_type.rb index 654562da0a7..cc00feba2e6 100644 --- a/app/graphql/types/notes/diff_position_type.rb +++ b/app/graphql/types/notes/diff_position_type.rb @@ -29,10 +29,10 @@ module Types # Fields for image positions field :x, GraphQL::INT_TYPE, null: true, - description: 'X position on which the comment was made', + description: 'X position of the note', resolve: -> (position, _args, _ctx) { position.x if position.on_image? } field :y, GraphQL::INT_TYPE, null: true, - description: 'Y position on which the comment was made', + description: 'Y position of the note', resolve: -> (position, _args, _ctx) { position.y if position.on_image? } field :width, GraphQL::INT_TYPE, null: true, description: 'Total width of the image', diff --git a/app/graphql/types/notes/update_diff_image_position_input_type.rb b/app/graphql/types/notes/update_diff_image_position_input_type.rb new file mode 100644 index 00000000000..af99764f9f2 --- /dev/null +++ b/app/graphql/types/notes/update_diff_image_position_input_type.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Types + module Notes + # InputType used for updateImageDiffNote mutation. + # + # rubocop: disable Graphql/AuthorizeTypes + class UpdateDiffImagePositionInputType < BaseInputObject + graphql_name 'UpdateDiffImagePositionInput' + + argument :x, GraphQL::INT_TYPE, + required: false, + description: copy_field_description(Types::Notes::DiffPositionType, :x) + + argument :y, GraphQL::INT_TYPE, + required: false, + description: copy_field_description(Types::Notes::DiffPositionType, :y) + + argument :width, GraphQL::INT_TYPE, + required: false, + description: copy_field_description(Types::Notes::DiffPositionType, :width) + + argument :height, GraphQL::INT_TYPE, + required: false, + description: copy_field_description(Types::Notes::DiffPositionType, :height) + end + # rubocop: enable Graphql/AuthorizeTypes + end +end |