summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorKamil Trzciński <ayufan@ayufan.eu>2016-11-17 10:54:57 +0000
committerKamil Trzciński <ayufan@ayufan.eu>2016-11-17 10:54:57 +0000
commit9ad0d879fb99685f5b41994911983ab7d05ace31 (patch)
treebe08676f5172cb918142c5c53b4cdcf5659c58f2 /app/services
parent0aca9472b056c52de89419798afee95b93c5bc3e (diff)
parent5d5b80a608d8d89525e5e903ea47c6b66e13ed23 (diff)
downloadgitlab-ce-9ad0d879fb99685f5b41994911983ab7d05ace31.tar.gz
Merge branch 'feature/environment-teardown-when-branch-deleted' into 'master'
Stop environment when branch is deleted ## What does this MR do? This MR adds a environment teardown service, that is called when user deletes a branch. This most often happens when merge requests is merged. ## Does this MR meet the acceptance criteria? - [x] [Changelog entry](https://docs.gitlab.com/ce/development/changelog.html) added - [x] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md) - [x] API support added - Tests - [x] Added for this feature/bug - [x] All builds are passing ## What are the relevant issue numbers? Closes #23218 See merge request !7355
Diffstat (limited to 'app/services')
-rw-r--r--app/services/after_branch_delete_service.rb23
-rw-r--r--app/services/ci/stop_environments_service.rb29
-rw-r--r--app/services/git_push_service.rb19
3 files changed, 65 insertions, 6 deletions
diff --git a/app/services/after_branch_delete_service.rb b/app/services/after_branch_delete_service.rb
new file mode 100644
index 00000000000..2be4d3e6ab5
--- /dev/null
+++ b/app/services/after_branch_delete_service.rb
@@ -0,0 +1,23 @@
+require_relative 'base_service'
+
+##
+# Branch can be deleted either by DeleteBranchService
+# or by GitPushService.
+#
+class AfterBranchDeleteService < BaseService
+ attr_reader :branch_name
+
+ def execute(branch_name)
+ @branch_name = branch_name
+
+ stop_environments
+ end
+
+ private
+
+ def stop_environments
+ Ci::StopEnvironmentsService
+ .new(project, current_user)
+ .execute(branch_name)
+ end
+end
diff --git a/app/services/ci/stop_environments_service.rb b/app/services/ci/stop_environments_service.rb
new file mode 100644
index 00000000000..cf590459cb2
--- /dev/null
+++ b/app/services/ci/stop_environments_service.rb
@@ -0,0 +1,29 @@
+module Ci
+ class StopEnvironmentsService < BaseService
+ attr_reader :ref
+
+ def execute(branch_name)
+ @ref = branch_name
+
+ return unless has_ref?
+
+ environments.each do |environment|
+ next unless environment.stoppable?
+ next unless can?(current_user, :create_deployment, project)
+
+ environment.stop!(current_user)
+ end
+ end
+
+ private
+
+ def has_ref?
+ @ref.present?
+ end
+
+ def environments
+ @environments ||= project
+ .environments_recently_updated_on_branch(@ref)
+ end
+ end
+end
diff --git a/app/services/git_push_service.rb b/app/services/git_push_service.rb
index de313095bed..77c6c81cc1b 100644
--- a/app/services/git_push_service.rb
+++ b/app/services/git_push_service.rb
@@ -49,10 +49,7 @@ class GitPushService < BaseService
update_gitattributes if is_default_branch?
end
- # Update merge requests that may be affected by this push. A new branch
- # could cause the last commit of a merge request to change.
- update_merge_requests
-
+ execute_related_hooks
perform_housekeeping
end
@@ -62,14 +59,24 @@ class GitPushService < BaseService
protected
- def update_merge_requests
- UpdateMergeRequestsWorker.perform_async(@project.id, current_user.id, params[:oldrev], params[:newrev], params[:ref])
+ def execute_related_hooks
+ # Update merge requests that may be affected by this push. A new branch
+ # could cause the last commit of a merge request to change.
+ #
+ UpdateMergeRequestsWorker
+ .perform_async(@project.id, current_user.id, params[:oldrev], params[:newrev], params[:ref])
EventCreateService.new.push(@project, current_user, build_push_data)
@project.execute_hooks(build_push_data.dup, :push_hooks)
@project.execute_services(build_push_data.dup, :push_hooks)
Ci::CreatePipelineService.new(@project, current_user, build_push_data).execute
ProjectCacheWorker.perform_async(@project.id)
+
+ if push_remove_branch?
+ AfterBranchDeleteService
+ .new(project, current_user)
+ .execute(branch_name)
+ end
end
def perform_housekeeping