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

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

module IncidentManagement
  module Incidents
    class CreateService < BaseService
      ISSUE_TYPE = 'incident'

      def initialize(project, current_user, title:, description:)
        super(project, current_user)

        @title = title
        @description = description
      end

      def execute
        issue = Issues::CreateService.new(
          project,
          current_user,
          title: title,
          description: description,
          label_ids: [find_or_create_incident_label.id],
          issue_type: ISSUE_TYPE
        ).execute

        return error(issue.errors.full_messages.to_sentence, issue) unless issue.valid?

        success(issue)
      end

      private

      attr_reader :title, :description

      def find_or_create_incident_label
        IncidentManagement::CreateIncidentLabelService
          .new(project, current_user)
          .execute
          .payload[:label]
      end

      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