2019-08-05 16:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ContainerRepositoriesFinder
|
2019-11-04 10:07:36 -05:00
|
|
|
VALID_SUBJECTS = [Group, Project].freeze
|
|
|
|
|
2020-04-27 20:09:33 -04:00
|
|
|
def initialize(user:, subject:, params: {})
|
2019-11-04 10:07:36 -05:00
|
|
|
@user = user
|
|
|
|
@subject = subject
|
2020-04-27 20:09:33 -04:00
|
|
|
@params = params
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-11-04 10:07:36 -05:00
|
|
|
raise ArgumentError, "invalid subject_type" unless valid_subject_type?
|
|
|
|
return unless authorized?
|
|
|
|
|
2020-04-27 20:09:33 -04:00
|
|
|
repositories = @subject.is_a?(Project) ? project_repositories : group_repositories
|
2021-02-09 13:09:59 -05:00
|
|
|
repositories = filter_by_image_name(repositories)
|
|
|
|
sort(repositories)
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
def valid_subject_type?
|
|
|
|
VALID_SUBJECTS.include?(@subject.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_repositories
|
|
|
|
@subject.container_repositories
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
def group_repositories
|
|
|
|
ContainerRepository.for_group_and_its_subgroups(@subject)
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
2020-04-27 20:09:33 -04:00
|
|
|
def filter_by_image_name(repositories)
|
|
|
|
return repositories unless @params[:name]
|
|
|
|
|
|
|
|
repositories.search_by_name(@params[:name])
|
|
|
|
end
|
|
|
|
|
2021-02-09 13:09:59 -05:00
|
|
|
def sort(repositories)
|
|
|
|
return repositories unless @params[:sort]
|
|
|
|
|
|
|
|
repositories.order_by(@params[:sort])
|
|
|
|
end
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
def authorized?
|
|
|
|
Ability.allowed?(@user, :read_container_image, @subject)
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
end
|