summaryrefslogtreecommitdiff
path: root/app/controllers/projects/error_tracking_controller.rb
blob: 4596b6c91f2af87df6741c8fc2c888c7ea61cd4f (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
# frozen_string_literal: true

class Projects::ErrorTrackingController < Projects::ApplicationController
  before_action :check_feature_flag!
  before_action :authorize_read_sentry_issue!
  before_action :push_feature_flag_to_frontend

  POLLING_INTERVAL = 10_000

  def index
    respond_to do |format|
      format.html
      format.json do
        set_polling_interval
        render_index_json
      end
    end
  end

  private

  def render_index_json
    service = ErrorTracking::ListIssuesService.new(project, current_user)
    result = service.execute

    unless result[:status] == :success
      return render json: { message: result[:message] },
                    status: result[:http_status] || :bad_request
    end

    render json: {
      errors: serialize_errors(result[:issues]),
      external_url: service.external_url
    }
  end

  def set_polling_interval
    Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
  end

  def serialize_errors(errors)
    ErrorTracking::ErrorSerializer
      .new(project: project, user: current_user)
      .represent(errors)
  end

  def check_feature_flag!
    render_404 unless Feature.enabled?(:error_tracking, project)
  end

  def push_feature_flag_to_frontend
    push_frontend_feature_flag(:error_tracking, current_user)
  end
end