summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/notes/reposition_image_diff_note.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-11-19 08:27:35 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-11-19 08:27:35 +0000
commit7e9c479f7de77702622631cff2628a9c8dcbc627 (patch)
treec8f718a08e110ad7e1894510980d2155a6549197 /app/graphql/mutations/notes/reposition_image_diff_note.rb
parente852b0ae16db4052c1c567d9efa4facc81146e88 (diff)
downloadgitlab-ce-7e9c479f7de77702622631cff2628a9c8dcbc627.tar.gz
Add latest changes from gitlab-org/gitlab@13-6-stable-eev13.6.0-rc42
Diffstat (limited to 'app/graphql/mutations/notes/reposition_image_diff_note.rb')
-rw-r--r--app/graphql/mutations/notes/reposition_image_diff_note.rb65
1 files changed, 65 insertions, 0 deletions
diff --git a/app/graphql/mutations/notes/reposition_image_diff_note.rb b/app/graphql/mutations/notes/reposition_image_diff_note.rb
new file mode 100644
index 00000000000..0d88bcd9a30
--- /dev/null
+++ b/app/graphql/mutations/notes/reposition_image_diff_note.rb
@@ -0,0 +1,65 @@
+# frozen_string_literal: true
+
+module Mutations
+ module Notes
+ # This mutation differs from the update note mutations as it checks the
+ # `reposition_note` permission, and doesn't allow updating a note's `body`.
+ class RepositionImageDiffNote < Mutations::Notes::Base
+ graphql_name 'RepositionImageDiffNote'
+
+ description 'Repositions a DiffNote on an image (a `Note` where the `position.positionType` is `"image"`)'
+
+ authorize :reposition_note
+
+ argument :id,
+ Types::GlobalIDType[DiffNote],
+ loads: Types::Notes::NoteType,
+ as: :note,
+ required: true,
+ description: 'The global id of the DiffNote to update'
+
+ argument :position,
+ Types::Notes::UpdateDiffImagePositionInputType,
+ required: true,
+ description: copy_field_description(Types::Notes::NoteType, :position)
+
+ def resolve(note:, position:)
+ authorize!(note)
+
+ pre_update_checks!(note, position)
+
+ updated_note = ::Notes::UpdateService.new(
+ note.project,
+ current_user,
+ note_params(note.position, position)
+ ).execute(note)
+
+ {
+ note: updated_note.reset,
+ errors: errors_on_object(updated_note)
+ }
+ end
+
+ private
+
+ # An ImageDiffNote does not exist as a class itself, but is instead
+ # just a `DiffNote` with a particular kind of `Gitlab::Diff::Position`.
+ # In addition to accepting a `DiffNote` Global ID we also need to
+ # perform this check.
+ def pre_update_checks!(note, position)
+ unless note.position&.on_image?
+ raise Gitlab::Graphql::Errors::ResourceNotAvailable,
+ 'Resource is not an ImageDiffNote'
+ end
+ end
+
+ def note_params(old_position, new_position)
+ position = old_position.to_h.merge(new_position)
+
+ {
+ position: Gitlab::Diff::Position.new(position)
+ }
+ end
+ end
+ end
+end