2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module Issues
|
|
|
|
class CloseService < Issues::BaseService
|
2017-08-17 22:00:37 +05:30
|
|
|
# Closes the supplied issue if the current user is able to do so.
|
2021-11-18 22:05:49 +05:30
|
|
|
def execute(issue, commit: nil, notifications: true, system_note: true, skip_authorization: false)
|
|
|
|
return issue unless can_close?(issue, skip_authorization: skip_authorization)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
close_issue(issue,
|
2019-09-04 21:01:54 +05:30
|
|
|
closed_via: commit,
|
2017-08-17 22:00:37 +05:30
|
|
|
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-09-04 21:01:54 +05:30
|
|
|
def close_issue(issue, closed_via: nil, notifications: true, system_note: true)
|
2020-04-08 14:13:33 +05:30
|
|
|
if issue.is_a?(ExternalIssue)
|
|
|
|
close_external_issue(issue, closed_via)
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
return issue
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
if perform_close(issue)
|
2014-09-02 18:07:02 +05:30
|
|
|
event_service.close_issue(issue, current_user)
|
2019-09-04 21:01:54 +05:30
|
|
|
create_note(issue, closed_via) if system_note
|
|
|
|
|
|
|
|
closed_via = _("commit %{commit_id}") % { commit_id: closed_via.id } if closed_via.is_a?(Commit)
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
notification_service.async.close_issue(issue, current_user, { closed_via: closed_via }) if notifications
|
2016-04-02 18:10:28 +05:30
|
|
|
todo_service.close_issue(issue, current_user)
|
2021-10-27 15:23:28 +05:30
|
|
|
perform_incident_management_actions(issue)
|
2014-09-02 18:07:02 +05:30
|
|
|
execute_hooks(issue, 'close')
|
2017-09-10 17:25:29 +05:30
|
|
|
invalidate_cache_counts(issue, users: issue.assignees)
|
2018-03-17 18:26:18 +05:30
|
|
|
issue.update_project_counter_caches
|
2020-11-24 15:15:51 +05:30
|
|
|
track_incident_action(current_user, issue, :incident_closed)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
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-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
delete_milestone_closed_issue_counter_cache(issue.milestone)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
issue
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
# 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-10-27 15:23:28 +05:30
|
|
|
def perform_incident_management_actions(issue)
|
|
|
|
resolve_alert(issue)
|
2021-12-11 22:18:48 +05:30
|
|
|
resolve_incident(issue)
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def close_external_issue(issue, closed_via)
|
|
|
|
return unless project.external_issue_tracker&.support_close_issue?
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
project.external_issue_tracker.close_issue(closed_via, issue, current_user)
|
2020-04-08 14:13:33 +05:30
|
|
|
todo_service.close_issue(issue, current_user)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def create_note(issue, current_commit)
|
2015-09-11 14:41:01 +05:30
|
|
|
SystemNoteService.change_status(issue, issue.project, current_user, issue.state, current_commit)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def resolve_alert(issue)
|
|
|
|
return unless alert = issue.alert_management_alert
|
|
|
|
return if alert.resolved?
|
|
|
|
|
|
|
|
if alert.resolve
|
2022-04-04 11:22:00 +05:30
|
|
|
SystemNoteService.change_alert_status(alert, current_user, " by closing incident #{issue.to_reference(project)}")
|
2020-10-24 23:57:45 +05:30
|
|
|
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-12-11 22:18:48 +05:30
|
|
|
def resolve_incident(issue)
|
|
|
|
return unless issue.incident?
|
|
|
|
|
|
|
|
status = issue.incident_management_issuable_escalation_status || issue.build_incident_management_issuable_escalation_status
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
return unless status.resolve
|
|
|
|
|
|
|
|
SystemNoteService.change_incident_status(issue, current_user, ' by closing the incident')
|
|
|
|
IncidentManagement::TimelineEvents::CreateService.resolve_incident(issue, current_user)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def store_first_mentioned_in_commit_at(issue, merge_request, max_commit_lookup: 100)
|
2020-04-08 14:13:33 +05:30
|
|
|
metrics = issue.metrics
|
|
|
|
return if metrics.nil? || metrics.first_mentioned_in_commit_at
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
first_commit_timestamp = merge_request.commits(limit: max_commit_lookup).last.try(:authored_date)
|
2020-04-08 14:13:33 +05:30
|
|
|
return unless first_commit_timestamp
|
|
|
|
|
|
|
|
metrics.update!(first_mentioned_in_commit_at: first_commit_timestamp)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
Issues::CloseService.prepend_mod_with('Issues::CloseService')
|