diff options
author | Sean McGivern <sean@gitlab.com> | 2019-04-30 15:13:24 +0000 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2019-04-30 15:13:24 +0000 |
commit | 2432a540cff461c5d9c0346dd4021229078d674d (patch) | |
tree | 868dc925eaa0a328d00e88f98f4921394d151602 /app | |
parent | 191152104399cad8b01d06712501250688fca9d7 (diff) | |
parent | daa8f784d016091fd2a56fd195cbd6da2f199350 (diff) | |
download | gitlab-ce-2432a540cff461c5d9c0346dd4021229078d674d.tar.gz |
Merge branch 'fix-environment-on-stop-not-work' into 'master'
`on_stop` is not automatically triggered with pipelines for merge requests
Closes #60885
See merge request gitlab-org/gitlab-ce!27618
Diffstat (limited to 'app')
-rw-r--r-- | app/models/merge_request.rb | 10 | ||||
-rw-r--r-- | app/services/ci/stop_environments_service.rb | 16 | ||||
-rw-r--r-- | app/services/merge_requests/base_service.rb | 5 | ||||
-rw-r--r-- | app/services/merge_requests/close_service.rb | 1 | ||||
-rw-r--r-- | app/services/merge_requests/post_merge_service.rb | 1 |
5 files changed, 28 insertions, 5 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index a5b62659b24..c2a1487fc6e 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -1054,6 +1054,16 @@ class MergeRequest < ApplicationRecord @environments[current_user] end + ## + # This method is for looking for active environments which created via pipelines for merge requests. + # Since deployments run on a merge request ref (e.g. `refs/merge-requests/:iid/head`), + # we cannot look up environments with source branch name. + def environments + return Environment.none unless actual_head_pipeline&.triggered_by_merge_request? + + actual_head_pipeline.environments + end + def state_human_name if merged? "Merged" diff --git a/app/services/ci/stop_environments_service.rb b/app/services/ci/stop_environments_service.rb index 973ae5ce5aa..d9a800791f2 100644 --- a/app/services/ci/stop_environments_service.rb +++ b/app/services/ci/stop_environments_service.rb @@ -9,12 +9,11 @@ module Ci return unless @ref.present? - environments.each do |environment| - next unless environment.stop_action_available? - next unless can?(current_user, :stop_environment, environment) + environments.each { |environment| stop(environment) } + end - environment.stop_with_action!(current_user) - end + def execute_for_merge_request(merge_request) + merge_request.environments.each { |environment| stop(environment) } end private @@ -24,5 +23,12 @@ module Ci .new(project, current_user, ref: @ref, recently_updated: true) .execute end + + def stop(environment) + return unless environment.stop_action_available? + return unless can?(current_user, :stop_environment, environment) + + environment.stop_with_action!(current_user) + end end end diff --git a/app/services/merge_requests/base_service.rb b/app/services/merge_requests/base_service.rb index b8334a87f6d..a9dd26c02ad 100644 --- a/app/services/merge_requests/base_service.rb +++ b/app/services/merge_requests/base_service.rb @@ -24,6 +24,11 @@ module MergeRequests end end + def cleanup_environments(merge_request) + Ci::StopEnvironmentsService.new(merge_request.source_project, current_user) + .execute_for_merge_request(merge_request) + end + private def handle_wip_event(merge_request) diff --git a/app/services/merge_requests/close_service.rb b/app/services/merge_requests/close_service.rb index 04527bb9713..e77051bb1c9 100644 --- a/app/services/merge_requests/close_service.rb +++ b/app/services/merge_requests/close_service.rb @@ -17,6 +17,7 @@ module MergeRequests execute_hooks(merge_request, 'close') invalidate_cache_counts(merge_request, users: merge_request.assignees) merge_request.update_project_counter_caches + cleanup_environments(merge_request) end merge_request diff --git a/app/services/merge_requests/post_merge_service.rb b/app/services/merge_requests/post_merge_service.rb index f26e3bee06f..c13f7dd5088 100644 --- a/app/services/merge_requests/post_merge_service.rb +++ b/app/services/merge_requests/post_merge_service.rb @@ -18,6 +18,7 @@ module MergeRequests invalidate_cache_counts(merge_request, users: merge_request.assignees) merge_request.update_project_counter_caches delete_non_latest_diffs(merge_request) + cleanup_environments(merge_request) end private |