2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
module Environments
|
|
|
|
class StopService < BaseService
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_reader :ref
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def execute(environment)
|
|
|
|
return unless can?(current_user, :stop_environment, environment)
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
if params[:force]
|
|
|
|
environment.stop_complete!
|
|
|
|
else
|
|
|
|
environment.stop_with_actions!(current_user)
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute_for_branch(branch_name)
|
2017-08-17 22:00:37 +05:30
|
|
|
@ref = branch_name
|
|
|
|
|
|
|
|
return unless @ref.present?
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
environments.each { |environment| execute(environment) }
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def execute_for_merge_request_pipeline(merge_request)
|
|
|
|
return unless merge_request.actual_head_pipeline&.merge_request?
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
created_environments = merge_request.created_environments
|
|
|
|
|
|
|
|
if created_environments.any?
|
2023-04-23 21:23:45 +05:30
|
|
|
# This log message can be removed with https://gitlab.com/gitlab-org/gitlab/-/issues/372965
|
|
|
|
Gitlab::AppJsonLogger.info(message: 'Running new dynamic environment stop logic', project_id: project.id)
|
2022-10-11 01:57:18 +05:30
|
|
|
created_environments.each { |env| execute(env) }
|
|
|
|
else
|
|
|
|
environments_in_head_pipeline = merge_request.environments_in_head_pipeline(deployment_status: :success)
|
|
|
|
environments_in_head_pipeline.each { |env| execute(env) }
|
|
|
|
|
|
|
|
if environments_in_head_pipeline.any?
|
|
|
|
# If we don't see a message often, we'd be able to remove this path. (or likely in GitLab 16.0)
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/372965
|
|
|
|
Gitlab::AppJsonLogger.info(message: 'Running legacy dynamic environment stop logic', project_id: project.id)
|
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def environments
|
2021-06-08 01:23:25 +05:30
|
|
|
@environments ||= Environments::EnvironmentsByDeploymentsFinder
|
2017-08-17 22:00:37 +05:30
|
|
|
.new(project, current_user, ref: @ref, recently_updated: true)
|
|
|
|
.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|