BigW Consortium Gitlab

pipelines_finder.rb 839 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
  def execute(scope: nil)
    scoped_pipelines =
      case scope
      when 'running'
13 14 15 16 17
        pipelines.running
      when 'pending'
        pipelines.pending
      when 'finished'
        pipelines.finished
18 19 20 21 22 23 24 25 26
      when 'branches'
        from_ids(ids_for_ref(branches))
      when 'tags'
        from_ids(ids_for_ref(tags))
      else
        pipelines
      end

    scoped_pipelines.order(id: :desc)
27 28 29 30
  end

  private

31
  def ids_for_ref(refs)
32 33 34
    pipelines.where(ref: refs).group(:ref).select('max(id)')
  end

35
  def from_ids(ids)
36 37 38 39
    pipelines.unscoped.where(id: ids)
  end

  def branches
40
    project.repository.branch_names
41 42 43
  end

  def tags
44
    project.repository.tag_names
45 46
  end
end