summaryrefslogtreecommitdiff
path: root/app/finders/container_repositories_finder.rb
blob: 5109efb361b3ee04584942b5b215c8a05b10f81a (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

class ContainerRepositoriesFinder
  VALID_SUBJECTS = [Group, Project].freeze

  def initialize(user:, subject:, params: {})
    @user = user
    @subject = subject
    @params = params
  end

  def execute
    raise ArgumentError, "invalid subject_type" unless valid_subject_type?
    return unless authorized?

    repositories = @subject.is_a?(Project) ? project_repositories : group_repositories
    filter_by_image_name(repositories)
  end

  private

  def valid_subject_type?
    VALID_SUBJECTS.include?(@subject.class)
  end

  def project_repositories
    return unless @subject.container_registry_enabled

    @subject.container_repositories
  end

  def group_repositories
    ContainerRepository.for_group_and_its_subgroups(@subject)
  end

  def filter_by_image_name(repositories)
    return repositories unless @params[:name]

    repositories.search_by_name(@params[:name])
  end

  def authorized?
    Ability.allowed?(@user, :read_container_image, @subject)
  end
end