Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create.rb « runner « ci « mutations « graphql « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7eca6c27d103312a167ccd8f185e929456189c72 (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
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

module Mutations
  module Ci
    module Runner
      class Create < BaseMutation
        graphql_name 'RunnerCreate'

        authorize :create_runner

        include Mutations::Ci::Runner::CommonMutationArguments

        argument :runner_type, ::Types::Ci::RunnerTypeEnum,
          required: true,
          description: 'Type of the runner to create.'

        argument :group_id, ::Types::GlobalIDType[Group],
          required: false,
          description: 'Global ID of the group that the runner is created in (valid only for group runner).'

        argument :project_id, ::Types::GlobalIDType[Project],
          required: false,
          description: 'Global ID of the project that the runner is created in (valid only for project runner).'

        field :runner,
          Types::Ci::RunnerType,
          null: true,
          description: 'Runner after mutation.'

        def ready?(**args)
          case args[:runner_type]
          when 'group_type'
            raise Gitlab::Graphql::Errors::ArgumentError, '`group_id` is missing' unless args[:group_id].present?
          when 'project_type'
            raise Gitlab::Graphql::Errors::ArgumentError, '`project_id` is missing' unless args[:project_id].present?
          end

          parse_gid(**args)

          check_feature_flag(**args)

          super
        end

        def resolve(**args)
          case args[:runner_type]
          when 'group_type', 'project_type'
            args[:scope] = authorized_find!(**args)
            args.except!(:group_id, :project_id)
          else
            raise_resource_not_available_error! unless current_user.can?(:create_instance_runner)
          end

          response = { runner: nil, errors: [] }
          result = ::Ci::Runners::CreateRunnerService.new(user: current_user, params: args).execute

          if result.success?
            response[:runner] = result.payload[:runner]
          else
            response[:errors] = result.errors
          end

          response
        end

        private

        def find_object(**args)
          obj = parse_gid(**args)

          GitlabSchema.find_by_gid(obj) if obj
        end

        def parse_gid(runner_type:, **args)
          case runner_type
          when 'group_type'
            GitlabSchema.parse_gid(args[:group_id], expected_type: ::Group)
          when 'project_type'
            GitlabSchema.parse_gid(args[:project_id], expected_type: ::Project)
          end
        end

        def check_feature_flag(**args)
          case args[:runner_type]
          when 'instance_type'
            if Feature.disabled?(:create_runner_workflow_for_admin, current_user)
              raise Gitlab::Graphql::Errors::ResourceNotAvailable,
                '`create_runner_workflow_for_admin` feature flag is disabled.'
            end
          when 'group_type'
            namespace = find_object(**args).sync
            if Feature.disabled?(:create_runner_workflow_for_namespace, namespace)
              raise Gitlab::Graphql::Errors::ResourceNotAvailable,
                '`create_runner_workflow_for_namespace` feature flag is disabled.'
            end
          when 'project_type'
            project = find_object(**args).sync
            if project && Feature.disabled?(:create_runner_workflow_for_namespace, project.namespace)
              raise Gitlab::Graphql::Errors::ResourceNotAvailable,
                '`create_runner_workflow_for_namespace` feature flag is disabled.'
            end
          end
        end
      end
    end
  end
end