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
0d7d7c10
Commit
0d7d7c10
authored
Aug 24, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use aspect-oriented design in CI/CD config entries
parent
5ced2d8d
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
36 additions
and
37 deletions
+36
-37
lib/gitlab/ci/config/entry/configurable.rb
lib/gitlab/ci/config/entry/configurable.rb
+2
-1
lib/gitlab/ci/config/entry/node.rb
lib/gitlab/ci/config/entry/node.rb
+6
-5
lib/gitlab/ci/config/entry/validatable.rb
lib/gitlab/ci/config/entry/validatable.rb
+11
-0
lib/gitlab/ci/config/entry/validator.rb
lib/gitlab/ci/config/entry/validator.rb
+1
-1
spec/lib/gitlab/ci/config/entry/configurable_spec.rb
spec/lib/gitlab/ci/config/entry/configurable_spec.rb
+11
-25
spec/lib/gitlab/ci/config/entry/validatable_spec.rb
spec/lib/gitlab/ci/config/entry/validatable_spec.rb
+5
-5
No files found.
lib/gitlab/ci/config/entry/configurable.rb
View file @
0d7d7c10
...
@@ -15,9 +15,10 @@ module Gitlab
...
@@ -15,9 +15,10 @@ module Gitlab
#
#
module
Configurable
module
Configurable
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
include
Validatable
included
do
included
do
include
Validatable
validations
do
validations
do
validates
:config
,
type:
Hash
validates
:config
,
type:
Hash
end
end
...
...
lib/gitlab/ci/config/entry/node.rb
View file @
0d7d7c10
...
@@ -16,8 +16,9 @@ module Gitlab
...
@@ -16,8 +16,9 @@ module Gitlab
@metadata
=
metadata
@metadata
=
metadata
@entries
=
{}
@entries
=
{}
@validator
=
self
.
class
.
validator
.
new
(
self
)
self
.
class
.
aspects
.
to_a
.
each
do
|
aspect
|
@validator
.
validate
(
:new
)
instance_exec
(
&
aspect
)
end
end
end
def
[]
(
key
)
def
[]
(
key
)
...
@@ -47,7 +48,7 @@ module Gitlab
...
@@ -47,7 +48,7 @@ module Gitlab
end
end
def
errors
def
errors
@validator
.
messages
+
descendants
.
flat_map
(
&
:errors
)
[]
end
end
def
value
def
value
...
@@ -79,8 +80,8 @@ module Gitlab
...
@@ -79,8 +80,8 @@ module Gitlab
def
self
.
default
def
self
.
default
end
end
def
self
.
validator
def
self
.
aspects
Validator
@aspects
||=
[]
end
end
end
end
end
end
...
...
lib/gitlab/ci/config/entry/validatable.rb
View file @
0d7d7c10
...
@@ -5,6 +5,17 @@ module Gitlab
...
@@ -5,6 +5,17 @@ module Gitlab
module
Validatable
module
Validatable
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
def
self
.
included
(
node
)
node
.
aspects
.
append
->
do
@validator
=
self
.
class
.
validator
.
new
(
self
)
@validator
.
validate
(
:new
)
end
end
def
errors
@validator
.
messages
+
descendants
.
flat_map
(
&
:errors
)
end
class_methods
do
class_methods
do
def
validator
def
validator
@validator
||=
Class
.
new
(
Entry
::
Validator
).
tap
do
|
validator
|
@validator
||=
Class
.
new
(
Entry
::
Validator
).
tap
do
|
validator
|
...
...
lib/gitlab/ci/config/entry/validator.rb
View file @
0d7d7c10
...
@@ -30,7 +30,7 @@ module Gitlab
...
@@ -30,7 +30,7 @@ module Gitlab
def
key_name
def
key_name
if
key
.
blank?
if
key
.
blank?
@entry
.
class
.
name
.
demodulize
.
underscore
.
humanize
@entry
.
class
.
name
.
to_s
.
demodulize
.
underscore
.
humanize
else
else
key
key
end
end
...
...
spec/lib/gitlab/ci/config/entry/configurable_spec.rb
View file @
0d7d7c10
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
Ci
::
Config
::
Entry
::
Configurable
do
describe
Gitlab
::
Ci
::
Config
::
Entry
::
Configurable
do
let
(
:entry
)
{
Class
.
new
}
let
(
:entry
)
do
Class
.
new
(
Gitlab
::
Ci
::
Config
::
Entry
::
Node
)
do
before
do
include
Gitlab
::
Ci
::
Config
::
Entry
::
Configurable
en
try
.
include
(
described_class
)
en
d
end
end
describe
'validations'
do
describe
'validations'
do
let
(
:validator
)
{
entry
.
validator
.
new
(
instance
)
}
context
'when entry is a hash'
do
let
(
:instance
)
{
entry
.
new
(
key:
'value'
)
}
before
do
entry
.
class_eval
do
attr_reader
:config
def
initialize
(
config
)
it
'correctly validates an instance'
do
@config
=
config
expect
(
instance
).
to
be_valid
end
end
end
validator
.
validate
end
end
context
'when entry
validator is invalid
'
do
context
'when entry
is not a hash
'
do
let
(
:instance
)
{
entry
.
new
(
'ls'
)
}
let
(
:instance
)
{
entry
.
new
(
'ls'
)
}
it
'returns invalid validator'
do
it
'invalidates the instance'
do
expect
(
validator
).
to
be_invalid
expect
(
instance
).
not_to
be_valid
end
end
context
'when entry instance is valid'
do
let
(
:instance
)
{
entry
.
new
(
key:
'value'
)
}
it
'returns valid validator'
do
expect
(
validator
).
to
be_valid
end
end
end
end
end
end
...
...
spec/lib/gitlab/ci/config/entry/validatable_spec.rb
View file @
0d7d7c10
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
Ci
::
Config
::
Entry
::
Validatable
do
describe
Gitlab
::
Ci
::
Config
::
Entry
::
Validatable
do
let
(
:entry
)
{
Class
.
new
}
let
(
:entry
)
do
Class
.
new
(
Gitlab
::
Ci
::
Config
::
Entry
::
Node
)
do
before
do
include
Gitlab
::
Ci
::
Config
::
Entry
::
Validatable
en
try
.
include
(
described_class
)
en
d
end
end
describe
'.validator'
do
describe
'.validator'
do
...
@@ -28,7 +28,7 @@ describe Gitlab::Ci::Config::Entry::Validatable do
...
@@ -28,7 +28,7 @@ describe Gitlab::Ci::Config::Entry::Validatable do
end
end
context
'when validating entry instance'
do
context
'when validating entry instance'
do
let
(
:entry_instance
)
{
entry
.
new
}
let
(
:entry_instance
)
{
entry
.
new
(
'something'
)
}
context
'when attribute is valid'
do
context
'when attribute is valid'
do
before
do
before
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