debian-mirror-gitlab/app/services/incident_management/incidents/create_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.4 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
module IncidentManagement
module Incidents
2022-04-04 11:22:00 +05:30
class CreateService < ::BaseProjectService
2020-10-24 23:57:45 +05:30
ISSUE_TYPE = 'incident'
2022-04-04 11:22:00 +05:30
def initialize(project, current_user, title:, description:, severity: IssuableSeverity::DEFAULT, alert: nil)
super(project: project, current_user: current_user)
2020-10-24 23:57:45 +05:30
@title = title
@description = description
2020-11-24 15:15:51 +05:30
@severity = severity
2022-04-04 11:22:00 +05:30
@alert = alert
2020-10-24 23:57:45 +05:30
end
def execute
issue = Issues::CreateService.new(
2021-06-08 01:23:25 +05:30
project: project,
current_user: current_user,
params: {
title: title,
description: description,
2021-09-30 23:02:18 +05:30
issue_type: ISSUE_TYPE,
2022-04-04 11:22:00 +05:30
severity: severity,
alert_management_alert: alert
2021-09-30 23:02:18 +05:30
},
spam_params: nil
2020-10-24 23:57:45 +05:30
).execute
2022-04-04 11:22:00 +05:30
if alert
return error(alert.errors.full_messages.to_sentence, issue) unless alert.valid?
end
2020-10-24 23:57:45 +05:30
return error(issue.errors.full_messages.to_sentence, issue) unless issue.valid?
success(issue)
end
private
2022-04-04 11:22:00 +05:30
attr_reader :title, :description, :severity, :alert
2020-10-24 23:57:45 +05:30
def success(issue)
ServiceResponse.success(payload: { issue: issue })
end
def error(message, issue = nil)
ServiceResponse.error(payload: { issue: issue }, message: message)
end
end
end
end