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
4030a9ae
Commit
4030a9ae
authored
Feb 23, 2021
by
Pedro Pombeiro
Committed by
Fabio Pitino
Feb 23, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add #sorted_collection in Variables::Collection
parent
6863012f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
14 deletions
+68
-14
lib/gitlab/ci/variables/collection.rb
lib/gitlab/ci/variables/collection.rb
+9
-1
lib/gitlab/ci/variables/collection/sort.rb
lib/gitlab/ci/variables/collection/sort.rb
+6
-5
spec/lib/gitlab/ci/variables/collection/sort_spec.rb
spec/lib/gitlab/ci/variables/collection/sort_spec.rb
+8
-8
spec/lib/gitlab/ci/variables/collection_spec.rb
spec/lib/gitlab/ci/variables/collection_spec.rb
+45
-0
No files found.
lib/gitlab/ci/variables/collection.rb
View file @
4030a9ae
...
@@ -6,8 +6,11 @@ module Gitlab
...
@@ -6,8 +6,11 @@ module Gitlab
class
Collection
class
Collection
include
Enumerable
include
Enumerable
def
initialize
(
variables
=
[])
attr_reader
:errors
def
initialize
(
variables
=
[],
errors
=
nil
)
@variables
=
[]
@variables
=
[]
@errors
=
errors
variables
.
each
{
|
variable
|
self
.
append
(
variable
)
}
variables
.
each
{
|
variable
|
self
.
append
(
variable
)
}
end
end
...
@@ -42,6 +45,11 @@ module Gitlab
...
@@ -42,6 +45,11 @@ module Gitlab
.
map
{
|
env
|
[
env
.
fetch
(
:key
),
env
.
fetch
(
:value
)]
}
.
map
{
|
env
|
[
env
.
fetch
(
:key
),
env
.
fetch
(
:value
)]
}
.
to_h
.
with_indifferent_access
.
to_h
.
with_indifferent_access
end
end
# Returns a sorted Collection object, and sets errors property in case of an error
def
sorted_collection
(
project
)
Sort
.
new
(
self
,
project
).
collection
end
end
end
end
end
end
end
...
...
lib/gitlab/ci/variables/collection/sort
ed
.rb
→
lib/gitlab/ci/variables/collection/sort.rb
View file @
4030a9ae
...
@@ -4,7 +4,7 @@ module Gitlab
...
@@ -4,7 +4,7 @@ module Gitlab
module
Ci
module
Ci
module
Variables
module
Variables
class
Collection
class
Collection
class
Sort
ed
class
Sort
include
TSort
include
TSort
include
Gitlab
::
Utils
::
StrongMemoize
include
Gitlab
::
Utils
::
StrongMemoize
...
@@ -35,11 +35,12 @@ module Gitlab
...
@@ -35,11 +35,12 @@ module Gitlab
end
end
end
end
#
sort sorts an array
of variables, ignoring unknown variable references.
#
collection sorts a collection
of variables, ignoring unknown variable references.
# If a circular variable reference is found,
the original array
is returned
# If a circular variable reference is found,
a new collection with the original array and an error
is returned
def
sort
def
collection
return
@collection
if
Feature
.
disabled?
(
:variable_inside_variable
,
@project
)
return
@collection
if
Feature
.
disabled?
(
:variable_inside_variable
,
@project
)
return
@collection
if
errors
return
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
@collection
,
errors
)
if
errors
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
tsort
)
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
tsort
)
end
end
...
...
spec/lib/gitlab/ci/variables/collection/sort
ed
_spec.rb
→
spec/lib/gitlab/ci/variables/collection/sort_spec.rb
View file @
4030a9ae
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
require
'spec_helper'
require
'spec_helper'
RSpec
.
describe
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
ed
do
RSpec
.
describe
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
do
describe
'#initialize with non-Collection value'
do
describe
'#initialize with non-Collection value'
do
let_it_be
(
:project_with_flag_disabled
)
{
create
(
:project
)
}
let_it_be
(
:project_with_flag_disabled
)
{
create
(
:project
)
}
let_it_be
(
:project_with_flag_enabled
)
{
create
(
:project
)
}
let_it_be
(
:project_with_flag_enabled
)
{
create
(
:project
)
}
...
@@ -12,7 +12,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
...
@@ -12,7 +12,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
end
end
context
'when FF :variable_inside_variable is disabled'
do
context
'when FF :variable_inside_variable is disabled'
do
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
ed
.
new
([],
project_with_flag_disabled
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
.
new
([],
project_with_flag_disabled
)
}
it
'raises ArgumentError'
do
it
'raises ArgumentError'
do
expect
{
subject
}.
to
raise_error
(
ArgumentError
,
/Collection object was expected/
)
expect
{
subject
}.
to
raise_error
(
ArgumentError
,
/Collection object was expected/
)
...
@@ -20,7 +20,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
...
@@ -20,7 +20,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
end
end
context
'when FF :variable_inside_variable is enabled'
do
context
'when FF :variable_inside_variable is enabled'
do
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
ed
.
new
([],
project_with_flag_enabled
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
.
new
([],
project_with_flag_enabled
)
}
it
'raises ArgumentError'
do
it
'raises ArgumentError'
do
expect
{
subject
}.
to
raise_error
(
ArgumentError
,
/Collection object was expected/
)
expect
{
subject
}.
to
raise_error
(
ArgumentError
,
/Collection object was expected/
)
...
@@ -83,7 +83,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
...
@@ -83,7 +83,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
with_them
do
with_them
do
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
ed
.
new
(
collection
,
project_with_flag_disabled
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
.
new
(
collection
,
project_with_flag_disabled
)
}
it
'does not report error'
do
it
'does not report error'
do
expect
(
subject
.
errors
).
to
eq
(
nil
)
expect
(
subject
.
errors
).
to
eq
(
nil
)
...
@@ -135,7 +135,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
...
@@ -135,7 +135,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
with_them
do
with_them
do
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
ed
.
new
(
collection
,
project_with_flag_enabled
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
.
new
(
collection
,
project_with_flag_enabled
)
}
it
'errors matches expected validation result'
do
it
'errors matches expected validation result'
do
expect
(
subject
.
errors
).
to
eq
(
validation_result
)
expect
(
subject
.
errors
).
to
eq
(
validation_result
)
...
@@ -149,7 +149,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
...
@@ -149,7 +149,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
end
end
end
end
describe
'#
sort
'
do
describe
'#
collection
'
do
context
'when FF :variable_inside_variable is disabled'
do
context
'when FF :variable_inside_variable is disabled'
do
before
do
before
do
stub_feature_flags
(
variable_inside_variable:
false
)
stub_feature_flags
(
variable_inside_variable:
false
)
...
@@ -202,7 +202,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
...
@@ -202,7 +202,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
let_it_be
(
:project
)
{
create
(
:project
)
}
let_it_be
(
:project
)
{
create
(
:project
)
}
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
ed
.
new
(
collection
,
project
).
sort
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
.
new
(
collection
,
project
).
collection
}
it
'does not expand variables'
do
it
'does not expand variables'
do
is_expected
.
to
be
(
collection
)
is_expected
.
to
be
(
collection
)
...
@@ -280,7 +280,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
...
@@ -280,7 +280,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sorted do
let_it_be
(
:project
)
{
create
(
:project
)
}
let_it_be
(
:project
)
{
create
(
:project
)
}
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
let
(
:collection
)
{
Gitlab
::
Ci
::
Variables
::
Collection
.
new
(
variables
)
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
ed
.
new
(
collection
,
project
).
sort
}
subject
{
Gitlab
::
Ci
::
Variables
::
Collection
::
Sort
.
new
(
collection
,
project
).
collection
}
it
'returns correctly sorted variables'
do
it
'returns correctly sorted variables'
do
expect
(
subject
.
map
{
|
var
|
var
[
:key
]
}).
to
eq
(
result
)
expect
(
subject
.
map
{
|
var
|
var
[
:key
]
}).
to
eq
(
result
)
...
...
spec/lib/gitlab/ci/variables/collection_spec.rb
View file @
4030a9ae
...
@@ -121,4 +121,49 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
...
@@ -121,4 +121,49 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
expect
(
collection
.
to_hash
).
not_to
include
(
TEST1
:
'test-1'
)
expect
(
collection
.
to_hash
).
not_to
include
(
TEST1
:
'test-1'
)
end
end
end
end
describe
'#sorted_collection'
do
let!
(
:project
)
{
create
(
:project
)
}
subject
{
collection
.
sorted_collection
(
project
)
}
context
'when FF :variable_inside_variable is disabled'
do
before
do
stub_feature_flags
(
variable_inside_variable:
false
)
end
let
(
:collection
)
do
described_class
.
new
.
append
(
key:
'A'
,
value:
'test-$B'
)
.
append
(
key:
'B'
,
value:
'test-$C'
)
.
append
(
key:
'C'
,
value:
'test'
)
end
it
{
is_expected
.
to
be
(
collection
)
}
end
context
'when FF :variable_inside_variable is enabled'
do
before
do
stub_feature_flags
(
variable_inside_variable:
[
project
])
end
let
(
:collection
)
do
described_class
.
new
.
append
(
key:
'A'
,
value:
'test-$B'
)
.
append
(
key:
'B'
,
value:
'test-$C'
)
.
append
(
key:
'C'
,
value:
'test'
)
end
it
{
is_expected
.
to
be_a
(
Gitlab
::
Ci
::
Variables
::
Collection
)
}
it
'returns sorted collection'
do
expect
(
subject
.
to_a
).
to
eq
(
[
{
key:
'C'
,
value:
'test'
,
masked:
false
,
public:
true
},
{
key:
'B'
,
value:
'test-$C'
,
masked:
false
,
public:
true
},
{
key:
'A'
,
value:
'test-$B'
,
masked:
false
,
public:
true
}
])
end
end
end
end
end
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