Commit 4949e2b2 authored by Shinya Maeda's avatar Shinya Maeda

Separate cron_valid? and cron_time_zone_valid?

parent 3d3df097
...@@ -12,7 +12,7 @@ module Ci ...@@ -12,7 +12,7 @@ module Ci
validates :trigger, presence: { unless: :importing? } validates :trigger, presence: { unless: :importing? }
validates :cron, cron: true, presence: { unless: :importing? } validates :cron, cron: true, presence: { unless: :importing? }
validates :cron_time_zone, presence: { unless: :importing? } validates :cron_time_zone, cron_time_zone: true, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? } validates :ref, presence: { unless: :importing? }
after_create :schedule_next_run! after_create :schedule_next_run!
......
# CronTimeZoneValidator
#
# Custom validator for CronTimeZone.
class CronTimeZoneValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
cron_parser = Gitlab::Ci::CronParser.new(record.cron, record.cron_time_zone)
record.errors.add(attribute, " is invalid syntax") unless cron_parser.cron_time_zone_valid?
end
end
...@@ -4,12 +4,6 @@ ...@@ -4,12 +4,6 @@
class CronValidator < ActiveModel::EachValidator class CronValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value) def validate_each(record, attribute, value)
cron_parser = Gitlab::Ci::CronParser.new(record.cron, record.cron_time_zone) cron_parser = Gitlab::Ci::CronParser.new(record.cron, record.cron_time_zone)
is_valid_cron, is_valid_cron_time_zone = cron_parser.validation record.errors.add(attribute, " is invalid syntax") unless cron_parser.cron_valid?
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
end end
...@@ -14,10 +14,12 @@ module Gitlab ...@@ -14,10 +14,12 @@ module Gitlab
cron_line.next_time(time).in_time_zone(Time.zone) if cron_line.present? cron_line.next_time(time).in_time_zone(Time.zone) if cron_line.present?
end end
def validation def cron_valid?
is_valid_cron = try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present? try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
is_valid_cron_time_zone = try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_time_zone).present? end
return is_valid_cron, is_valid_cron_time_zone
def cron_time_zone_valid?
try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_time_zone).present?
end end
private private
......
...@@ -82,23 +82,35 @@ describe Gitlab::Ci::CronParser do ...@@ -82,23 +82,35 @@ describe Gitlab::Ci::CronParser do
end end
end end
describe '#validation' do describe '#cron_valid?' do
it 'returns results' do subject { described_class.new(cron, Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE).cron_valid? }
is_valid_cron, is_valid_cron_time_zone = described_class.new('* * * * *', 'Europe/Istanbul').validation
expect(is_valid_cron).to eq(true) context 'when cron is valid' do
expect(is_valid_cron_time_zone).to eq(true) let(:cron) { '* * * * *' }
it { is_expected.to eq(true) }
end end
it 'returns results' do context 'when cron is invalid' do
is_valid_cron, is_valid_cron_time_zone = described_class.new('*********', 'Europe/Istanbul').validation let(:cron) { '*********' }
expect(is_valid_cron).to eq(false)
expect(is_valid_cron_time_zone).to eq(true) it { is_expected.to eq(false) }
end end
end
describe '#cron_time_zone_valid?' do
subject { described_class.new(Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_CRON, cron_time_zone).cron_time_zone_valid? }
context 'when cron is valid' do
let(:cron_time_zone) { 'Europe/Istanbul' }
it { is_expected.to eq(true) }
end
context 'when cron is invalid' do
let(:cron_time_zone) { 'Invalid-zone' }
it 'returns results' do it { is_expected.to eq(false) }
is_valid_cron, is_valid_cron_time_zone = described_class.new('* * * * *', 'Invalid-zone').validation
expect(is_valid_cron).to eq(true)
expect(is_valid_cron_time_zone).to eq(false)
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