2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-02 08:38:24 -04:00
|
|
|
module Issues
|
2014-04-02 12:55:23 -04:00
|
|
|
class CloseService < Issues::BaseService
|
2016-10-07 09:20:57 -04:00
|
|
|
# Closes the supplied issue if the current user is able to do so.
|
2021-10-05 20:11:56 -04:00
|
|
|
def execute(issue, commit: nil, notifications: true, system_note: true, skip_authorization: false)
|
|
|
|
return issue unless can_close?(issue, skip_authorization: skip_authorization)
|
2016-08-09 11:51:40 -04:00
|
|
|
|
2016-10-07 09:20:57 -04:00
|
|
|
close_issue(issue,
|
2019-05-16 07:59:02 -04:00
|
|
|
closed_via: commit,
|
2016-10-07 09:20:57 -04:00
|
|
|
notifications: notifications,
|
|
|
|
system_note: system_note)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Closes the supplied issue without checking if the user is authorized to
|
|
|
|
# do so.
|
|
|
|
#
|
|
|
|
# The code calling this method is responsible for ensuring that a user is
|
|
|
|
# allowed to close the given issue.
|
2019-05-16 07:59:02 -04:00
|
|
|
def close_issue(issue, closed_via: nil, notifications: true, system_note: true)
|
2020-03-13 02:09:37 -04:00
|
|
|
if issue.is_a?(ExternalIssue)
|
|
|
|
close_external_issue(issue, closed_via)
|
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
return issue
|
|
|
|
end
|
|
|
|
|
2021-10-05 20:11:56 -04:00
|
|
|
if perform_close(issue)
|
2014-04-02 08:38:24 -04:00
|
|
|
event_service.close_issue(issue, current_user)
|
2019-05-16 07:59:02 -04:00
|
|
|
create_note(issue, closed_via) if system_note
|
|
|
|
|
2019-06-04 05:56:32 -04:00
|
|
|
closed_via = _("commit %{commit_id}") % { commit_id: closed_via.id } if closed_via.is_a?(Commit)
|
2019-05-16 07:59:02 -04:00
|
|
|
|
2021-01-07 10:10:33 -05:00
|
|
|
notification_service.async.close_issue(issue, current_user, { closed_via: closed_via }) if notifications
|
2016-02-20 08:59:59 -05:00
|
|
|
todo_service.close_issue(issue, current_user)
|
2021-08-05 11:09:46 -04:00
|
|
|
perform_incident_management_actions(issue)
|
2014-05-23 09:16:03 -04:00
|
|
|
execute_hooks(issue, 'close')
|
2017-07-11 12:12:33 -04:00
|
|
|
invalidate_cache_counts(issue, users: issue.assignees)
|
2017-09-19 07:55:56 -04:00
|
|
|
issue.update_project_counter_caches
|
2020-09-10 17:08:28 -04:00
|
|
|
track_incident_action(current_user, issue, :incident_closed)
|
2020-02-21 07:09:07 -05:00
|
|
|
|
2021-01-19 04:10:32 -05:00
|
|
|
if closed_via.is_a?(MergeRequest)
|
|
|
|
store_first_mentioned_in_commit_at(issue, closed_via)
|
|
|
|
OnboardingProgressService.new(project.namespace).execute(action: :issue_auto_closed)
|
|
|
|
end
|
2020-03-04 19:07:49 -05:00
|
|
|
|
|
|
|
delete_milestone_closed_issue_counter_cache(issue.milestone)
|
2014-04-02 08:38:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
issue
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-05 20:11:56 -04:00
|
|
|
# Overridden on EE
|
|
|
|
def perform_close(issue)
|
|
|
|
issue.close(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_close?(issue, skip_authorization: false)
|
|
|
|
skip_authorization || can?(current_user, :update_issue, issue) || issue.is_a?(ExternalIssue)
|
|
|
|
end
|
|
|
|
|
2021-08-05 11:09:46 -04:00
|
|
|
def perform_incident_management_actions(issue)
|
|
|
|
resolve_alert(issue)
|
2021-11-02 17:11:52 -04:00
|
|
|
resolve_incident(issue)
|
2021-08-05 11:09:46 -04:00
|
|
|
end
|
|
|
|
|
2020-03-13 02:09:37 -04:00
|
|
|
def close_external_issue(issue, closed_via)
|
|
|
|
return unless project.external_issue_tracker&.support_close_issue?
|
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
project.external_issue_tracker.close_issue(closed_via, issue, current_user)
|
2020-03-13 02:09:37 -04:00
|
|
|
todo_service.close_issue(issue, current_user)
|
|
|
|
end
|
|
|
|
|
2014-04-02 08:38:24 -04:00
|
|
|
def create_note(issue, current_commit)
|
2015-05-09 18:18:50 -04:00
|
|
|
SystemNoteService.change_status(issue, issue.project, current_user, issue.state, current_commit)
|
2014-04-02 08:38:24 -04:00
|
|
|
end
|
2020-02-21 07:09:07 -05:00
|
|
|
|
2020-07-23 05:09:18 -04:00
|
|
|
def resolve_alert(issue)
|
|
|
|
return unless alert = issue.alert_management_alert
|
|
|
|
return if alert.resolved?
|
|
|
|
|
|
|
|
if alert.resolve
|
|
|
|
SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project, author: current_user).closed_alert_issue(issue)
|
|
|
|
else
|
|
|
|
Gitlab::AppLogger.warn(
|
|
|
|
message: 'Cannot resolve an associated Alert Management alert',
|
|
|
|
issue_id: issue.id,
|
|
|
|
alert_id: alert.id,
|
|
|
|
alert_errors: alert.errors.messages
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-02 17:11:52 -04:00
|
|
|
def resolve_incident(issue)
|
|
|
|
return unless issue.incident?
|
|
|
|
|
|
|
|
status = issue.incident_management_issuable_escalation_status || issue.build_incident_management_issuable_escalation_status
|
|
|
|
|
|
|
|
SystemNoteService.resolve_incident_status(issue, current_user) if status.resolve
|
|
|
|
end
|
|
|
|
|
2021-10-07 08:12:09 -04:00
|
|
|
def store_first_mentioned_in_commit_at(issue, merge_request, max_commit_lookup: 100)
|
2020-02-21 07:09:07 -05:00
|
|
|
metrics = issue.metrics
|
|
|
|
return if metrics.nil? || metrics.first_mentioned_in_commit_at
|
|
|
|
|
2021-10-07 08:12:09 -04:00
|
|
|
first_commit_timestamp = merge_request.commits(limit: max_commit_lookup).last.try(:authored_date)
|
2020-02-21 07:09:07 -05:00
|
|
|
return unless first_commit_timestamp
|
|
|
|
|
|
|
|
metrics.update!(first_mentioned_in_commit_at: first_commit_timestamp)
|
|
|
|
end
|
2014-04-02 08:38:24 -04:00
|
|
|
end
|
|
|
|
end
|
2021-08-05 11:09:46 -04:00
|
|
|
|
|
|
|
Issues::CloseService.prepend_mod_with('Issues::CloseService')
|