BigW Consortium Gitlab

pipelines_finder.rb 712 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
class PipelinesFinder
  attr_reader :project

  def initialize(project)
    @project = project
  end

  def execute(pipelines, scope)
    case scope
    when 'running'
      pipelines.running_or_pending
    when 'branches'
      from_ids(pipelines, ids_for_ref(pipelines, branches))
    when 'tags'
      from_ids(pipelines, ids_for_ref(pipelines, tags))
    else
      pipelines
    end
  end

  private

  def ids_for_ref(pipelines, refs)
    pipelines.where(ref: refs).group(:ref).select('max(id)')
  end

  def from_ids(pipelines, ids)
    pipelines.unscoped.where(id: ids)
  end

  def branches
    project.repository.branches.map(&:name)
  end

  def tags
    project.repository.tags.map(&:name)
  end
end