summaryrefslogtreecommitdiff
path: root/app/finders/pipelines_finder.rb
blob: a9172f6767fb42ca51339d5b1af49d61559857f7 (plain)
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
39
40
41
42
43
44
45
46
class PipelinesFinder
  attr_reader :project, :pipelines

  def initialize(project)
    @project = project
    @pipelines = project.pipelines
  end

  def execute(scope: nil)
    scoped_pipelines =
      case scope
      when 'running'
        pipelines.running
      when 'pending'
        pipelines.pending
      when 'finished'
        pipelines.finished
      when 'branches'
        from_ids(ids_for_ref(branches))
      when 'tags'
        from_ids(ids_for_ref(tags))
      else
        pipelines
      end

    scoped_pipelines.order(id: :desc)
  end

  private

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

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

  def branches
    project.repository.branch_names
  end

  def tags
    project.repository.tag_names
  end
end