gitlab-org--gitlab-foss/app/finders/pipelines_finder.rb

47 lines
839 B
Ruby
Raw Normal View History

2016-05-09 23:26:13 +00:00
class PipelinesFinder
2016-08-29 16:02:08 +00:00
attr_reader :project, :pipelines
2016-05-09 23:26:13 +00:00
def initialize(project)
@project = project
2016-08-29 16:02:08 +00:00
@pipelines = project.pipelines
2016-05-09 23:26:13 +00:00
end
2016-08-29 16:02:08 +00:00
def execute(scope: nil)
scoped_pipelines =
case scope
when 'running'
pipelines.running
when 'pending'
pipelines.pending
when 'finished'
pipelines.finished
2016-08-29 16:02:08 +00:00
when 'branches'
from_ids(ids_for_ref(branches))
when 'tags'
from_ids(ids_for_ref(tags))
else
pipelines
end
scoped_pipelines.order(id: :desc)
2016-05-09 23:26:13 +00:00
end
private
2016-08-29 16:02:08 +00:00
def ids_for_ref(refs)
2016-05-09 23:26:13 +00:00
pipelines.where(ref: refs).group(:ref).select('max(id)')
end
2016-08-29 16:02:08 +00:00
def from_ids(ids)
2016-05-09 23:26:13 +00:00
pipelines.unscoped.where(id: ids)
end
def branches
2016-06-29 05:10:18 +00:00
project.repository.branch_names
2016-05-09 23:26:13 +00:00
end
def tags
2016-06-29 05:10:18 +00:00
project.repository.tag_names
2016-05-09 23:26:13 +00:00
end
end