2020-03-13 20:09:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
class JobsFinder
|
|
|
|
include Gitlab::Allowable
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
def initialize(current_user:, pipeline: nil, project: nil, params: {}, type: ::Ci::Build)
|
|
|
|
@pipeline = pipeline
|
2020-03-13 20:09:30 -04:00
|
|
|
@current_user = current_user
|
|
|
|
@project = project
|
|
|
|
@params = params
|
2020-09-11 08:08:50 -04:00
|
|
|
@type = type
|
|
|
|
raise ArgumentError 'type must be a subclass of Ci::Processable' unless type < ::Ci::Processable
|
2020-03-13 20:09:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
builds = init_collection.order_id_desc
|
|
|
|
filter_by_scope(builds)
|
|
|
|
rescue Gitlab::Access::AccessDeniedError
|
2020-09-11 08:08:50 -04:00
|
|
|
type.none
|
2020-03-13 20:09:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
attr_reader :current_user, :pipeline, :project, :params, :type
|
2020-03-13 20:09:30 -04:00
|
|
|
|
|
|
|
def init_collection
|
2020-10-21 05:08:50 -04:00
|
|
|
pipeline_jobs || project_jobs || all_jobs
|
2020-03-13 20:09:30 -04:00
|
|
|
end
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
def all_jobs
|
2020-03-13 20:09:30 -04:00
|
|
|
raise Gitlab::Access::AccessDeniedError unless current_user&.admin?
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
type.all
|
2020-03-13 20:09:30 -04:00
|
|
|
end
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
def project_jobs
|
|
|
|
return unless project
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless can?(current_user, :read_build, project)
|
|
|
|
|
|
|
|
jobs_by_type(project, type).relevant
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_jobs
|
|
|
|
return unless pipeline
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless can?(current_user, :read_build, pipeline)
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
jobs_scope = jobs_by_type(pipeline, type)
|
|
|
|
params[:include_retried] ? jobs_scope : jobs_scope.latest
|
2020-09-11 08:08:50 -04:00
|
|
|
end
|
|
|
|
|
2020-03-13 20:09:30 -04:00
|
|
|
def filter_by_scope(builds)
|
2021-02-03 13:09:25 -05:00
|
|
|
return filter_by_statuses!(builds) if params[:scope].is_a?(Array)
|
2020-09-11 08:08:50 -04:00
|
|
|
|
2020-03-13 20:09:30 -04:00
|
|
|
case params[:scope]
|
|
|
|
when 'pending'
|
|
|
|
builds.pending.reverse_order
|
|
|
|
when 'running'
|
|
|
|
builds.running.reverse_order
|
|
|
|
when 'finished'
|
|
|
|
builds.finished
|
|
|
|
else
|
|
|
|
builds
|
|
|
|
end
|
|
|
|
end
|
2020-09-11 08:08:50 -04:00
|
|
|
|
2021-02-03 13:09:25 -05:00
|
|
|
def filter_by_statuses!(builds)
|
2020-09-11 08:08:50 -04:00
|
|
|
unknown_statuses = params[:scope] - ::CommitStatus::AVAILABLE_STATUSES
|
|
|
|
raise ArgumentError, 'Scope contains invalid value(s)' unless unknown_statuses.empty?
|
|
|
|
|
|
|
|
builds.where(status: params[:scope]) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
|
|
|
def jobs_by_type(relation, type)
|
|
|
|
case type.name
|
|
|
|
when ::Ci::Build.name
|
|
|
|
relation.builds
|
|
|
|
when ::Ci::Bridge.name
|
|
|
|
relation.bridges
|
|
|
|
else
|
|
|
|
raise ArgumentError, "finder does not support #{type} type"
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 20:09:30 -04:00
|
|
|
end
|
|
|
|
end
|