diff --git a/app/models/project.rb b/app/models/project.rb index 64e707233f4bdf92082a43d712cc921d9fa74064..2f4fb0d082d6ad233d065e8196cd0c98c1546304 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1289,12 +1289,12 @@ class Project < ActiveRecord::Base end def environments_for(ref, commit: nil, with_tags: false) - environments_query = with_tags ? 'ref=? OR tag IS TRUE' : 'ref=?' + environments_query = with_tags ? 'ref = ? OR tag IS TRUE' : 'ref = ?' environment_ids = deployments .group(:environment_id) .select(:environment_id) - .where(environments_query, ref) + .where(environments_query, ref.to_s) envs = environments.available.where(id: environment_ids) diff --git a/app/models/repository.rb b/app/models/repository.rb index 30be7262438496bf0f1d50263b3cbfc9726bbeb1..38e5cd3faa93ffc9c4221b4cb911febf367bc91a 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -203,7 +203,7 @@ class Repository update_ref!(ref, newrev, oldrev) end - after_remove_branch + after_remove_branch(user, branch_name) true end @@ -524,7 +524,12 @@ class Repository end # Runs code after an existing branch has been removed. - def after_remove_branch + def after_remove_branch(user, branch_name) + expire_branch_cache_after_removal + stop_environments_for_branch(user, branch_name) + end + + def expire_branch_cache_after_removal expire_has_visible_content_cache expire_branch_count_cache expire_branches_cache @@ -1165,4 +1170,10 @@ class Repository def repository_event(event, tags = {}) Gitlab::Metrics.add_event(event, { path: path_with_namespace }.merge(tags)) end + + def stop_environments_for_branch(user, branch_name) + Ci::StopEnvironmentService + .new(@project, user) + .execute(branch_name) + end end diff --git a/app/services/delete_branch_service.rb b/app/services/delete_branch_service.rb index ec8ee7452d651b34e0929d4a28274f3b4d496e50..3e5dd4ebb86b90da2322d91b1c27d38095a1a6d2 100644 --- a/app/services/delete_branch_service.rb +++ b/app/services/delete_branch_service.rb @@ -21,8 +21,6 @@ class DeleteBranchService < BaseService return error('You dont have push access to repo', 405) end - # StopEnvironmentService - if repository.rm_branch(current_user, branch_name) success('Branch was removed') else diff --git a/app/services/git_push_service.rb b/app/services/git_push_service.rb index de313095bedb85b03c950b45a331a6140724be9b..ec1c2f61c27d2e1767bdf959becf08a9783d4bfe 100644 --- a/app/services/git_push_service.rb +++ b/app/services/git_push_service.rb @@ -21,7 +21,7 @@ class GitPushService < BaseService @project.repository.after_push_commit(branch_name, params[:newrev]) if push_remove_branch? - @project.repository.after_remove_branch + @project.repository.after_remove_branch(current_user, branch_name) @push_commits = [] elsif push_to_new_branch? @project.repository.after_create_branch diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index 90cf38a8513e9bf83689689037700be99afc115e..c724577ae895a9e968a04236c62da6191ed086cd 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -111,7 +111,7 @@ module Gitlab end end - project.repository.after_remove_branch + project.repository.expire_branch_cache_after_removal end def restore_source_branch(pull_request) diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 04b7d19d41496dbc02056927105a6615251b09cb..6d65f6ead120dae1ac1b9a659cb6f1f3925c15f1 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1182,7 +1182,18 @@ describe Repository, models: true do it 'flushes the visible content cache' do expect(repository).to receive(:expire_has_visible_content_cache) - repository.after_remove_branch + repository.after_remove_branch(user, 'master') + end + + context 'when there is environment with review app available for branch' do + before do + create(:environment, :with_review_app, project: project) + end + + it 'stops environment' do + expect_any_instance_of(Environment).to receive(:stop!) + repository.after_remove_branch(user, 'master') + end end end