Commit c2ec3027 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement method that returns environment's folder

`Environment#folder_name` method returns a folder name that holds the
environment. If it is a top-level environment it will return environment
name, folder name otherwise.
parent ae3b06ab
...@@ -82,12 +82,7 @@ class Environment < ActiveRecord::Base ...@@ -82,12 +82,7 @@ class Environment < ActiveRecord::Base
def set_environment_type def set_environment_type
names = name.split('/') names = name.split('/')
self.environment_type = self.environment_type = names.many? ? names.first : nil
if names.many?
names.first
else
nil
end
end end
def includes_commit?(commit) def includes_commit?(commit)
...@@ -101,7 +96,7 @@ class Environment < ActiveRecord::Base ...@@ -101,7 +96,7 @@ class Environment < ActiveRecord::Base
end end
def update_merge_request_metrics? def update_merge_request_metrics?
(environment_type || name) == "production" folder_name == "production"
end end
def first_deployment_for(commit) def first_deployment_for(commit)
...@@ -223,6 +218,10 @@ class Environment < ActiveRecord::Base ...@@ -223,6 +218,10 @@ class Environment < ActiveRecord::Base
format: :json) format: :json)
end end
def folder_name
self.environment_type || self.name
end
private private
# Slugifying a name may remove the uniqueness guarantee afforded by it being # Slugifying a name may remove the uniqueness guarantee afforded by it being
......
...@@ -27,7 +27,7 @@ class EnvironmentEntity < Grape::Entity ...@@ -27,7 +27,7 @@ class EnvironmentEntity < Grape::Entity
end end
expose :folder_path do |environment| expose :folder_path do |environment|
folder_project_environments_path(environment.project, environment.environment_type || environment.name) folder_project_environments_path(environment.project, environment.folder_name)
end end
expose :created_at, :updated_at expose :created_at, :updated_at
......
...@@ -36,9 +36,9 @@ class EnvironmentSerializer < BaseSerializer ...@@ -36,9 +36,9 @@ class EnvironmentSerializer < BaseSerializer
private private
def itemize(resource) def itemize(resource)
items = resource.order('folder_name ASC') items = resource.order('folder ASC')
.group('COALESCE(environment_type, name)') .group('COALESCE(environment_type, name)')
.select('COALESCE(environment_type, name) AS folder_name', .select('COALESCE(environment_type, name) AS folder',
'COUNT(*) AS size', 'MAX(id) AS last_id') 'COUNT(*) AS size', 'MAX(id) AS last_id')
# It makes a difference when you call `paginate` method, because # It makes a difference when you call `paginate` method, because
...@@ -49,7 +49,7 @@ class EnvironmentSerializer < BaseSerializer ...@@ -49,7 +49,7 @@ class EnvironmentSerializer < BaseSerializer
environments = resource.where(id: items.map(&:last_id)).index_by(&:id) environments = resource.where(id: items.map(&:last_id)).index_by(&:id)
items.map do |item| items.map do |item|
Item.new(item.folder_name, item.size, environments[item.last_id]) Item.new(item.folder, item.size, environments[item.last_id])
end end
end end
end end
...@@ -54,6 +54,28 @@ describe Environment do ...@@ -54,6 +54,28 @@ describe Environment do
end end
end end
describe '#folder_name' do
context 'when it is inside a folder' do
subject(:environment) do
create(:environment, name: 'staging/review-1')
end
it 'returns a top-level folder name' do
expect(environment.folder_name).to eq 'staging'
end
end
context 'when the environment if a top-level item itself' do
subject(:environment) do
create(:environment, name: 'production')
end
it 'returns an environment name' do
expect(environment.folder_name).to eq 'production'
end
end
end
describe '#nullify_external_url' do describe '#nullify_external_url' do
it 'replaces a blank url with nil' do it 'replaces a blank url with nil' do
env = build(:environment, external_url: "") env = build(:environment, external_url: "")
......
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