summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/commits/create.rb
blob: 00ec64becc895887fd598ef800f40e1df7fc8b36 (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
# frozen_string_literal: true

module Mutations
  module Commits
    class Create < BaseMutation
      graphql_name 'CommitCreate'

      include FindsProject

      class UrlHelpers
        include GitlabRoutingHelper
        include Gitlab::Routing
      end

      argument :project_path, GraphQL::Types::ID,
               required: true,
               description: 'Project full path the branch is associated with.'

      argument :branch, GraphQL::Types::String,
               required: true,
               description: 'Name of the branch to commit into, it can be a new branch.'

      argument :start_branch, GraphQL::Types::String,
               required: false,
               description: 'If on a new branch, name of the original branch.'

      argument :message,
               GraphQL::Types::String,
               required: true,
               description: copy_field_description(Types::CommitType, :message)

      argument :actions,
               [Types::CommitActionType],
               required: true,
               description: 'Array of action hashes to commit as a batch.'

      field :commit_pipeline_path,
            GraphQL::Types::String,
            null: true,
            description: "ETag path for the commit's pipeline."

      field :commit,
            Types::CommitType,
            null: true,
            description: 'Commit after mutation.'

      field :content,
            [GraphQL::Types::String],
            null: true,
            description: 'Contents of the commit.'

      authorize :push_code

      def resolve(project_path:, branch:, message:, actions:, **args)
        project = authorized_find!(project_path)

        attributes = {
          commit_message: message,
          branch_name: branch,
          start_branch: args[:start_branch] || branch,
          actions: actions.map { |action| action.to_h }
        }

        result = ::Files::MultiService.new(project, current_user, attributes).execute

        {
          content: actions.pluck(:content),  # rubocop:disable CodeReuse/ActiveRecord because actions is an Array, not a Relation
          commit: (project.repository.commit(result[:result]) if result[:status] == :success),
          commit_pipeline_path: UrlHelpers.new.graphql_etag_pipeline_sha_path(result[:result]),
          errors: Array.wrap(result[:message])
        }
      end
    end
  end
end