Commit 99dea5fa authored by Tiago Botelho's avatar Tiago Botelho

Makes production environment the default environment for a project

parent 55e2df6c
......@@ -121,7 +121,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
end
def metrics_redirect
environment = project.environments.with_state(:available).first
environment = project.default_environment
if environment
redirect_to environment_metrics_path(environment)
......
......@@ -1774,6 +1774,15 @@ class Project < ActiveRecord::Base
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)
# EE would use the environment
if protected_for?(ref)
......
......@@ -2292,6 +2292,28 @@ describe Project do
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
let(:project) { create(:project) }
......
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