summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/models/merge_request.rb15
-rw-r--r--app/models/project.rb23
-rw-r--r--app/services/ci/stop_environment_service.rb12
3 files changed, 25 insertions, 25 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index d76feb9680e..9d3eab52189 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -692,12 +692,15 @@ class MergeRequest < ActiveRecord::Base
def environments
return [] unless diff_head_commit
- @environments ||=
- begin
- envs = target_project.environments_for(target_branch, diff_head_commit, with_tags: true)
- envs.concat(source_project.environments_for(source_branch, diff_head_commit)) if source_project
- envs.uniq
- end
+ @environments ||= begin
+ target_envs = target_project.environments_for(
+ target_branch, commit: diff_head_commit, with_tags: true)
+
+ source_envs = source_project.environments_for(
+ source_branch, commit: diff_head_commit) if source_project
+
+ (target_envs.to_a + source_envs.to_a).uniq
+ end
end
def state_human_name
diff --git a/app/models/project.rb b/app/models/project.rb
index 4c9c7c001dd..64e707233f4 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -1288,19 +1288,20 @@ class Project < ActiveRecord::Base
Gitlab::Redis.with { |redis| redis.del(pushes_since_gc_redis_key) }
end
- def environments_for(ref, commit, with_tags: false)
- environment_ids = deployments.group(:environment_id).
- select(:environment_id)
+ def environments_for(ref, commit: nil, with_tags: false)
+ environments_query = with_tags ? 'ref=? OR tag IS TRUE' : 'ref=?'
- environment_ids =
- if with_tags
- environment_ids.where('ref=? OR tag IS TRUE', ref)
- else
- environment_ids.where(ref: ref)
- end
+ environment_ids = deployments
+ .group(:environment_id)
+ .select(:environment_id)
+ .where(environments_query, ref)
- environments.available.where(id: environment_ids).select do |environment|
- environment.includes_commit?(commit)
+ envs = environments.available.where(id: environment_ids)
+
+ if commit
+ envs.select { |env| env.includes_commit?(commit) }
+ else
+ envs.to_a
end
end
diff --git a/app/services/ci/stop_environment_service.rb b/app/services/ci/stop_environment_service.rb
index 2ac0b3d885a..cb49f71c5a3 100644
--- a/app/services/ci/stop_environment_service.rb
+++ b/app/services/ci/stop_environment_service.rb
@@ -5,7 +5,7 @@ module Ci
def execute(branch_name)
@ref = branch_name
- return unless has_ref_commit_pair?
+ return unless has_ref?
return unless has_environments?
environments.each do |environment|
@@ -17,12 +17,8 @@ module Ci
private
- def has_ref_commit_pair?
- ref && commit
- end
-
- def commit
- @commit ||= project.commit(ref)
+ def has_ref?
+ @ref.present?
end
def has_environments?
@@ -30,7 +26,7 @@ module Ci
end
def environments
- @environments ||= project.environments_for(ref, commit)
+ @environments ||= project.environments_for(@ref)
end
end
end