diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-24 21:09:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-24 21:09:03 +0300 |
commit | c59765a50abd6a235220fd895f5de78038c243a8 (patch) | |
tree | 6cacf61d1746e2d54149c028ecd3f187128cd7da /app/services | |
parent | 4c5468b40825debc2b7bbe08b975dedd2f7f1523 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
3 files changed, 73 insertions, 93 deletions
diff --git a/app/services/alert_management/create_alert_issue_service.rb b/app/services/alert_management/create_alert_issue_service.rb index 211d3c29a13..66ca28f2223 100644 --- a/app/services/alert_management/create_alert_issue_service.rb +++ b/app/services/alert_management/create_alert_issue_service.rb @@ -4,8 +4,6 @@ module AlertManagement class CreateAlertIssueService include Gitlab::Utils::StrongMemoize - INCIDENT_LABEL = ::IncidentManagement::CreateIssueService::INCIDENT_LABEL - # @param alert [AlertManagement::Alert] # @param user [User] def initialize(alert, user) @@ -18,17 +16,17 @@ module AlertManagement return error_issue_already_exists if alert.issue result = create_issue - @issue = result.payload[:issue] + issue = result.payload[:issue] - return error(result.message) if result.error? - return error(alert.errors.full_messages.to_sentence) unless update_alert_issue_id + return error(result.message, issue) if result.error? + return error(object_errors(alert), issue) unless associate_alert_with_issue(issue) - success + result end private - attr_reader :alert, :user, :issue + attr_reader :alert, :user delegate :project, to: :alert @@ -37,31 +35,35 @@ module AlertManagement end def create_issue - issue = do_create_issue(label_ids: issue_label_ids) + label_result = find_or_create_incident_label - # Create an unlabelled issue if we couldn't create the issue - # due to labels errors. + # 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 - if issue.errors.include?(:labels) - log_label_error(issue) - issue = do_create_issue - end + extra_params = label_result.success? ? { label_ids: [label_result.payload[:label].id] } : {} + + issue = Issues::CreateService.new( + project, + user, + title: alert_presenter.title, + description: alert_presenter.issue_description, + **extra_params + ).execute - return error(issue_errors(issue)) unless issue.valid? + return error(object_errors(issue), issue) unless issue.valid? - @issue = issue - success + success(issue) end - def update_alert_issue_id + def associate_alert_with_issue(issue) alert.update(issue_id: issue.id) end - def success + def success(issue) ServiceResponse.success(payload: { issue: issue }) end - def error(message) + def error(message, issue = nil) ServiceResponse.error(payload: { issue: issue }, message: message) end @@ -73,47 +75,18 @@ module AlertManagement error(_('You have no permissions')) end - def do_create_issue(**params) - Issues::CreateService.new( - project, - user, - title: alert_presenter.title, - description: alert_presenter.issue_description, - **params - ).execute - end - def alert_presenter strong_memoize(:alert_presenter) do alert.present end end - def issue_label_ids - [ - find_or_create_label(**INCIDENT_LABEL) - ].compact.map(&:id) - end - - def find_or_create_label(**params) - Labels::FindOrCreateService - .new(user, project, **params) - .execute - end - - def issue_errors(issue) - issue.errors.full_messages.to_sentence + def find_or_create_incident_label + IncidentManagement::CreateIncidentLabelService.new(project, user).execute end - def log_label_error(issue) - Gitlab::AppLogger.info( - <<~TEXT.chomp - Cannot create incident issue with labels \ - #{issue.labels.map(&:title).inspect} \ - for "#{project.full_name}": #{issue.errors.full_messages.to_sentence}. - Retrying without labels. - TEXT - ) + def object_errors(object) + object.errors.full_messages.to_sentence end end end diff --git a/app/services/incident_management/create_incident_label_service.rb b/app/services/incident_management/create_incident_label_service.rb new file mode 100644 index 00000000000..dbd0d78fa3c --- /dev/null +++ b/app/services/incident_management/create_incident_label_service.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module IncidentManagement + class CreateIncidentLabelService < BaseService + LABEL_PROPERTIES = { + title: 'incident', + color: '#CC0033', + description: <<~DESCRIPTION.chomp + Denotes a disruption to IT services and \ + the associated issues require immediate attention + DESCRIPTION + }.freeze + + 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 + + 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 index 43077e03e6d..5e1e0863115 100644 --- a/app/services/incident_management/create_issue_service.rb +++ b/app/services/incident_management/create_issue_service.rb @@ -4,15 +4,6 @@ module IncidentManagement class CreateIssueService < BaseService include Gitlab::Utils::StrongMemoize - INCIDENT_LABEL = { - title: 'incident', - color: '#CC0033', - description: <<~DESCRIPTION.chomp - Denotes a disruption to IT services and \ - the associated issues require immediate attention - DESCRIPTION - }.freeze - def initialize(project, params) super(project, User.alert_bot, params) end @@ -30,26 +21,19 @@ module IncidentManagement private def create_issue - issue = do_create_issue(label_ids: issue_label_ids) + label_result = find_or_create_incident_label - # Create an unlabelled issue if we couldn't create the issue - # due to labels errors. + # 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 - if issue.errors.include?(:labels) - log_label_error(issue) - issue = do_create_issue - end + extra_params = label_result.success? ? { label_ids: [label_result.payload[:label].id] } : {} - issue - end - - def do_create_issue(**params) Issues::CreateService.new( project, current_user, title: issue_title, description: issue_description, - **params + **extra_params ).execute end @@ -67,16 +51,8 @@ module IncidentManagement ].compact.join(horizontal_line) end - def issue_label_ids - [ - find_or_create_label(**INCIDENT_LABEL) - ].compact.map(&:id) - end - - def find_or_create_label(**params) - Labels::FindOrCreateService - .new(current_user, project, **params) - .execute + def find_or_create_incident_label + IncidentManagement::CreateIncidentLabelService.new(project, current_user).execute end def alert_summary @@ -108,15 +84,6 @@ module IncidentManagement issue.errors.full_messages.to_sentence end - def log_label_error(issue) - log_info <<~TEXT.chomp - Cannot create incident issue with labels \ - #{issue.labels.map(&:title).inspect} \ - for "#{project.full_name}": #{issue.errors.full_messages.to_sentence}. - Retrying without labels. - TEXT - end - def error_with(message) log_error(%{Cannot create incident issue for "#{project.full_name}": #{message}}) |