Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
dedccdc1
Commit
dedccdc1
authored
Apr 08, 2022
by
Alishan Ladhani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Consolidate Protected Environment batch loading logic
parent
87bf25e6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
116 additions
and
40 deletions
+116
-40
ee/app/models/preloaders/environments/protected_environment_preloader.rb
...reloaders/environments/protected_environment_preloader.rb
+4
-12
ee/app/models/protected_environment.rb
ee/app/models/protected_environment.rb
+23
-14
ee/spec/models/protected_environment_spec.rb
ee/spec/models/protected_environment_spec.rb
+89
-14
No files found.
ee/app/models/preloaders/environments/protected_environment_preloader.rb
View file @
dedccdc1
...
@@ -16,19 +16,11 @@ module Preloaders
...
@@ -16,19 +16,11 @@ module Preloaders
def
execute
def
execute
return
if
environments
.
empty?
return
if
environments
.
empty?
project
=
environments
.
first
.
project
associated_protected_environments
=
project_id
=
project
.
id
ProtectedEnvironment
.
for_environments
(
environments
).
preload
(
:deploy_access_levels
)
group_ids
=
project
.
ancestors_upto_ids
names
=
environments
.
map
(
&
:name
)
project_protected_environments
=
associated_protected_environments
.
select
(
&
:project_level?
).
index_by
(
&
:name
)
tiers
=
environments
.
map
(
&
:tier
)
group_protected_environments
=
associated_protected_environments
.
select
(
&
:group_level?
).
index_by
(
&
:name
)
project_protected_environments
=
ProtectedEnvironment
.
preload
(
:deploy_access_levels
)
.
where
(
project_id:
project_id
,
name:
names
)
.
index_by
(
&
:name
)
group_protected_environments
=
ProtectedEnvironment
.
preload
(
:deploy_access_levels
)
.
where
(
group_id:
group_ids
,
name:
tiers
)
.
index_by
(
&
:name
)
environments
.
each
do
|
environment
|
environments
.
each
do
|
environment
|
protected_environments
||=
[]
protected_environments
||=
[]
...
...
ee/app/models/protected_environment.rb
View file @
dedccdc1
...
@@ -59,12 +59,21 @@ class ProtectedEnvironment < ApplicationRecord
...
@@ -59,12 +59,21 @@ class ProtectedEnvironment < ApplicationRecord
key
=
"protected_environment:for_environment:
#{
environment
.
id
}
"
key
=
"protected_environment:for_environment:
#{
environment
.
id
}
"
::
Gitlab
::
SafeRequestStore
.
fetch
(
key
)
do
::
Gitlab
::
SafeRequestStore
.
fetch
(
key
)
{
for_environments
([
environment
])
}
from_union
([
end
where
(
project:
environment
.
project_id
,
name:
environment
.
name
),
where
(
group:
environment
.
project
.
ancestors_upto_ids
,
name:
environment
.
tier
)
def
for_environments
(
environments
)
])
raise
ArgumentError
,
'Environments must be in the same project'
if
environments
.
map
(
&
:project_id
).
uniq
.
size
>
1
end
project_id
=
environments
.
first
.
project_id
group_ids
=
environments
.
first
.
project
.
ancestors_upto_ids
names
=
environments
.
map
(
&
:name
)
tiers
=
environments
.
map
(
&
:tier
)
from_union
([
where
(
project:
project_id
,
name:
names
),
where
(
group:
group_ids
,
name:
tiers
)
])
end
end
end
end
...
@@ -81,14 +90,6 @@ class ProtectedEnvironment < ApplicationRecord
...
@@ -81,14 +90,6 @@ class ProtectedEnvironment < ApplicationRecord
end
end
end
end
private
def
valid_tier_name
unless
Environment
.
tiers
[
name
]
errors
.
add
(
:name
,
"must be one of environment tiers:
#{
Environment
.
tiers
.
keys
.
join
(
', '
)
}
."
)
end
end
def
project_level?
def
project_level?
project_id
.
present?
project_id
.
present?
end
end
...
@@ -96,4 +97,12 @@ class ProtectedEnvironment < ApplicationRecord
...
@@ -96,4 +97,12 @@ class ProtectedEnvironment < ApplicationRecord
def
group_level?
def
group_level?
group_id
.
present?
group_id
.
present?
end
end
private
def
valid_tier_name
unless
Environment
.
tiers
[
name
]
errors
.
add
(
:name
,
"must be one of environment tiers:
#{
Environment
.
tiers
.
keys
.
join
(
', '
)
}
."
)
end
end
end
end
ee/spec/models/protected_environment_spec.rb
View file @
dedccdc1
...
@@ -179,6 +179,38 @@ RSpec.describe ProtectedEnvironment do
...
@@ -179,6 +179,38 @@ RSpec.describe ProtectedEnvironment do
end
end
end
end
describe
'#project_level?'
do
subject
{
protected_environment
.
project_level?
}
context
'for a project-level protected environment'
do
let
(
:protected_environment
)
{
create
(
:protected_environment
,
:project_level
)
}
it
{
is_expected
.
to
be_truthy
}
end
context
'for a group-level protected environment'
do
let
(
:protected_environment
)
{
create
(
:protected_environment
,
:group_level
)
}
it
{
is_expected
.
to
be_falsey
}
end
end
describe
'#group_level?'
do
subject
{
protected_environment
.
group_level?
}
context
'for a group-level protected environment'
do
let
(
:protected_environment
)
{
create
(
:protected_environment
,
:group_level
)
}
it
{
is_expected
.
to
be_truthy
}
end
context
'for a project-level protected environment'
do
let
(
:protected_environment
)
{
create
(
:protected_environment
,
:project_level
)
}
it
{
is_expected
.
to
be_falsey
}
end
end
describe
'.sorted_by_name'
do
describe
'.sorted_by_name'
do
subject
(
:protected_environments
)
{
described_class
.
sorted_by_name
}
subject
(
:protected_environments
)
{
described_class
.
sorted_by_name
}
...
@@ -294,7 +326,7 @@ RSpec.describe ProtectedEnvironment do
...
@@ -294,7 +326,7 @@ RSpec.describe ProtectedEnvironment do
subject
{
described_class
.
for_environment
(
environment
)
}
subject
{
described_class
.
for_environment
(
environment
)
}
it
{
is_expected
.
to
eq
([
protected_environment
])
}
it
{
is_expected
.
to
match_array
([
protected_environment
])
}
it
'caches result'
,
:request_store
do
it
'caches result'
,
:request_store
do
described_class
.
for_environment
(
environment
).
to_a
described_class
.
for_environment
(
environment
).
to_a
...
@@ -303,31 +335,54 @@ RSpec.describe ProtectedEnvironment do
...
@@ -303,31 +335,54 @@ RSpec.describe ProtectedEnvironment do
.
not_to
exceed_query_limit
(
0
)
.
not_to
exceed_query_limit
(
0
)
end
end
context
'when environment
is a different name
'
do
context
'when environment
does not exist
'
do
let!
(
:environment
)
{
create
(
:environment
,
name:
'staging'
,
project:
project
)
}
let!
(
:environment
)
{
}
it
{
is_expected
.
to
be_empty
}
it
'raises an error'
do
expect
{
subject
}.
to
raise_error
(
ArgumentError
)
end
end
end
context
'when environment exists in a different project'
do
it
'calls .for_environments with the environment'
do
let!
(
:environment
)
{
create
(
:environment
,
name:
'production'
,
project:
create
(
:project
))
}
expect
(
described_class
).
to
receive
(
:for_environments
).
with
([
environment
]).
and_call_original
described_class
.
for_environment
(
environment
)
end
end
describe
'.for_environments'
do
let_it_be
(
:group
)
{
create
(
:group
)
}
let_it_be
(
:project
,
reload:
true
)
{
create
(
:project
,
group:
group
)
}
let!
(
:environments
)
{
[
create
(
:environment
,
name:
'production'
,
project:
project
)]
}
let!
(
:protected_environment
)
{
create
(
:protected_environment
,
name:
'production'
,
project:
project
)
}
subject
{
described_class
.
for_environments
(
environments
)
}
it
{
is_expected
.
to
match_array
([
protected_environment
])
}
it
'raises an error if environments belong to more than one project'
do
expect
{
described_class
.
for_environments
([
create
(
:environment
),
create
(
:environment
)])
}
.
to
raise_error
(
ArgumentError
,
'Environments must be in the same project'
)
end
context
'when environment is a different name'
do
let!
(
:environments
)
{
[
create
(
:environment
,
name:
'staging'
,
project:
project
)]
}
it
{
is_expected
.
to
be_empty
}
it
{
is_expected
.
to
be_empty
}
end
end
context
'when environment
does not exis
t'
do
context
'when environment
exists in a different projec
t'
do
let!
(
:environment
)
{
}
let!
(
:environment
s
)
{
[
create
(
:environment
,
name:
'production'
,
project:
create
(
:project
))]
}
it
'raises an error'
do
it
{
is_expected
.
to
be_empty
}
expect
{
subject
}.
to
raise_error
(
ArgumentError
)
end
end
end
context
'with group-level protected environment'
do
context
'with group-level protected environment'
do
let!
(
:group_protected_environment
)
{
create
(
:protected_environment
,
:production
,
:group_level
,
group:
group
)
}
let!
(
:group_protected_environment
)
{
create
(
:protected_environment
,
:production
,
:group_level
,
group:
group
)
}
context
'with project-level production environment'
do
context
'with project-level production environment'
do
let!
(
:environment
)
{
create
(
:environment
,
:production
,
project:
project
)
}
let!
(
:environment
s
)
{
[
create
(
:environment
,
:production
,
project:
project
)]
}
it
'has multiple protections'
do
it
'has multiple protections'
do
is_expected
.
to
contain_exactly
(
protected_environment
,
group_protected_environment
)
is_expected
.
to
contain_exactly
(
protected_environment
,
group_protected_environment
)
...
@@ -337,19 +392,39 @@ RSpec.describe ProtectedEnvironment do
...
@@ -337,19 +392,39 @@ RSpec.describe ProtectedEnvironment do
let!
(
:protected_environment
)
{
}
let!
(
:protected_environment
)
{
}
it
'has only group-level protection'
do
it
'has only group-level protection'
do
is_expected
.
to
eq
([
group_protected_environment
])
is_expected
.
to
match_array
([
group_protected_environment
])
end
end
end
end
end
end
context
'with staging environment'
do
context
'with staging environment'
do
let
(
:environment
)
{
create
(
:environment
,
:staging
,
project:
project
)
}
let
(
:environment
s
)
{
[
create
(
:environment
,
:staging
,
project:
project
)]
}
it
'does not have any protections'
do
it
'does not have any protections'
do
is_expected
.
to
be_empty
is_expected
.
to
be_empty
end
end
end
end
end
end
context
'with multiple environments'
do
let!
(
:protected_environment
)
{}
let!
(
:environments
)
do
[
create
(
:environment
,
name:
'production'
,
project:
project
),
create
(
:environment
,
name:
'canary'
,
project:
project
),
create
(
:environment
,
name:
'dev'
,
project:
project
)
]
end
let!
(
:protected_environments
)
do
[
create
(
:protected_environment
,
name:
'production'
,
project:
project
),
create
(
:protected_environment
,
name:
'canary'
,
project:
project
)
]
end
it
{
is_expected
.
to
match_array
(
protected_environments
)
}
end
end
end
def
create_deploy_access_level
(
protected_environment
,
**
opts
)
def
create_deploy_access_level
(
protected_environment
,
**
opts
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment