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

resource_type.rb « catalog « ci « types « graphql « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 119313ae52b33fab5cf25f40dac5be05652f189e (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
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

module Types
  module Ci
    module Catalog
      # rubocop: disable Graphql/AuthorizeTypes
      class ResourceType < BaseObject
        graphql_name 'CiCatalogResource'

        connection_type_class Types::CountableConnectionType

        field :open_issues_count, GraphQL::Types::Int, null: false,
          description: 'Count of open issues that belong to the the catalog resource.',
          alpha: { milestone: '16.3' }

        field :open_merge_requests_count, GraphQL::Types::Int, null: false,
          description: 'Count of open merge requests that belong to the the catalog resource.',
          alpha: { milestone: '16.3' }

        field :id, GraphQL::Types::ID, null: false, description: 'ID of the catalog resource.',
          alpha: { milestone: '15.11' }

        field :name, GraphQL::Types::String, null: true, description: 'Name of the catalog resource.',
          alpha: { milestone: '15.11' }

        field :description, GraphQL::Types::String, null: true, description: 'Description of the catalog resource.',
          alpha: { milestone: '15.11' }

        field :icon, GraphQL::Types::String, null: true, description: 'Icon for the catalog resource.',
          method: :avatar_path, alpha: { milestone: '15.11' }

        field :web_path, GraphQL::Types::String, null: true, description: 'Web path of the catalog resource.',
          alpha: { milestone: '16.1' }

        field :versions, Types::ReleaseType.connection_type, null: true,
          description: 'Versions of the catalog resource. This field can only be ' \
                       'resolved for one catalog resource in any single request.',
          resolver: Resolvers::Ci::Catalog::VersionsResolver,
          alpha: { milestone: '16.2' }

        field :latest_version, Types::ReleaseType, null: true, description: 'Latest version of the catalog resource.',
          alpha: { milestone: '16.1' }

        field :latest_released_at, Types::TimeType, null: true,
          description: "Release date of the catalog resource's latest version.",
          alpha: { milestone: '16.5' }

        field :star_count, GraphQL::Types::Int, null: false,
          description: 'Number of times the catalog resource has been starred.',
          alpha: { milestone: '16.1' }

        field :forks_count, GraphQL::Types::Int, null: false, calls_gitaly: true,
          description: 'Number of times the catalog resource has been forked.',
          alpha: { milestone: '16.1' }

        field :root_namespace, Types::NamespaceType, null: true,
          description: 'Root namespace of the catalog resource.',
          alpha: { milestone: '16.1' }

        markdown_field :readme_html, null: false,
          alpha: { milestone: '16.1' }

        def open_issues_count
          BatchLoader::GraphQL.wrap(object.project.open_issues_count)
        end

        def open_merge_requests_count
          BatchLoader::GraphQL.wrap(object.project.open_merge_requests_count)
        end

        def web_path
          ::Gitlab::Routing.url_helpers.project_path(object.project)
        end

        def latest_version
          BatchLoader::GraphQL.for(object.project).batch do |projects, loader|
            latest_releases = ReleasesFinder.new(projects, current_user, latest: true).execute

            latest_releases.index_by(&:project).each do |project, latest_release|
              loader.call(project, latest_release)
            end
          end
        end

        def forks_count
          BatchLoader::GraphQL.wrap(object.forks_count)
        end

        def root_namespace
          BatchLoader::GraphQL.for(object.project_id).batch do |project_ids, loader|
            projects = Project.id_in(project_ids)

            # This preloader uses traversal_ids to obtain Group-type root namespaces.
            # It also preloads each project's immediate parent namespace, which effectively
            # preloads the User-type root namespaces since they cannot be nested (parent == root).
            Preloaders::ProjectRootAncestorPreloader.new(projects, :group).execute
            root_namespaces = projects.map(&:root_ancestor)

            # NamespaceType requires the `:read_namespace` ability. We must preload the policy for
            # Group-type namespaces to avoid N+1 queries caused by the authorization requests.
            group_root_namespaces = root_namespaces.select { |n| n.type == ::Group.sti_name }
            Preloaders::GroupPolicyPreloader.new(group_root_namespaces, current_user).execute

            # For User-type namespaces, the authorization request requires preloading the owner objects.
            user_root_namespaces = root_namespaces.select { |n| n.type == ::Namespaces::UserNamespace.sti_name }
            ActiveRecord::Associations::Preloader.new(records: user_root_namespaces, associations: :owner).call

            projects.each { |project| loader.call(project.id, project.root_ancestor) }
          end
        end

        def readme_html_resolver
          markdown_context = context.to_h.dup.merge(project: object.project)
          ::MarkupHelper.markdown(object.project.repository.readme&.data, markdown_context)
        end
      end
      # rubocop: enable Graphql/AuthorizeTypes
    end
  end
end