Welcome to mirror list, hosted at ThFree Co, Russian Federation.

upload_service.rb « metric_images « alert_management « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9db10594df33db374a1d40019f975e176b6c5a5 (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
# frozen_string_literal: true

module AlertManagement
  module MetricImages
    class UploadService < BaseService
      attr_reader :alert, :file, :url, :url_text, :metric

      def initialize(alert, current_user, params = {})
        super

        @alert = alert
        @file = params.fetch(:file)
        @url = params.fetch(:url, nil)
        @url_text = params.fetch(:url_text, nil)
      end

      def execute
        unless can_upload_metrics?
          return ServiceResponse.error(
            message: _("You are not authorized to upload metric images"),
            http_status: :forbidden
          )
        end

        metric = AlertManagement::MetricImage.new(
          alert: alert,
          file: file,
          url: url,
          url_text: url_text
        )

        if metric.save
          ServiceResponse.success(payload: { metric: metric, alert: alert })
        else
          ServiceResponse.error(message: metric.errors.full_messages.join(', '), http_status: :bad_request)
        end
      end

      private

      def can_upload_metrics?
        alert.metric_images_available? && current_user&.can?(:upload_alert_management_metric_image, alert)
      end
    end
  end
end