gitlab-org--gitlab-foss/app/models/ci/trigger_schedule.rb

57 lines
1.4 KiB
Ruby
Raw Normal View History

module Ci
class TriggerSchedule < ActiveRecord::Base
extend Ci::Model
acts_as_paranoid
belongs_to :project
belongs_to :trigger
2017-03-30 11:59:20 +00:00
delegate :ref, 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!
2017-03-31 10:08:39 +00:00
next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from(Time.now)
if next_time.present? && !less_than_1_hour_from_now?(next_time)
update!(next_run_at: next_time)
2017-03-23 15:18:13 +00:00
end
end
private
2017-03-31 10:08:39 +00:00
def less_than_1_hour_from_now?(time)
((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
2017-03-31 10:08:39 +00:00
next_time = cron_parser.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")
2017-03-31 10:08:39 +00:00
elsif less_than_1_hour_from_now?(next_time)
self.errors.add(:cron, " can not be less than 1 hour")
end
end
def check_ref
2017-03-30 18:16:24 +00:00
if !ref.present?
self.errors.add(:ref, " is empty")
2017-03-31 14:18:07 +00:00
elsif !project.repository.branch_exists?(ref)
self.errors.add(:ref, " does not exist")
end
end
end
end