summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiago Botelho <tiagonbotelho@hotmail.com>2018-07-03 10:17:07 +0100
committerTiago Botelho <tiagonbotelho@hotmail.com>2018-07-04 09:35:25 +0100
commit99dea5fa13286346f8c7066d803eb04f4e989663 (patch)
tree53f87b3caca11b825dee9de433815f2fd69174b0
parent55e2df6c804202a39d0165289988062a693087aa (diff)
downloadgitlab-ce-99dea5fa13286346f8c7066d803eb04f4e989663.tar.gz
Makes production environment the default environment for a project
-rw-r--r--app/controllers/projects/environments_controller.rb2
-rw-r--r--app/models/project.rb9
-rw-r--r--spec/models/project_spec.rb22
3 files changed, 32 insertions, 1 deletions
diff --git a/app/controllers/projects/environments_controller.rb b/app/controllers/projects/environments_controller.rb
index 1a586105a6d..27b7425b965 100644
--- a/app/controllers/projects/environments_controller.rb
+++ b/app/controllers/projects/environments_controller.rb
@@ -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)
diff --git a/app/models/project.rb b/app/models/project.rb
index d91d7dcfe9a..c2afd2dfec5 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -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)
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index a2f8fac2f38..312164558fb 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -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) }