Makes production environment the default environment for a project

This commit is contained in:
Tiago Botelho 2018-07-03 10:17:07 +01:00
parent 55e2df6c80
commit 99dea5fa13
3 changed files with 32 additions and 1 deletions

View file

@ -121,7 +121,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
end end
def metrics_redirect def metrics_redirect
environment = project.environments.with_state(:available).first environment = project.default_environment
if environment if environment
redirect_to environment_metrics_path(environment) redirect_to environment_metrics_path(environment)

View file

@ -1774,6 +1774,15 @@ class Project < ActiveRecord::Base
end end
end end
def default_environment
production_first = "(CASE WHEN name = 'production' THEN 0 ELSE 1 END), id ASC"
environments
.with_state(:available)
.reorder(production_first)
.first
end
def secret_variables_for(ref:, environment: nil) def secret_variables_for(ref:, environment: nil)
# EE would use the environment # EE would use the environment
if protected_for?(ref) if protected_for?(ref)

View file

@ -2292,6 +2292,28 @@ describe Project do
end end
end end
describe '#default_environment' do
let(:project) { create(:project) }
it 'returns production environment when it exists' do
production = create(:environment, name: "production", project: project)
create(:environment, name: 'staging', project: project)
expect(project.default_environment).to eq(production)
end
it 'returns first environment when no production environment exists' do
create(:environment, name: 'staging', project: project)
create(:environment, name: 'foo', project: project)
expect(project.default_environment).to eq(project.environments.first)
end
it 'returns nil when no available environment exists' do
expect(project.default_environment).to be_nil
end
end
describe '#secret_variables_for' do describe '#secret_variables_for' do
let(:project) { create(:project) } let(:project) { create(:project) }