File: labels_resolver.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 (69 lines) | stat: -rw-r--r-- 2,120 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

module Resolvers
  class LabelsResolver < BaseResolver
    include Gitlab::Graphql::Authorize::AuthorizeResource

    authorize :read_label

    type Types::LabelType.connection_type, null: true

    argument :title, GraphQL::Types::String,
      required: false,
      description: 'Exact match on title. Cannot be used with `searchTerm`. ' \
        '`searchIn` will be ignored if `title` argument is provided.'

    argument :search_term, GraphQL::Types::String,
      required: false,
      description: 'Search term to find labels with.'

    argument :search_in, [Types::Issuables::Labels::SearchFieldListEnum],
      default_value: [:title, :description],
      description: 'Specify which fields to search in. Ignored if using `title`.'

    argument :include_ancestor_groups, GraphQL::Types::Boolean,
      required: false,
      description: 'Include labels from ancestor groups.',
      default_value: false

    before_connection_authorization do |nodes, current_user|
      Preloaders::LabelsPreloader.new(nodes, current_user).preload_all
    end

    validates mutually_exclusive: [:search_term, :title]

    def resolve(**args)
      return Label.none if parent.nil?

      authorize!(parent)

      # LabelsFinder uses `search` param, so we transform `search_term` into `search`
      args[:search] = args.delete(:search_term)

      # If `title` is used, remove `search_in`
      args.delete(:search_in) if args[:title]

      # Optimization:
      # Rely on the LabelsPreloader rather than the default parent record preloading in the
      # finder because LabelsPreloader preloads more associations which are required for the
      # permission check.
      LabelsFinder.new(current_user, parent_param.merge(args)).execute
    end

    private

    def parent
      object.respond_to?(:sync) ? object.sync : object
    end

    def parent_param
      key = case parent
            when Group then :group
            when Project then :project
            else raise "Unexpected parent type: #{parent.class}"
            end

      { "#{key}": parent }
    end
  end
end