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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
4b2b154c
Commit
4b2b154c
authored
Nov 05, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Gitlab::Ci::Config::Normalizer
Use Hash#each_with_object to manipulate job hashes.
parent
5fd8933d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
44 deletions
+56
-44
lib/gitlab/ci/config/normalizer.rb
lib/gitlab/ci/config/normalizer.rb
+44
-42
lib/gitlab/ci/yaml_processor.rb
lib/gitlab/ci/yaml_processor.rb
+1
-1
spec/lib/gitlab/ci/config/normalizer_spec.rb
spec/lib/gitlab/ci/config/normalizer_spec.rb
+11
-1
No files found.
lib/gitlab/ci/config/normalizer.rb
View file @
4b2b154c
...
...
@@ -4,56 +4,58 @@ module Gitlab
module
Ci
class
Config
class
Normalizer
def
initialize
(
jobs_config
)
@jobs_config
=
jobs_config
end
def
normalize_jobs
parallelized_jobs
=
parallelize_jobs
parallelize_dependencies
(
parallelized_jobs
)
class
<<
self
def
normalize_jobs
(
jobs_config
)
parallelized_jobs
=
extract_parallelized_jobs
(
jobs_config
)
parallelized_config
=
parallelize_jobs
(
jobs_config
,
parallelized_jobs
)
parallelize_dependencies
(
parallelized_config
,
parallelized_jobs
)
end
private
def
parallelize_jobs
def
extract_parallelized_jobs
(
jobs_config
)
parallelized_jobs
=
{}
@jobs_config
=
@jobs_config
.
map
do
|
name
,
config
|
jobs_config
.
each
do
|
job_
name
,
config
|
if
config
[
:parallel
]
total
=
config
[
:parallel
]
names
=
self
.
class
.
parallelize_job_names
(
name
,
total
)
parallelized_jobs
[
name
]
=
names
.
map
(
&
:first
)
Hash
[
names
.
collect
{
|
job_name
,
index
|
[
job_name
.
to_sym
,
config
.
merge
(
name:
job_name
,
instance:
index
)]
}]
else
{
name
=>
config
}
parallelized_jobs
[
job_name
]
=
parallelize_job_names
(
job_name
,
config
[
:parallel
])
end
end
end
.
reduce
(
:merge
)
parallelized_jobs
end
def
parallelize_dependencies
(
parallelized_jobs
)
@jobs_config
.
map
do
|
name
,
config
|
if
config
[
:dependencies
]
deps
=
config
[
:dependencies
].
map
do
|
dep
|
if
parallelized_jobs
.
keys
.
include?
(
dep
.
to_sym
)
parallelized_jobs
[
dep
.
to_sym
]
def
parallelize_jobs
(
jobs_config
,
parallelized_jobs
)
jobs_config
.
each_with_object
({})
do
|
(
job_name
,
config
),
hash
|
if
parallelized_jobs
.
keys
.
include?
(
job_name
)
parallelized_jobs
[
job_name
].
each
{
|
name
,
index
|
hash
[
name
.
to_sym
]
=
config
.
merge
(
name:
name
,
instance:
index
)
}
else
dep
hash
[
job_name
]
=
config
end
hash
end
end
end
.
flatten
{
name
=>
config
.
merge
(
dependencies:
deps
)
}
def
parallelize_dependencies
(
parallelized_config
,
parallelized_jobs
)
parallelized_config
.
each_with_object
({})
do
|
(
job_name
,
config
),
hash
|
intersection
=
config
[
:dependencies
]
&
parallelized_jobs
.
keys
.
map
(
&
:to_s
)
if
intersection
&&
intersection
.
any?
deps
=
intersection
.
map
{
|
dep
|
parallelized_jobs
[
dep
.
to_sym
].
map
(
&
:first
)
}.
flatten
hash
[
job_name
]
=
config
.
merge
(
dependencies:
deps
)
else
{
name
=>
config
}
hash
[
job_name
]
=
config
end
hash
end
end
.
reduce
(
:merge
)
end
def
self
.
parallelize_job_names
(
name
,
total
)
def
parallelize_job_names
(
name
,
total
)
Array
.
new
(
total
)
{
|
index
|
[
"
#{
name
}
#{
index
+
1
}
/
#{
total
}
"
,
index
+
1
]
}
end
end
end
end
end
end
lib/gitlab/ci/yaml_processor.rb
View file @
4b2b154c
...
...
@@ -104,7 +104,7 @@ module Gitlab
##
# Jobs
#
@jobs
=
Ci
::
Config
::
Normalizer
.
n
ew
(
@ci_config
.
jobs
).
normalize_jobs
@jobs
=
Ci
::
Config
::
Normalizer
.
n
ormalize_jobs
(
@ci_config
.
jobs
)
@jobs
.
each
do
|
name
,
job
|
# logical validation for job
...
...
spec/lib/gitlab/ci/config/normalizer_spec.rb
View file @
4b2b154c
...
...
@@ -8,7 +8,7 @@ describe Gitlab::Ci::Config::Normalizer do
let
(
:config
)
{
{
job_name
=>
job_config
}
}
describe
'.normalize_jobs'
do
subject
{
described_class
.
n
ew
(
config
).
normalize_jobs
}
subject
{
described_class
.
n
ormalize_jobs
(
config
)
}
it
'does not have original job'
do
is_expected
.
not_to
include
(
job_name
)
...
...
@@ -30,6 +30,16 @@ describe Gitlab::Ci::Config::Normalizer do
expect
(
configs
).
to
all
(
eq
(
original_config
))
end
context
'when jobs depend on parallelized jobs'
do
let
(
:config
)
{
{
job_name
=>
job_config
,
other_job:
{
script:
'echo 1'
,
dependencies:
[
job_name
.
to_s
]
}
}
}
it
'parallelizes dependencies'
do
job_names
=
described_class
.
send
(
:parallelize_job_names
,
job_name
,
5
).
map
(
&
:first
)
expect
(
subject
[
:other_job
][
:dependencies
]).
to
include
(
*
job_names
)
end
end
end
describe
'.parallelize_job_names'
do
...
...
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