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

listing.rb « catalog « ci « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3b18af8c3f81fa8757eb50ba9a83364e5f8fd35 (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
# frozen_string_literal: true

module Ci
  module Catalog
    class Listing
      # This class is the SSoT to displaying the list of resources in the
      # CI/CD Catalog given a namespace as a scope.
      # This model is not directly backed by a table and joins catalog resources
      # with projects to return relevant data.
      def initialize(namespace, current_user)
        raise ArgumentError, 'Namespace is not a root namespace' unless namespace.root?

        @namespace = namespace
        @current_user = current_user
      end

      def resources(sort: nil)
        case sort.to_s
        when 'name_desc' then all_resources.order_by_name_desc
        when 'name_asc' then all_resources.order_by_name_asc
        when 'latest_released_at_desc' then all_resources.order_by_latest_released_at_desc
        when 'latest_released_at_asc' then all_resources.order_by_latest_released_at_asc
        else
          all_resources.order_by_created_at_desc
        end
      end

      private

      attr_reader :namespace, :current_user

      def all_resources
        Ci::Catalog::Resource
          .joins(:project).includes(:project)
          .merge(projects_in_namespace_visible_to_user)
      end

      def projects_in_namespace_visible_to_user
        Project
          .in_namespace(namespace.self_and_descendant_ids)
          .public_or_visible_to_user(current_user, ::Gitlab::Access::DEVELOPER)
      end
    end
  end
end