Commit 5328e3b1 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make commit an optional arg for environments search

parent 45bd5391
...@@ -692,12 +692,15 @@ class MergeRequest < ActiveRecord::Base ...@@ -692,12 +692,15 @@ class MergeRequest < ActiveRecord::Base
def environments def environments
return [] unless diff_head_commit return [] unless diff_head_commit
@environments ||= @environments ||= begin
begin target_envs = target_project.environments_for(
envs = target_project.environments_for(target_branch, diff_head_commit, with_tags: true) target_branch, commit: diff_head_commit, with_tags: true)
envs.concat(source_project.environments_for(source_branch, diff_head_commit)) if source_project
envs.uniq source_envs = source_project.environments_for(
end source_branch, commit: diff_head_commit) if source_project
(target_envs.to_a + source_envs.to_a).uniq
end
end end
def state_human_name def state_human_name
......
...@@ -1288,19 +1288,20 @@ class Project < ActiveRecord::Base ...@@ -1288,19 +1288,20 @@ class Project < ActiveRecord::Base
Gitlab::Redis.with { |redis| redis.del(pushes_since_gc_redis_key) } Gitlab::Redis.with { |redis| redis.del(pushes_since_gc_redis_key) }
end end
def environments_for(ref, commit, with_tags: false) def environments_for(ref, commit: nil, with_tags: false)
environment_ids = deployments.group(:environment_id). environments_query = with_tags ? 'ref=? OR tag IS TRUE' : 'ref=?'
select(:environment_id)
environment_ids = environment_ids = deployments
if with_tags .group(:environment_id)
environment_ids.where('ref=? OR tag IS TRUE', ref) .select(:environment_id)
else .where(environments_query, ref)
environment_ids.where(ref: ref)
end
environments.available.where(id: environment_ids).select do |environment| envs = environments.available.where(id: environment_ids)
environment.includes_commit?(commit)
if commit
envs.select { |env| env.includes_commit?(commit) }
else
envs.to_a
end end
end end
......
...@@ -5,7 +5,7 @@ module Ci ...@@ -5,7 +5,7 @@ module Ci
def execute(branch_name) def execute(branch_name)
@ref = branch_name @ref = branch_name
return unless has_ref_commit_pair? return unless has_ref?
return unless has_environments? return unless has_environments?
environments.each do |environment| environments.each do |environment|
...@@ -17,12 +17,8 @@ module Ci ...@@ -17,12 +17,8 @@ module Ci
private private
def has_ref_commit_pair? def has_ref?
ref && commit @ref.present?
end
def commit
@commit ||= project.commit(ref)
end end
def has_environments? def has_environments?
...@@ -30,7 +26,7 @@ module Ci ...@@ -30,7 +26,7 @@ module Ci
end end
def environments def environments
@environments ||= project.environments_for(ref, commit) @environments ||= project.environments_for(@ref)
end end
end end
end end
...@@ -1646,15 +1646,18 @@ describe Project, models: true do ...@@ -1646,15 +1646,18 @@ describe Project, models: true do
end end
it 'returns environment when with_tags is set' do it 'returns environment when with_tags is set' do
expect(project.environments_for('master', project.commit, with_tags: true)).to contain_exactly(environment) expect(project.environments_for('master', commit: project.commit, with_tags: true))
.to contain_exactly(environment)
end end
it 'does not return environment when no with_tags is set' do it 'does not return environment when no with_tags is set' do
expect(project.environments_for('master', project.commit)).to be_empty expect(project.environments_for('master', commit: project.commit))
.to be_empty
end end
it 'does not return environment when commit is not part of deployment' do it 'does not return environment when commit is not part of deployment' do
expect(project.environments_for('master', project.commit('feature'))).to be_empty expect(project.environments_for('master', commit: project.commit('feature')))
.to be_empty
end end
end end
...@@ -1664,15 +1667,23 @@ describe Project, models: true do ...@@ -1664,15 +1667,23 @@ describe Project, models: true do
end end
it 'returns environment when ref is set' do it 'returns environment when ref is set' do
expect(project.environments_for('master', project.commit)).to contain_exactly(environment) expect(project.environments_for('master', commit: project.commit))
.to contain_exactly(environment)
end end
it 'does not environment when ref is different' do it 'does not environment when ref is different' do
expect(project.environments_for('feature', project.commit)).to be_empty expect(project.environments_for('feature', commit: project.commit))
.to be_empty
end end
it 'does not return environment when commit is not part of deployment' do it 'does not return environment when commit is not part of deployment' do
expect(project.environments_for('master', project.commit('feature'))).to be_empty expect(project.environments_for('master', commit: project.commit('feature')))
.to be_empty
end
it 'returns environment when commit constraint is not set' do
expect(project.environments_for('master'))
.to contain_exactly(environment)
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment