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
Tatuya Kamada
gitlab-ce
Commits
2a1a7310
Commit
2a1a7310
authored
Mar 30, 2017
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add validation to Ci::TriggerSchedule (Halfway)
parent
75f5e710
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
29 deletions
+72
-29
app/models/ci/trigger_schedule.rb
app/models/ci/trigger_schedule.rb
+31
-4
lib/ci/cron_parser.rb
lib/ci/cron_parser.rb
+7
-5
spec/factories/ci/trigger_schedules.rb
spec/factories/ci/trigger_schedules.rb
+9
-7
spec/factories/ci/triggers.rb
spec/factories/ci/triggers.rb
+1
-1
spec/models/ci/trigger_schedule_spec.rb
spec/models/ci/trigger_schedule_spec.rb
+24
-12
No files found.
app/models/ci/trigger_schedule.rb
View file @
2a1a7310
...
@@ -7,15 +7,42 @@ module Ci
...
@@ -7,15 +7,42 @@ module Ci
belongs_to
:project
belongs_to
:project
belongs_to
:trigger
belongs_to
:trigger
validates
:trigger
,
presence:
true
validates
:cron
,
presence:
true
validates
:cron_time_zone
,
presence:
true
validate
:check_cron
validate
:check_ref
after_create
:schedule_next_run!
def
schedule_next_run!
def
schedule_next_run!
next_time
=
Ci
::
CronParser
.
new
(
cron
,
cron_time_zone
).
next_time_from_now
next_time
=
Ci
::
CronParser
.
new
(
cron
,
cron_time_zone
).
next_time_from_now
if
next_time
.
present?
if
next_time
.
present?
update
_attributes
(
next_run_at:
next_time
)
update
!
(
next_run_at:
next_time
)
end
end
end
end
# def update_last_run!
private
# update_attributes(last_run_at: Time.now)
# end
def
check_cron
cron_parser
=
Ci
::
CronParser
.
new
(
cron
,
cron_time_zone
)
is_valid_cron
,
is_valid_cron_time_zone
=
cron_parser
.
validation
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
(
cron_parser
.
next_time_from_now
-
Time
.
now
).
abs
<
1
.
hour
self
.
errors
.
add
(
:cron
,
" can not be less than 1 hour"
)
end
end
def
check_ref
if
!
trigger
.
ref
.
present?
self
.
errors
.
add
(
:ref
,
" is empty"
)
elsif
trigger
.
project
.
repository
.
ref_exists?
(
trigger
.
ref
)
self
.
errors
.
add
(
:ref
,
" does not exist"
)
end
end
end
end
end
end
lib/ci/cron_parser.rb
View file @
2a1a7310
...
@@ -6,20 +6,22 @@ module Ci
...
@@ -6,20 +6,22 @@ module Ci
end
end
def
next_time_from_now
def
next_time_from_now
cronLine
=
try_parse_cron
cronLine
=
try_parse_cron
(
@cron
,
@cron_time_zone
)
return
nil
unless
cronLine
.
present?
return
nil
unless
cronLine
.
present?
cronLine
.
next_time
cronLine
.
next_time
end
end
def
valid_syntax?
def
validation
try_parse_cron
.
present?
?
true
:
false
is_valid_cron
=
try_parse_cron
(
@cron
,
'Europe/Istanbul'
).
present?
is_valid_cron_time_zone
=
try_parse_cron
(
'* * * * *'
,
@cron_time_zone
).
present?
return
is_valid_cron
,
is_valid_cron_time_zone
end
end
private
private
def
try_parse_cron
def
try_parse_cron
(
cron
,
cron_time_zone
)
begin
begin
Rufus
::
Scheduler
.
parse
(
"
#{
@cron
}
#{
@
cron_time_zone
}
"
)
Rufus
::
Scheduler
.
parse
(
"
#{
cron
}
#{
cron_time_zone
}
"
)
rescue
rescue
nil
nil
end
end
...
...
spec/factories/ci/trigger_schedules.rb
View file @
2a1a7310
...
@@ -4,18 +4,20 @@ FactoryGirl.define do
...
@@ -4,18 +4,20 @@ FactoryGirl.define do
trigger
factory: :ci_trigger
trigger
factory: :ci_trigger
trait
:force_triggable
do
trait
:force_triggable
do
next_run_at
Time
.
now
-
1
.
month
after
(
:create
)
do
|
trigger_schedule
,
evaluator
|
trigger_schedule
.
next_run_at
-=
1
.
month
end
end
end
trait
:cron_nightly_build
do
trait
:cron_nightly_build
do
cron
'0 1 * * *'
cron
'0 1 * * *'
cron_time_zone
'Europe/Istanbul'
cron_time_zone
'Europe/Istanbul'
next_run_at
do
# TODO: Use CronParser
#
next_run_at do # TODO: Use CronParser
time
=
Time
.
now
.
in_time_zone
(
cron_time_zone
)
#
time = Time.now.in_time_zone(cron_time_zone)
time
=
time
+
1
.
day
if
time
.
hour
>
1
#
time = time + 1.day if time.hour > 1
time
=
time
.
change
(
sec:
0
,
min:
0
,
hour:
1
)
#
time = time.change(sec: 0, min: 0, hour: 1)
time
#
time
end
#
end
end
end
trait
:cron_weekly_build
do
trait
:cron_weekly_build
do
...
...
spec/factories/ci/triggers.rb
View file @
2a1a7310
FactoryGirl
.
define
do
FactoryGirl
.
define
do
factory
:ci_trigger_without_token
,
class:
Ci
::
Trigger
do
factory
:ci_trigger_without_token
,
class:
Ci
::
Trigger
do
factory
:ci_trigger
do
factory
:ci_trigger
do
token
{
SecureRandom
.
hex
(
1
0
)
}
token
{
SecureRandom
.
hex
(
1
5
)
}
end
end
end
end
end
end
spec/models/ci/trigger_schedule_spec.rb
View file @
2a1a7310
require
'spec_helper'
require
'spec_helper'
describe
Ci
::
TriggerSchedule
,
models:
true
do
describe
Ci
::
TriggerSchedule
,
models:
true
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:trigger
)
{
create
(
:ci_trigger
,
owner:
user
,
project:
project
,
ref:
'master'
)
}
describe
'associations'
do
describe
'associations'
do
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
belong_to
(
:trigger
)
}
it
{
is_expected
.
to
belong_to
(
:trigger
)
}
end
end
describe
'
#schedule_next_run!
'
do
describe
'
validation
'
do
subject
{
trigger_schedule
.
schedule_next_run!
}
let
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
,
trigger:
trigger
)
}
let
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
,
next_run_at:
nil
)
}
it
{
expect
(
trigger_schedule
).
to
validate_presence_of
(
:trigger
)
}
it
{
is_expected
.
to
validate_presence_of
(
:cron
)
}
it
{
is_expected
.
to
validate_presence_of
(
:cron_time_zone
)
}
it
'updates next_run_at'
do
it
'#check_cron'
do
is_expected
.
not_to
be_nil
subject
.
cron
=
'Hack'
subject
.
valid?
subject
.
errors
[
:screen_name
].
to
include
(
' is invalid syntax'
)
end
it
'#check_ref'
do
end
end
end
end
# describe '#update_las
t_run!' do
describe
'#schedule_nex
t_run!'
do
# subject { scheduled_trigger.update_last_run!
}
let
(
:trigger_schedule
)
{
create
(
:ci_trigger_schedule
,
:cron_nightly_build
,
next_run_at:
nil
,
trigger:
trigger
)
}
# let(:scheduled_trigger) { create(:ci_scheduled_trigger, :cron_nightly_build, last_run_at: nil) }
before
do
trigger_schedule
.
schedule_next_run!
end
# it 'updates las
t_run_at' do
it
'updates nex
t_run_at'
do
# is_expected
.not_to be_nil
expect
(
Ci
::
TriggerSchedule
.
last
.
next_run_at
)
.
not_to
be_nil
#
end
end
#
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