debian-mirror-gitlab/app/finders/container_repositories_finder.rb

46 lines
1,015 B
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
class ContainerRepositoriesFinder
2019-12-26 22:10:19 +05:30
VALID_SUBJECTS = [Group, Project].freeze
2020-05-24 23:13:21 +05:30
def initialize(user:, subject:, params: {})
2019-12-26 22:10:19 +05:30
@user = user
@subject = subject
2020-05-24 23:13:21 +05:30
@params = params
2019-10-12 21:52:04 +05:30
end
def execute
2019-12-26 22:10:19 +05:30
raise ArgumentError, "invalid subject_type" unless valid_subject_type?
return unless authorized?
2020-05-24 23:13:21 +05:30
repositories = @subject.is_a?(Project) ? project_repositories : group_repositories
filter_by_image_name(repositories)
2019-10-12 21:52:04 +05:30
end
private
2019-12-26 22:10:19 +05:30
def valid_subject_type?
VALID_SUBJECTS.include?(@subject.class)
end
def project_repositories
return unless @subject.container_registry_enabled
2019-10-12 21:52:04 +05:30
2019-12-26 22:10:19 +05:30
@subject.container_repositories
2019-10-12 21:52:04 +05:30
end
2019-12-26 22:10:19 +05:30
def group_repositories
ContainerRepository.for_group_and_its_subgroups(@subject)
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
def filter_by_image_name(repositories)
return repositories unless @params[:name]
repositories.search_by_name(@params[:name])
end
2019-12-26 22:10:19 +05:30
def authorized?
Ability.allowed?(@user, :read_container_image, @subject)
2019-10-12 21:52:04 +05:30
end
end