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
Jérome Perrin
gitlab-ce
Commits
57d082f3
Commit
57d082f3
authored
Apr 01, 2017
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add validator
parent
21cabf38
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
41 deletions
+34
-41
app/models/ci/trigger_schedule.rb
app/models/ci/trigger_schedule.rb
+6
-20
app/validators/cron_validator.rb
app/validators/cron_validator.rb
+16
-0
app/validators/ref_validator.rb
app/validators/ref_validator.rb
+10
-0
spec/models/ci/trigger_schedule_spec.rb
spec/models/ci/trigger_schedule_spec.rb
+2
-4
spec/workers/trigger_schedule_worker_spec.rb
spec/workers/trigger_schedule_worker_spec.rb
+0
-17
No files found.
app/models/ci/trigger_schedule.rb
View file @
57d082f3
...
...
@@ -10,10 +10,10 @@ module Ci
delegate
:ref
,
to: :trigger
validates
:trigger
,
presence:
true
validates
:cron
,
presence:
true
validates
:cron
,
cron:
true
,
presence:
true
validates
:cron_time_zone
,
presence:
true
validate
:check_cron
validate
:check_
ref
validate
s
:ref
,
ref:
true
,
presence:
true
validate
:check_
cron_frequency
after_create
:schedule_next_run!
...
...
@@ -31,26 +31,12 @@ module Ci
((
time
-
Time
.
now
).
abs
<
1
.
hour
)
?
true
:
false
end
def
check_cron
cron_parser
=
Ci
::
CronParser
.
new
(
cron
,
cron_time_zone
)
is_valid_cron
,
is_valid_cron_time_zone
=
cron_parser
.
validation
next_time
=
cron_parser
.
next_time_from
(
Time
.
now
)
def
check_cron_frequency
next_time
=
Ci
::
CronParser
.
new
(
cron
,
cron_time_zone
).
next_time_from
(
Time
.
now
)
if
!
is_valid_cron
self
.
errors
.
add
(
:cron
,
" is invalid syntax"
)
elsif
!
is_valid_cron_time_zone
self
.
errors
.
add
(
:cron_time_zone
,
" is invalid timezone"
)
elsif
less_than_1_hour_from_now?
(
next_time
)
if
less_than_1_hour_from_now?
(
next_time
)
self
.
errors
.
add
(
:cron
,
" can not be less than 1 hour"
)
end
end
def
check_ref
if
!
ref
.
present?
self
.
errors
.
add
(
:ref
,
" is empty"
)
elsif
!
project
.
repository
.
branch_exists?
(
ref
)
self
.
errors
.
add
(
:ref
,
" does not exist"
)
end
end
end
end
app/validators/cron_validator.rb
0 → 100644
View file @
57d082f3
# CronValidator
#
# Custom validator for Cron.
class
CronValidator
<
ActiveModel
::
EachValidator
def
validate_each
(
record
,
attribute
,
value
)
cron_parser
=
Ci
::
CronParser
.
new
(
record
.
cron
,
record
.
cron_time_zone
)
is_valid_cron
,
is_valid_cron_time_zone
=
cron_parser
.
validation
next_time
=
cron_parser
.
next_time_from
(
Time
.
now
)
if
!
is_valid_cron
record
.
errors
.
add
(
:cron
,
" is invalid syntax"
)
elsif
!
is_valid_cron_time_zone
record
.
errors
.
add
(
:cron_time_zone
,
" is invalid timezone"
)
end
end
end
app/validators/ref_validator.rb
0 → 100644
View file @
57d082f3
# RefValidator
#
# Custom validator for Ref.
class
RefValidator
<
ActiveModel
::
EachValidator
def
validate_each
(
record
,
attribute
,
value
)
unless
record
.
project
.
repository
.
branch_exists?
(
value
)
record
.
errors
.
add
(
attribute
,
" does not exist"
)
end
end
end
spec/models/ci/trigger_schedule_spec.rb
View file @
57d082f3
...
...
@@ -4,8 +4,6 @@ describe Ci::TriggerSchedule, models: true do
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
belong_to
(
:trigger
)
}
# it { is_expected.to validate_presence_of :cron }
# it { is_expected.to validate_presence_of :cron_time_zone }
it
{
is_expected
.
to
respond_to
:ref
}
it
'should validate ref existence'
do
...
...
@@ -26,7 +24,7 @@ describe Ci::TriggerSchedule, models: true do
context
'when every hour'
do
let
(
:cron
)
{
'0 * * * *'
}
# 00:00, 01:00, 02:00, ..., 23:00
it
'
fails
'
do
it
'
gets an error
'
do
expect
(
trigger_schedule
.
errors
[
:cron
].
first
).
to
include
(
'can not be less than 1 hour'
)
end
end
...
...
@@ -34,7 +32,7 @@ describe Ci::TriggerSchedule, models: true do
context
'when each six hours'
do
let
(
:cron
)
{
'0 */6 * * *'
}
# 00:00, 06:00, 12:00, 18:00
it
'
succeed
s'
do
it
'
gets no error
s'
do
expect
(
trigger_schedule
.
errors
[
:cron
]).
to
be_empty
end
end
...
...
spec/workers/trigger_schedule_worker_spec.rb
View file @
57d082f3
...
...
@@ -28,23 +28,6 @@ describe TriggerScheduleWorker do
end
end
context
'when there is a scheduled trigger within next_run_at and a runnign pipeline'
do
let!
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
,
:force_triggable
)
}
before
do
create
(
:ci_pipeline
,
project:
trigger_schedule
.
project
,
ref:
trigger_schedule
.
ref
,
status:
'running'
)
worker
.
perform
end
it
'do not create a new pipeline'
do
expect
(
Ci
::
Pipeline
.
count
).
to
eq
(
1
)
end
it
'do not reschedule next_run_at'
do
expect
(
Ci
::
TriggerSchedule
.
last
.
next_run_at
).
to
eq
(
trigger_schedule
.
next_run_at
)
end
end
context
'when there are no scheduled triggers within next_run_at'
do
let!
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
)
}
...
...
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