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
iv
gitlab-ce
Commits
652de0b8
Commit
652de0b8
authored
Dec 07, 2015
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor CI YAML processor's validators
parent
66f658a9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
29 deletions
+43
-29
lib/ci/gitlab_ci_yaml_processor.rb
lib/ci/gitlab_ci_yaml_processor.rb
+40
-26
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
+3
-3
No files found.
lib/ci/gitlab_ci_yaml_processor.rb
View file @
652de0b8
...
@@ -132,26 +132,36 @@ module Ci
...
@@ -132,26 +132,36 @@ module Ci
end
end
def
validate_job!
(
name
,
job
)
def
validate_job!
(
name
,
job
)
validate_job_name!
(
name
)
validate_job_keys!
(
name
,
job
)
validate_job_types!
(
name
,
job
)
validate_job_stage!
(
name
,
job
)
if
job
[
:stage
]
validate_job_cache!
(
name
,
job
)
if
job
[
:cache
]
validate_job_artifacts!
(
name
,
job
)
if
job
[
:artifacts
]
end
private
def
validate_job_name!
(
name
)
if
name
.
blank?
||
!
validate_string
(
name
)
if
name
.
blank?
||
!
validate_string
(
name
)
raise
ValidationError
,
"job name should be non-empty string"
raise
ValidationError
,
"job name should be non-empty string"
end
end
end
def
validate_job_keys!
(
name
,
job
)
job
.
keys
.
each
do
|
key
|
job
.
keys
.
each
do
|
key
|
unless
ALLOWED_JOB_KEYS
.
include?
key
unless
ALLOWED_JOB_KEYS
.
include?
key
raise
ValidationError
,
"
#{
name
}
job: unknown parameter
#{
key
}
"
raise
ValidationError
,
"
#{
name
}
job: unknown parameter
#{
key
}
"
end
end
end
end
end
def
validate_job_types!
(
name
,
job
)
if
!
validate_string
(
job
[
:script
])
&&
!
validate_array_of_strings
(
job
[
:script
])
if
!
validate_string
(
job
[
:script
])
&&
!
validate_array_of_strings
(
job
[
:script
])
raise
ValidationError
,
"
#{
name
}
job: script should be a string or an array of a strings"
raise
ValidationError
,
"
#{
name
}
job: script should be a string or an array of a strings"
end
end
if
job
[
:stage
]
unless
job
[
:stage
].
is_a?
(
String
)
&&
job
[
:stage
].
in?
(
stages
)
raise
ValidationError
,
"
#{
name
}
job: stage parameter should be
#{
stages
.
join
(
", "
)
}
"
end
end
if
job
[
:image
]
&&
!
validate_string
(
job
[
:image
])
if
job
[
:image
]
&&
!
validate_string
(
job
[
:image
])
raise
ValidationError
,
"
#{
name
}
job: image should be a string"
raise
ValidationError
,
"
#{
name
}
job: image should be a string"
end
end
...
@@ -172,36 +182,40 @@ module Ci
...
@@ -172,36 +182,40 @@ module Ci
raise
ValidationError
,
"
#{
name
}
job: except parameter should be an array of strings"
raise
ValidationError
,
"
#{
name
}
job: except parameter should be an array of strings"
end
end
if
job
[
:cache
]
if
job
[
:allow_failure
]
&&
!
validate_boolean
(
job
[
:allow_failure
])
if
job
[
:cache
][
:untracked
]
&&
!
validate_boolean
(
job
[
:cache
][
:untracked
])
raise
ValidationError
,
"
#{
name
}
job: allow_failure parameter should be an boolean"
raise
ValidationError
,
"
#{
name
}
job: cache:untracked parameter should be an boolean"
end
if
job
[
:cache
][
:paths
]
&&
!
validate_array_of_strings
(
job
[
:cache
][
:paths
])
raise
ValidationError
,
"
#{
name
}
job: cache:paths parameter should be an array of strings"
end
end
end
if
job
[
:
artifacts
]
if
job
[
:
when
]
&&
!
job
[
:when
].
in?
(
%w(on_success on_failure always)
)
if
job
[
:artifacts
][
:untracked
]
&&
!
validate_boolean
(
job
[
:artifacts
][
:untracked
])
raise
ValidationError
,
"
#{
name
}
job: when parameter should be on_success, on_failure or always"
raise
ValidationError
,
"
#{
name
}
job: artifacts:untracked parameter should be an boolean"
end
end
end
if
job
[
:artifacts
][
:paths
]
&&
!
validate_array_of_strings
(
job
[
:artifacts
][
:paths
]
)
def
validate_job_stage!
(
name
,
job
)
raise
ValidationError
,
"
#{
name
}
job: artifacts:paths parameter should be an array of strings"
unless
job
[
:stage
].
is_a?
(
String
)
&&
job
[
:stage
].
in?
(
stages
)
end
raise
ValidationError
,
"
#{
name
}
job: stage parameter should be
#{
stages
.
join
(
", "
)
}
"
end
end
end
if
job
[
:allow_failure
]
&&
!
validate_boolean
(
job
[
:allow_failure
])
def
validate_job_cache!
(
name
,
job
)
raise
ValidationError
,
"
#{
name
}
job: allow_failure parameter should be an boolean"
if
job
[
:cache
][
:untracked
]
&&
!
validate_boolean
(
job
[
:cache
][
:untracked
])
raise
ValidationError
,
"
#{
name
}
job: cache:untracked parameter should be an boolean"
end
end
if
job
[
:
when
]
&&
!
job
[
:when
].
in?
(
%w(on_success on_failure always)
)
if
job
[
:
cache
][
:paths
]
&&
!
validate_array_of_strings
(
job
[
:cache
][
:paths
]
)
raise
ValidationError
,
"
#{
name
}
job:
when parameter should be on_success, on_failure or alway
s"
raise
ValidationError
,
"
#{
name
}
job:
cache:paths parameter should be an array of string
s"
end
end
end
end
private
def
validate_job_artifacts!
(
name
,
job
)
if
job
[
:artifacts
][
:untracked
]
&&
!
validate_boolean
(
job
[
:artifacts
][
:untracked
])
raise
ValidationError
,
"
#{
name
}
job: artifacts:untracked parameter should be an boolean"
end
if
job
[
:artifacts
][
:paths
]
&&
!
validate_array_of_strings
(
job
[
:artifacts
][
:paths
])
raise
ValidationError
,
"
#{
name
}
job: artifacts:paths parameter should be an array of strings"
end
end
def
validate_array_of_strings
(
values
)
def
validate_array_of_strings
(
values
)
values
.
is_a?
(
Array
)
&&
values
.
all?
{
|
value
|
validate_string
(
value
)
}
values
.
is_a?
(
Array
)
&&
values
.
all?
{
|
value
|
validate_string
(
value
)
}
...
...
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
View file @
652de0b8
...
@@ -532,21 +532,21 @@ module Ci
...
@@ -532,21 +532,21 @@ module Ci
end
end
it
"returns errors if job stage is not a string"
do
it
"returns errors if job stage is not a string"
do
config
=
YAML
.
dump
({
rspec:
{
script:
"test"
,
type:
1
,
allow_failure:
"string"
}
})
config
=
YAML
.
dump
({
rspec:
{
script:
"test"
,
type:
1
}
})
expect
do
expect
do
GitlabCiYamlProcessor
.
new
(
config
,
path
)
GitlabCiYamlProcessor
.
new
(
config
,
path
)
end
.
to
raise_error
(
GitlabCiYamlProcessor
::
ValidationError
,
"rspec job: stage parameter should be build, test, deploy"
)
end
.
to
raise_error
(
GitlabCiYamlProcessor
::
ValidationError
,
"rspec job: stage parameter should be build, test, deploy"
)
end
end
it
"returns errors if job stage is not a pre-defined stage"
do
it
"returns errors if job stage is not a pre-defined stage"
do
config
=
YAML
.
dump
({
rspec:
{
script:
"test"
,
type:
"acceptance"
,
allow_failure:
"string"
}
})
config
=
YAML
.
dump
({
rspec:
{
script:
"test"
,
type:
"acceptance"
}
})
expect
do
expect
do
GitlabCiYamlProcessor
.
new
(
config
,
path
)
GitlabCiYamlProcessor
.
new
(
config
,
path
)
end
.
to
raise_error
(
GitlabCiYamlProcessor
::
ValidationError
,
"rspec job: stage parameter should be build, test, deploy"
)
end
.
to
raise_error
(
GitlabCiYamlProcessor
::
ValidationError
,
"rspec job: stage parameter should be build, test, deploy"
)
end
end
it
"returns errors if job stage is not a defined stage"
do
it
"returns errors if job stage is not a defined stage"
do
config
=
YAML
.
dump
({
types:
[
"build"
,
"test"
],
rspec:
{
script:
"test"
,
type:
"acceptance"
,
allow_failure:
"string"
}
})
config
=
YAML
.
dump
({
types:
[
"build"
,
"test"
],
rspec:
{
script:
"test"
,
type:
"acceptance"
}
})
expect
do
expect
do
GitlabCiYamlProcessor
.
new
(
config
,
path
)
GitlabCiYamlProcessor
.
new
(
config
,
path
)
end
.
to
raise_error
(
GitlabCiYamlProcessor
::
ValidationError
,
"rspec job: stage parameter should be build, test"
)
end
.
to
raise_error
(
GitlabCiYamlProcessor
::
ValidationError
,
"rspec job: stage parameter should be build, test"
)
...
...
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