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
e8f09f02
Commit
e8f09f02
authored
Jun 14, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate environment name with regex
parent
3656a6ed
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
10 deletions
+78
-10
app/models/environment.rb
app/models/environment.rb
+5
-1
lib/ci/gitlab_ci_yaml_processor.rb
lib/ci/gitlab_ci_yaml_processor.rb
+6
-2
lib/gitlab/regex.rb
lib/gitlab/regex.rb
+8
-0
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
+59
-7
No files found.
app/models/environment.rb
View file @
e8f09f02
...
@@ -3,7 +3,11 @@ class Environment < ActiveRecord::Base
...
@@ -3,7 +3,11 @@ class Environment < ActiveRecord::Base
has_many
:deployments
has_many
:deployments
validates_presence_of
:name
validates
:name
,
presence:
true
,
length:
{
within:
0
..
255
},
format:
{
with:
Gitlab
::
Regex
.
environment_name_regex
,
message:
Gitlab
::
Regex
.
environment_name_regex_message
}
def
last_deployment
def
last_deployment
deployments
.
last
deployments
.
last
...
...
lib/ci/gitlab_ci_yaml_processor.rb
View file @
e8f09f02
...
@@ -214,8 +214,8 @@ module Ci
...
@@ -214,8 +214,8 @@ module Ci
raise
ValidationError
,
"
#{
name
}
job: when parameter should be on_success, on_failure or always"
raise
ValidationError
,
"
#{
name
}
job: when parameter should be on_success, on_failure or always"
end
end
if
job
[
:environment
]
&&
!
validate_
string
(
job
[
:environment
])
if
job
[
:environment
]
&&
!
validate_
environment
(
job
[
:environment
])
raise
ValidationError
,
"
#{
name
}
job: environment
should be a string
"
raise
ValidationError
,
"
#{
name
}
job: environment
parameter
#{
Gitlab
::
Regex
.
environment_name_regex_message
}
"
end
end
end
end
...
@@ -322,6 +322,10 @@ module Ci
...
@@ -322,6 +322,10 @@ module Ci
value
.
in?
([
true
,
false
])
value
.
in?
([
true
,
false
])
end
end
def
validate_environment
(
value
)
value
.
is_a?
(
String
)
&&
value
=~
Gitlab
::
Regex
.
environment_name_regex
end
def
process?
(
only_params
,
except_params
,
ref
,
tag
,
trigger_request
)
def
process?
(
only_params
,
except_params
,
ref
,
tag
,
trigger_request
)
if
only_params
.
present?
if
only_params
.
present?
return
false
unless
matching?
(
only_params
,
ref
,
tag
,
trigger_request
)
return
false
unless
matching?
(
only_params
,
ref
,
tag
,
trigger_request
)
...
...
lib/gitlab/regex.rb
View file @
e8f09f02
...
@@ -100,5 +100,13 @@ module Gitlab
...
@@ -100,5 +100,13 @@ module Gitlab
def
container_registry_reference_regex
def
container_registry_reference_regex
git_reference_regex
git_reference_regex
end
end
def
environment_name_regex
@environment_name_regex
||=
/\A[a-zA-Z0-9_-]+\z/
.
freeze
end
def
environment_name_regex_message
"can contain only letters, digits, '-' and '_'."
end
end
end
end
end
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
View file @
e8f09f02
...
@@ -26,7 +26,8 @@ module Ci
...
@@ -26,7 +26,8 @@ module Ci
tag_list:
[],
tag_list:
[],
options:
{},
options:
{},
allow_failure:
false
,
allow_failure:
false
,
when:
"on_success"
when:
"on_success"
,
environment:
nil
,
})
})
end
end
...
@@ -387,7 +388,8 @@ module Ci
...
@@ -387,7 +388,8 @@ module Ci
services:
[
"mysql"
]
services:
[
"mysql"
]
},
},
allow_failure:
false
,
allow_failure:
false
,
when:
"on_success"
when:
"on_success"
,
environment:
nil
,
})
})
end
end
...
@@ -415,7 +417,8 @@ module Ci
...
@@ -415,7 +417,8 @@ module Ci
services:
[
"postgresql"
]
services:
[
"postgresql"
]
},
},
allow_failure:
false
,
allow_failure:
false
,
when:
"on_success"
when:
"on_success"
,
environment:
nil
,
})
})
end
end
end
end
...
@@ -599,7 +602,8 @@ module Ci
...
@@ -599,7 +602,8 @@ module Ci
}
}
},
},
when:
"on_success"
,
when:
"on_success"
,
allow_failure:
false
allow_failure:
false
,
environment:
nil
,
})
})
end
end
...
@@ -621,6 +625,51 @@ module Ci
...
@@ -621,6 +625,51 @@ module Ci
end
end
end
end
describe
'#environment'
do
let
(
:config
)
do
{
deploy_to_production:
{
stage:
'deploy'
,
script:
'test'
,
environment:
environment
}
}
end
let
(
:processor
)
{
GitlabCiYamlProcessor
.
new
(
YAML
.
dump
(
config
))
}
let
(
:builds
)
{
processor
.
builds_for_stage_and_ref
(
'deploy'
,
'master'
)
}
context
'when a production environment is specified'
do
let
(
:environment
)
{
'production'
}
it
'does return production'
do
expect
(
builds
.
size
).
to
eq
(
1
)
expect
(
builds
.
first
[
:environment
]).
to
eq
(
environment
)
end
end
context
'when no environment is specified'
do
let
(
:environment
)
{
nil
}
it
'does return nil environment'
do
expect
(
builds
.
size
).
to
eq
(
1
)
expect
(
builds
.
first
[
:environment
]).
to
be_nil
end
end
context
'is not a string'
do
let
(
:environment
)
{
1
}
it
'raises error'
do
expect
{
builds
}.
to
raise_error
(
"deploy_to_production job: environment parameter
#{
Gitlab
::
Regex
.
environment_name_regex_message
}
"
)
end
end
context
'is not a valid string'
do
let
(
:environment
)
{
'production staging'
}
it
'raises error'
do
expect
{
builds
}.
to
raise_error
(
"deploy_to_production job: environment parameter
#{
Gitlab
::
Regex
.
environment_name_regex_message
}
"
)
end
end
end
describe
"Dependencies"
do
describe
"Dependencies"
do
let
(
:config
)
do
let
(
:config
)
do
{
{
...
@@ -682,7 +731,8 @@ module Ci
...
@@ -682,7 +731,8 @@ module Ci
tag_list:
[],
tag_list:
[],
options:
{},
options:
{},
when:
"on_success"
,
when:
"on_success"
,
allow_failure:
false
allow_failure:
false
,
environment:
nil
,
})
})
end
end
end
end
...
@@ -727,7 +777,8 @@ module Ci
...
@@ -727,7 +777,8 @@ module Ci
tag_list:
[],
tag_list:
[],
options:
{},
options:
{},
when:
"on_success"
,
when:
"on_success"
,
allow_failure:
false
allow_failure:
false
,
environment:
nil
,
})
})
expect
(
subject
.
second
).
to
eq
({
expect
(
subject
.
second
).
to
eq
({
except:
nil
,
except:
nil
,
...
@@ -739,7 +790,8 @@ module Ci
...
@@ -739,7 +790,8 @@ module Ci
tag_list:
[],
tag_list:
[],
options:
{},
options:
{},
when:
"on_success"
,
when:
"on_success"
,
allow_failure:
false
allow_failure:
false
,
environment:
nil
,
})
})
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