summaryrefslogtreecommitdiff
path: root/app/controllers/projects/analytics/cycle_analytics/stages_controller.rb
blob: ab2cf3abdde713159a0fa0088011450c1cd83df5 (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
# frozen_string_literal: true

class Projects::Analytics::CycleAnalytics::StagesController < Projects::ApplicationController
  include ::Analytics::CycleAnalytics::StageActions
  include Gitlab::Utils::StrongMemoize
  extend ::Gitlab::Utils::Override

  respond_to :json

  feature_category :planning_analytics

  before_action :authorize_read_cycle_analytics!
  before_action :only_default_value_stream_is_allowed!
  before_action :authorize_stage!, only: [:median, :count, :average, :records]

  urgency :low

  private

  override :parent
  def parent
    @project
  end

  override :value_stream_class
  def value_stream_class
    Analytics::CycleAnalytics::ProjectValueStream
  end

  override :cycle_analytics_configuration
  def cycle_analytics_configuration(stages)
    super(stages.select { |stage| permitted_stage?(stage) })
  end

  def only_default_value_stream_is_allowed!
    render_404 if params[:value_stream_id] != Analytics::CycleAnalytics::Stages::BaseService::DEFAULT_VALUE_STREAM_NAME
  end

  def permitted_stage?(stage)
    permissions[stage.name.to_sym] # name matches the permission key (only when default stages are used)
  end

  def permissions
    strong_memoize(:permissions) do
      Gitlab::CycleAnalytics::Permissions.new(user: current_user, project: parent).get
    end
  end

  def authorize_stage!
    render_403 unless permitted_stage?(stage)
  end
end