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
4e80792b
Commit
4e80792b
authored
Feb 17, 2021
by
Pedro Pombeiro
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add lookup methods in `Variables::Collection`
Allows for quick lookup when expanding variable values.
parent
f962e3ad
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
1 deletion
+76
-1
lib/gitlab/ci/variables/collection.rb
lib/gitlab/ci/variables/collection.rb
+22
-1
spec/lib/gitlab/ci/variables/collection_spec.rb
spec/lib/gitlab/ci/variables/collection_spec.rb
+54
-0
No files found.
lib/gitlab/ci/variables/collection.rb
View file @
4e80792b
...
...
@@ -10,13 +10,18 @@ module Gitlab
def
initialize
(
variables
=
[],
errors
=
nil
)
@variables
=
[]
@var_hash
=
{}
@errors
=
errors
variables
.
each
{
|
variable
|
self
.
append
(
variable
)
}
end
def
append
(
resource
)
tap
{
@variables
.
append
(
Collection
::
Item
.
fabricate
(
resource
))
}
tap
do
item
=
Collection
::
Item
.
fabricate
(
resource
)
@variables
.
append
(
item
)
@var_hash
[
item
[
:key
]]
=
item
end
end
def
concat
(
resources
)
...
...
@@ -36,10 +41,26 @@ module Gitlab
end
end
def
[]
(
key
)
@var_hash
[
key
]
end
def
size
@variables
.
size
end
def
to_runner_variables
self
.
map
(
&
:to_runner_variable
)
end
def
include?
(
obj
)
return
false
unless
obj
.
is_a?
(
Hash
)
&&
obj
.
has_key?
(
:key
)
key
=
obj
.
fetch
(
:key
)
found_var
=
@var_hash
[
key
]
!
found_var
.
nil?
&&
found_var
.
to_runner_variable
==
Collection
::
Item
.
fabricate
(
obj
).
to_runner_variable
end
def
to_hash
self
.
to_runner_variables
.
map
{
|
env
|
[
env
.
fetch
(
:key
),
env
.
fetch
(
:value
)]
}
...
...
spec/lib/gitlab/ci/variables/collection_spec.rb
View file @
4e80792b
...
...
@@ -98,6 +98,60 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
end
end
describe
'#[]'
do
variable
=
{
key:
'VAR'
,
value:
'value'
,
public:
true
,
masked:
false
}
collection
=
described_class
.
new
([
variable
])
it
'returns nil for a non-existent variable name'
do
expect
(
collection
[
'UNKNOWN_VAR'
]).
to
be_nil
end
it
'returns Item for an existent variable name'
do
expect
(
collection
[
'VAR'
]).
to
be_an_instance_of
(
Gitlab
::
Ci
::
Variables
::
Collection
::
Item
)
expect
(
collection
[
'VAR'
].
to_runner_variable
).
to
eq
(
variable
)
end
end
describe
'#size'
do
it
'returns zero for empty collection'
do
collection
=
described_class
.
new
([])
expect
(
collection
.
size
).
to
eq
(
0
)
end
it
'returns 2 for collection with 2 variables'
do
collection
=
described_class
.
new
(
[
{
key:
'VAR1'
,
value:
'value'
,
public:
true
,
masked:
false
},
{
key:
'VAR2'
,
value:
'value'
,
public:
true
,
masked:
false
}
])
expect
(
collection
.
size
).
to
eq
(
2
)
end
end
describe
'#include?'
do
it
'returns false for unknown variable'
do
collection
=
described_class
.
new
([])
expect
(
collection
.
include?
({
key:
'VAR'
,
value:
'value'
,
public:
true
,
masked:
false
})).
to
eq
(
false
)
end
it
'returns false for unsupported scenario of variable name argument'
do
collection
=
described_class
.
new
([{
key:
'VAR'
,
value:
'value'
,
public:
true
,
masked:
false
}])
expect
(
collection
.
include?
(
'VAR'
)).
to
eq
(
false
)
end
it
'returns true for known variable'
do
variable
=
{
key:
'VAR'
,
value:
'value'
,
public:
true
,
masked:
false
}
collection
=
described_class
.
new
([
variable
])
expect
(
collection
.
include?
(
variable
)).
to
eq
(
true
)
end
end
describe
'#to_runner_variables'
do
it
'creates an array of hashes in a runner-compatible format'
do
collection
=
described_class
.
new
([{
key:
'TEST'
,
value:
'1'
}])
...
...
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