summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/snippets/create.rb
blob: 7f2dd448b8b14c45fd94d97503cb0a213f91ce5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

module Mutations
  module Snippets
    class Create < BaseMutation
      include ServiceCompatibility
      include CanMutateSpammable

      authorize :create_snippet

      graphql_name 'CreateSnippet'

      field :snippet,
            Types::SnippetType,
            null: true,
            description: 'The snippet after mutation.'

      argument :title, GraphQL::STRING_TYPE,
               required: true,
               description: 'Title of the snippet.'

      argument :description, GraphQL::STRING_TYPE,
               required: false,
               description: 'Description of the snippet.'

      argument :visibility_level, Types::VisibilityLevelsEnum,
               description: 'The visibility level of the snippet.',
               required: true

      argument :project_path, GraphQL::ID_TYPE,
               required: false,
               description: 'The project full path the snippet is associated with.'

      argument :uploaded_files, [GraphQL::STRING_TYPE],
               required: false,
               description: 'The paths to files uploaded in the snippet description.'

      argument :blob_actions, [Types::Snippets::BlobActionInputType],
               description: 'Actions to perform over the snippet repository and blobs.',
               required: false

      def resolve(project_path: nil, **args)
        if project_path.present?
          project = authorized_find!(project_path)
        else
          authorize!(:global)
        end

        process_args_for_params!(args)

        service_response = ::Snippets::CreateService.new(project, current_user, args).execute

        # Only when the user is not an api user and the operation was successful
        if !api_user? && service_response.success?
          ::Gitlab::UsageDataCounters::EditorUniqueCounter.track_snippet_editor_edit_action(author: current_user)
        end

        snippet = service_response.payload[:snippet]
        with_spam_action_response_fields(snippet) do
          {
            snippet: service_response.success? ? snippet : nil,
            errors: errors_on_object(snippet)
          }
        end
      end

      private

      def find_object(full_path)
        Project.find_by_full_path(full_path)
      end

      # process_args_for_params!(args)    -> nil
      #
      # Modifies/adds/deletes mutation resolve args as necessary to be passed as params to service layer.
      def process_args_for_params!(args)
        convert_blob_actions_to_snippet_actions!(args)

        # We need to rename `uploaded_files` into `files` because
        # it's the expected key param
        args[:files] = args.delete(:uploaded_files)

        if Feature.enabled?(:snippet_spam)
          args.merge!(additional_spam_params)
        else
          args[:disable_spam_action_service] = true
        end

        # Return nil to make it explicit that this method is mutating the args parameter, and that
        # the return value is not relevant and is not to be used.
        nil
      end
    end
  end
end