summaryrefslogtreecommitdiff
path: root/app/workers/projects/post_creation_worker.rb
blob: 886919ecace0d80b830ff7356367d0e24ed402d3 (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
51
52
53
# frozen_string_literal: true

module Projects
  class PostCreationWorker
    include ApplicationWorker

    data_consistency :always

    sidekiq_options retry: 3

    feature_category :source_code_management
    idempotent!

    def perform(project_id)
      project = Project.find_by_id(project_id)

      return unless project

      create_prometheus_integration(project)
      create_incident_management_timeline_event_tags(project)
    end

    private

    def create_prometheus_integration(project)
      integration = project.find_or_initialize_integration(::Integrations::Prometheus.to_param)

      # If the service has already been inserted in the database, that
      # means it came from a template, and there's nothing more to do.
      return if integration.persisted?

      return unless integration.prometheus_available?

      integration.save!
    rescue ActiveRecord::RecordInvalid => e
      Gitlab::ErrorTracking.track_exception(e, extra: { project_id: project.id })
    end

    def create_incident_management_timeline_event_tags(project)
      tags = project.incident_management_timeline_event_tags.pluck_names
      start_time_name = ::IncidentManagement::TimelineEventTag::START_TIME_TAG_NAME
      end_time_name = ::IncidentManagement::TimelineEventTag::END_TIME_TAG_NAME

      project.incident_management_timeline_event_tags.new(name: start_time_name) unless tags.include?(start_time_name)

      project.incident_management_timeline_event_tags.new(name: end_time_name) unless tags.include?(end_time_name)

      project.save!
    rescue StandardError => e
      Gitlab::ErrorTracking.track_exception(e, extra: { project_id: project.id })
    end
  end
end