Class: AlertManagement::MetricImages::UploadService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/alert_management/metric_images/upload_service.rb

Instance Attribute Summary collapse

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

#initialize(alert, current_user, params = {}) ⇒ UploadService

Returns a new instance of UploadService.



8
9
10
11
12
13
14
15
# File 'app/services/alert_management/metric_images/upload_service.rb', line 8

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

Instance Attribute Details

#alertObject (readonly)

Returns the value of attribute alert.



6
7
8
# File 'app/services/alert_management/metric_images/upload_service.rb', line 6

def alert
  @alert
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'app/services/alert_management/metric_images/upload_service.rb', line 6

def file
  @file
end

#metricObject (readonly)

Returns the value of attribute metric.



6
7
8
# File 'app/services/alert_management/metric_images/upload_service.rb', line 6

def metric
  @metric
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'app/services/alert_management/metric_images/upload_service.rb', line 6

def url
  @url
end

#url_textObject (readonly)

Returns the value of attribute url_text.



6
7
8
# File 'app/services/alert_management/metric_images/upload_service.rb', line 6

def url_text
  @url_text
end

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/alert_management/metric_images/upload_service.rb', line 17

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