File: bulk_delete.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (54 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download
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
# frozen_string_literal: true

module Mutations
  module Ci
    module Runner
      class BulkDelete < BaseMutation
        graphql_name 'BulkRunnerDelete'

        RunnerID = ::Types::GlobalIDType[::Ci::Runner]

        argument :ids, [RunnerID],
          required: false,
          description: 'IDs of the runners to delete.'

        field :deleted_count,
          ::GraphQL::Types::Int,
          null: true,
          description: 'Number of records effectively deleted. ' \
            'Only present if operation was performed synchronously.'

        field :deleted_ids,
          [RunnerID],
          null: true,
          description: 'IDs of records effectively deleted. ' \
            'Only present if operation was performed synchronously.'

        def resolve(**runner_attrs)
          if ids = runner_attrs[:ids]
            runner_ids = model_ids_of(ids)
            runners = find_all_runners_by_ids(runner_ids)

            result = ::Ci::Runners::BulkDeleteRunnersService.new(runners: runners, current_user: current_user).execute
            result.payload.slice(:deleted_count, :deleted_ids, :errors)
          else
            { errors: [] }
          end
        end

        private

        def model_ids_of(global_ids)
          global_ids.filter_map { |gid| gid.model_id.to_i }
        end

        def find_all_runners_by_ids(ids)
          return ::Ci::Runner.none if ids.blank?

          limit = ::Ci::Runners::BulkDeleteRunnersService::RUNNER_LIMIT
          ::Ci::Runner.id_in(ids).limit(limit + 1)
        end
      end
    end
  end
end