summaryrefslogtreecommitdiff
path: root/app/finders/merge_request/metrics_finder.rb
blob: d93e53d1636a30f957d6f79f2067ba20023ee863 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

class MergeRequest::MetricsFinder
  include Gitlab::Allowable

  def initialize(current_user, params = {})
    @current_user = current_user
    @params = params
  end

  def execute
    return klass.none if target_project.blank? || user_not_authorized?

    items = init_collection
    items = by_target_project(items)
    items = by_merged_after(items)
    items = by_merged_before(items)

    items
  end

  private

  attr_reader :current_user, :params

  def by_target_project(items)
    items.by_target_project(target_project)
  end

  def by_merged_after(items)
    return items unless merged_after

    items.merged_after(merged_after)
  end

  def by_merged_before(items)
    return items unless merged_before

    items.merged_before(merged_before)
  end

  def user_not_authorized?
    !can?(current_user, :read_merge_request, target_project)
  end

  def init_collection
    klass.all
  end

  def klass
    MergeRequest::Metrics
  end

  def target_project
    params[:target_project]
  end

  def merged_after
    params[:merged_after]
  end

  def merged_before
    params[:merged_before]
  end
end