pipelines_finder.rb 793 Bytes
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1
class PipelinesFinder
2
  attr_reader :project, :pipelines
Kamil Trzcinski's avatar
Kamil Trzcinski committed
3 4 5

  def initialize(project)
    @project = project
6
    @pipelines = project.pipelines
Kamil Trzcinski's avatar
Kamil Trzcinski committed
7 8
  end

9 10 11 12 13 14 15 16 17 18 19 20 21 22
  def execute(scope: nil)
    scoped_pipelines =
      case scope
      when 'running'
        pipelines.running_or_pending
      when 'branches'
        from_ids(ids_for_ref(branches))
      when 'tags'
        from_ids(ids_for_ref(tags))
      else
        pipelines
      end

    scoped_pipelines.order(id: :desc)
23
      .includes(project: [:namespace])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
24 25 26 27
  end

  private

28
  def ids_for_ref(refs)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
29 30 31
    pipelines.where(ref: refs).group(:ref).select('max(id)')
  end

32
  def from_ids(ids)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
33 34 35 36
    pipelines.unscoped.where(id: ids)
  end

  def branches
37
    project.repository.branch_names
Kamil Trzcinski's avatar
Kamil Trzcinski committed
38 39 40
  end

  def tags
41
    project.repository.tag_names
Kamil Trzcinski's avatar
Kamil Trzcinski committed
42 43
  end
end