Class: IncidentManagement::TimelineEvents::CreateService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/incident_management/timeline_events/create_service.rb

Constant Summary

Constants included from Gitlab::Utils::UsageData

Gitlab::Utils::UsageData::DISTRIBUTED_HLL_FALLBACK, Gitlab::Utils::UsageData::FALLBACK, Gitlab::Utils::UsageData::HISTOGRAM_FALLBACK, Gitlab::Utils::UsageData::MAX_BUCKET_SIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#auto_create_predefined_tags, #error, #error_in_save, #error_no_permissions, #success, #track_timeline_event

Methods included from Gitlab::Utils::UsageData

#add, #add_metric, #alt_usage_data, #average, #count, #distinct_count, #estimate_batch_distinct_count, #histogram, #maximum_id, #measure_duration, #minimum_id, #redis_usage_data, #sum, #track_usage_event, #with_finished_at, #with_metadata, #with_prometheus_client

Constructor Details

#initialize(incident, user, params) ⇒ CreateService

Returns a new instance of CreateService.



10
11
12
13
14
15
16
# File 'app/services/incident_management/timeline_events/create_service.rb', line 10

def initialize(incident, user, params)
  @project = incident.project
  @incident = incident
  @user = user
  @params = params
  @auto_created = !!params.fetch(:auto_created, DEFAULT_AUTO_CREATED)
end

Class Method Details

.change_incident_status(incident, user, escalation_status) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/services/incident_management/timeline_events/create_service.rb', line 43

def change_incident_status(incident, user, escalation_status)
  status = escalation_status.status_name.to_s.titleize
  note = "@#{user.username} changed the incident status to **#{status}**"
  occurred_at = incident.updated_at
  action = 'status'

  new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute
end

.change_labels(incident, user, added_labels: [], removed_labels: []) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/services/incident_management/timeline_events/create_service.rb', line 61

def change_labels(incident, user, added_labels: [], removed_labels: [])
  return if Feature.disabled?(:incident_timeline_events_from_labels, incident.project)

  if added_labels.blank? && removed_labels.blank?
    return ServiceResponse.error(message: _('There are no changed labels'))
  end

  labels_note = -> (verb, labels) {
    "#{verb} #{labels.map(&:to_reference).join(' ')} #{'label'.pluralize(labels.count)}" if labels.present?
  }

  added_note = labels_note.call('added', added_labels)
  removed_note = labels_note.call('removed', removed_labels)
  note = "@#{user.username} #{[added_note, removed_note].compact.join(' and ')}"
  occurred_at = incident.updated_at
  action = 'label'

  new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute
end

.change_severity(incident, user) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/services/incident_management/timeline_events/create_service.rb', line 52

def change_severity(incident, user)
  severity_label = IssuableSeverity::SEVERITY_LABELS[incident.severity.to_sym]
  note = "@#{user.username} changed the incident severity to **#{severity_label}**"
  occurred_at = incident.updated_at
  action = 'severity'

  new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute
end

.create_incident(incident, user) ⇒ Object



19
20
21
22
23
24
25
# File 'app/services/incident_management/timeline_events/create_service.rb', line 19

def create_incident(incident, user)
  note = "@#{user.username} created the incident"
  occurred_at = incident.created_at
  action = 'issues'

  new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute
end

.reopen_incident(incident, user) ⇒ Object



27
28
29
30
31
32
33
# File 'app/services/incident_management/timeline_events/create_service.rb', line 27

def reopen_incident(incident, user)
  note = "@#{user.username} reopened the incident"
  occurred_at = incident.updated_at
  action = 'issues'

  new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute
end

.resolve_incident(incident, user) ⇒ Object



35
36
37
38
39
40
41
# File 'app/services/incident_management/timeline_events/create_service.rb', line 35

def resolve_incident(incident, user)
  note = "@#{user.username} resolved the incident"
  occurred_at = incident.updated_at
  action = 'status'

  new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute
end

Instance Method Details

#executeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/incident_management/timeline_events/create_service.rb', line 82

def execute
  return error_no_permissions unless allowed?

  timeline_event_params = {
    project: project,
    incident: incident,
    author: user,
    note: params[:note],
    action: params.fetch(:action, DEFAULT_ACTION),
    note_html: params[:note_html].presence || params[:note],
    occurred_at: params[:occurred_at],
    promoted_from_note: params[:promoted_from_note],
    editable: params.fetch(:editable, DEFAULT_EDITABLE)
  }

  non_existing_tags = validate_tags(project, params[:timeline_event_tag_names])

  return error("#{_("Following tags don't exist")}: #{non_existing_tags}") unless non_existing_tags.empty?

  timeline_event = IncidentManagement::TimelineEvent.new(timeline_event_params)

  if timeline_event.save(context: validation_context)
    add_system_note(timeline_event)

    create_timeline_event_tag_links(timeline_event, params[:timeline_event_tag_names])

    track_timeline_event("incident_management_timeline_event_created", project)

    success(timeline_event)
  else
    error_in_save(timeline_event)
  end
end