diff options
Diffstat (limited to 'app/services/incident_management')
5 files changed, 57 insertions, 143 deletions
diff --git a/app/services/incident_management/create_incident_label_service.rb b/app/services/incident_management/create_incident_label_service.rb index dbd0d78fa3c..595f5df184f 100644 --- a/app/services/incident_management/create_incident_label_service.rb +++ b/app/services/incident_management/create_incident_label_service.rb @@ -14,27 +14,9 @@ module IncidentManagement def execute label = Labels::FindOrCreateService .new(current_user, project, **LABEL_PROPERTIES) - .execute - - if label.invalid? - log_invalid_label_info(label) - return ServiceResponse.error(payload: { label: label }, message: full_error_message(label)) - end + .execute(skip_authorization: true) ServiceResponse.success(payload: { label: label }) end - - private - - def log_invalid_label_info(label) - log_info <<~TEXT.chomp - Cannot create incident label "#{label.title}" \ - for "#{label.project.full_name}": #{full_error_message(label)}. - TEXT - end - - def full_error_message(label) - label.errors.full_messages.to_sentence - end end end diff --git a/app/services/incident_management/create_issue_service.rb b/app/services/incident_management/create_issue_service.rb deleted file mode 100644 index 5e1e0863115..00000000000 --- a/app/services/incident_management/create_issue_service.rb +++ /dev/null @@ -1,93 +0,0 @@ -# frozen_string_literal: true - -module IncidentManagement - class CreateIssueService < BaseService - include Gitlab::Utils::StrongMemoize - - def initialize(project, params) - super(project, User.alert_bot, params) - end - - def execute - return error_with('setting disabled') unless incident_management_setting.create_issue? - return error_with('invalid alert') unless alert.valid? - - issue = create_issue - return error_with(issue_errors(issue)) unless issue.valid? - - success(issue: issue) - end - - private - - def create_issue - label_result = find_or_create_incident_label - - # Create an unlabelled issue if we couldn't create the label - # due to a race condition. - # See https://gitlab.com/gitlab-org/gitlab-foss/issues/65042 - extra_params = label_result.success? ? { label_ids: [label_result.payload[:label].id] } : {} - - Issues::CreateService.new( - project, - current_user, - title: issue_title, - description: issue_description, - **extra_params - ).execute - end - - def issue_title - alert.full_title - end - - def issue_description - horizontal_line = "\n\n---\n\n" - - [ - alert_summary, - alert_markdown, - issue_template_content - ].compact.join(horizontal_line) - end - - def find_or_create_incident_label - IncidentManagement::CreateIncidentLabelService.new(project, current_user).execute - end - - def alert_summary - alert.issue_summary_markdown - end - - def alert_markdown - alert.alert_markdown - end - - def alert - strong_memoize(:alert) do - Gitlab::Alerting::Alert.new(project: project, payload: params).present - end - end - - def issue_template_content - incident_management_setting.issue_template_content - end - - def incident_management_setting - strong_memoize(:incident_management_setting) do - project.incident_management_setting || - project.build_incident_management_setting - end - end - - def issue_errors(issue) - issue.errors.full_messages.to_sentence - end - - def error_with(message) - log_error(%{Cannot create incident issue for "#{project.full_name}": #{message}}) - - error(message) - end - end -end diff --git a/app/services/incident_management/incidents/create_service.rb b/app/services/incident_management/incidents/create_service.rb new file mode 100644 index 00000000000..7206eaf51b2 --- /dev/null +++ b/app/services/incident_management/incidents/create_service.rb @@ -0,0 +1,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 diff --git a/app/services/incident_management/pager_duty/create_incident_issue_service.rb b/app/services/incident_management/pager_duty/create_incident_issue_service.rb index ee0feb49e0d..0c9ca2c0add 100644 --- a/app/services/incident_management/pager_duty/create_incident_issue_service.rb +++ b/app/services/incident_management/pager_duty/create_incident_issue_service.rb @@ -12,46 +12,30 @@ module IncidentManagement def execute return forbidden unless webhook_available? - issue = create_issue - return error(issue.errors.full_messages.to_sentence, issue) unless issue.valid? - - success(issue) + create_incident end private alias_method :incident_payload, :params - def create_issue - label_result = find_or_create_incident_label - - # Create an unlabelled issue if we couldn't create the label - # due to a race condition. - # See https://gitlab.com/gitlab-org/gitlab-foss/issues/65042 - extra_params = label_result.success? ? { label_ids: [label_result.payload[:label].id] } : {} - - Issues::CreateService.new( + def create_incident + ::IncidentManagement::Incidents::CreateService.new( project, current_user, title: issue_title, - description: issue_description, - **extra_params + description: issue_description ).execute end def webhook_available? - Feature.enabled?(:pagerduty_webhook, project) && - incident_management_setting.pagerduty_active? + incident_management_setting.pagerduty_active? end def forbidden ServiceResponse.error(message: 'Forbidden', http_status: :forbidden) end - def find_or_create_incident_label - ::IncidentManagement::CreateIncidentLabelService.new(project, current_user).execute - end - def issue_title incident_payload['title'] end @@ -59,14 +43,6 @@ module IncidentManagement def issue_description Gitlab::IncidentManagement::PagerDuty::IncidentIssueDescription.new(incident_payload).to_s 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 diff --git a/app/services/incident_management/pager_duty/process_webhook_service.rb b/app/services/incident_management/pager_duty/process_webhook_service.rb index 5dd3186694a..fd8252f75fb 100644 --- a/app/services/incident_management/pager_duty/process_webhook_service.rb +++ b/app/services/incident_management/pager_duty/process_webhook_service.rb @@ -39,8 +39,7 @@ module IncidentManagement end def webhook_setting_active? - Feature.enabled?(:pagerduty_webhook, project) && - incident_management_setting.pagerduty_active? + incident_management_setting.pagerduty_active? end def valid_token?(token) |