Commit 2a1a7310 authored by Shinya Maeda's avatar Shinya Maeda

Add validation to Ci::TriggerSchedule (Halfway)

parent 75f5e710
...@@ -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
...@@ -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
......
...@@ -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
......
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(10) } token { SecureRandom.hex(15) }
end end
end end
end end
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_last_run!' do describe '#schedule_next_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 last_run_at' do it 'updates next_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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment