diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 13:00:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 13:00:54 +0300 |
commit | 3cccd102ba543e02725d247893729e5c73b38295 (patch) | |
tree | f36a04ec38517f5deaaacb5acc7d949688d1e187 /app/services/alert_management/metric_images/upload_service.rb | |
parent | 205943281328046ef7b4528031b90fbda70c75ac (diff) |
Add latest changes from gitlab-org/gitlab@14-10-stable-eev14.10.0-rc42
Diffstat (limited to 'app/services/alert_management/metric_images/upload_service.rb')
-rw-r--r-- | app/services/alert_management/metric_images/upload_service.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/app/services/alert_management/metric_images/upload_service.rb b/app/services/alert_management/metric_images/upload_service.rb new file mode 100644 index 00000000000..e9db10594df --- /dev/null +++ b/app/services/alert_management/metric_images/upload_service.rb @@ -0,0 +1,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 |