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
|
2021-03-11 19:13:27 +05:30
|
|
|
repositories = filter_by_image_name(repositories)
|
|
|
|
sort(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
|
|
|
|
@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
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def sort(repositories)
|
|
|
|
return repositories unless @params[:sort]
|
|
|
|
|
|
|
|
repositories.order_by(@params[:sort])
|
|
|
|
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
|