Commit e84230eb authored by Shinya Maeda's avatar Shinya Maeda

Add limitation for start_in keyword

parent 28f895e4
...@@ -36,7 +36,7 @@ module Gitlab ...@@ -36,7 +36,7 @@ module Gitlab
validates :extends, type: String validates :extends, type: String
end end
validates :start_in, duration: true, if: :delayed? validates :start_in, duration: { limit: '1 day' }, if: :delayed?
validates :start_in, absence: true, unless: :delayed? validates :start_in, absence: true, unless: :delayed?
end end
......
...@@ -11,6 +11,15 @@ module Gitlab ...@@ -11,6 +11,15 @@ module Gitlab
false false
end end
def validate_duration_limit(value, limit)
return false unless value.is_a?(String)
ChronicDuration.parse(value).second.from_now <
ChronicDuration.parse(limit).second.from_now
rescue ChronicDuration::DurationParseError
false
end
def validate_array_of_strings(values) def validate_array_of_strings(values)
values.is_a?(Array) && values.all? { |value| validate_string(value) } values.is_a?(Array) && values.all? { |value| validate_string(value) }
end end
......
...@@ -49,6 +49,12 @@ module Gitlab ...@@ -49,6 +49,12 @@ module Gitlab
unless validate_duration(value) unless validate_duration(value)
record.errors.add(attribute, 'should be a duration') record.errors.add(attribute, 'should be a duration')
end end
if options[:limit]
unless validate_duration_limit(value, options[:limit])
record.errors.add(attribute, 'should not exceed the limit')
end
end
end end
end end
......
...@@ -44,9 +44,7 @@ describe Gitlab::Ci::Config::Entry::Job do ...@@ -44,9 +44,7 @@ describe Gitlab::Ci::Config::Entry::Job do
context 'when start_in is specified' do context 'when start_in is specified' do
let(:config) { { script: 'echo', when: 'delayed', start_in: '1 day' } } let(:config) { { script: 'echo', when: 'delayed', start_in: '1 day' } }
it 'returns error about invalid type' do it { expect(entry).to be_valid }
expect(entry).to be_valid
end
end end
end end
end end
...@@ -158,7 +156,7 @@ describe Gitlab::Ci::Config::Entry::Job do ...@@ -158,7 +156,7 @@ describe Gitlab::Ci::Config::Entry::Job do
end end
end end
context 'when start_in is not formateed ad a duration' do context 'when start_in is not formatted as a duration' do
let(:config) { { when: 'delayed', start_in: 'test' } } let(:config) { { when: 'delayed', start_in: 'test' } }
it 'returns error about invalid type' do it 'returns error about invalid type' do
...@@ -166,6 +164,15 @@ describe Gitlab::Ci::Config::Entry::Job do ...@@ -166,6 +164,15 @@ describe Gitlab::Ci::Config::Entry::Job do
expect(entry.errors).to include 'job start in should be a duration' expect(entry.errors).to include 'job start in should be a duration'
end end
end end
context 'when start_in is longer than one day' do
let(:config) { { when: 'delayed', start_in: '2 days' } }
it 'returns error about exceeding the limit' do
expect(entry).not_to be_valid
expect(entry.errors).to include 'job start in should not exceed the limit'
end
end
end end
context 'when start_in specified without delayed specification' do context 'when start_in specified without delayed specification' do
......
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