BigW Consortium Gitlab

pipelines_finder.rb 754 Bytes
Newer Older
1
class PipelinesFinder
2
  attr_reader :project, :pipelines
3 4 5

  def initialize(project)
    @project = project
6
    @pipelines = project.pipelines
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 24 25 26
  end

  private

27
  def ids_for_ref(refs)
28 29 30
    pipelines.where(ref: refs).group(:ref).select('max(id)')
  end

31
  def from_ids(ids)
32 33 34 35
    pipelines.unscoped.where(id: ids)
  end

  def branches
36
    project.repository.branch_names
37 38 39
  end

  def tags
40
    project.repository.tag_names
41 42
  end
end