Make commit an optional arg for environments search
This commit is contained in:
parent
45bd5391d7
commit
5328e3b127
4 changed files with 42 additions and 31 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1646,15 +1646,18 @@ describe Project, models: true do
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
|
@ -1664,15 +1667,23 @@ describe Project, models: true do
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue