summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2018-10-04 18:59:34 +0900
committerShinya Maeda <shinya@gitlab.com>2018-10-04 18:59:34 +0900
commite84230ebb6db5ff9e5990e945aa1e1aebf4e9fa9 (patch)
tree6fcbcf0d02a0b34177ee9af4b29972526a3308a8
parent28f895e495a7868e44602781c99bcd51fac08ccc (diff)
downloadgitlab-ce-e84230ebb6db5ff9e5990e945aa1e1aebf4e9fa9.tar.gz
Add limitation for start_in keyword
-rw-r--r--lib/gitlab/ci/config/entry/job.rb2
-rw-r--r--lib/gitlab/ci/config/entry/legacy_validation_helpers.rb9
-rw-r--r--lib/gitlab/ci/config/entry/validators.rb6
-rw-r--r--spec/lib/gitlab/ci/config/entry/job_spec.rb15
4 files changed, 27 insertions, 5 deletions
diff --git a/lib/gitlab/ci/config/entry/job.rb b/lib/gitlab/ci/config/entry/job.rb
index 03971254310..f290ff3a565 100644
--- a/lib/gitlab/ci/config/entry/job.rb
+++ b/lib/gitlab/ci/config/entry/job.rb
@@ -36,7 +36,7 @@ module Gitlab
validates :extends, type: String
end
- validates :start_in, duration: true, if: :delayed?
+ validates :start_in, duration: { limit: '1 day' }, if: :delayed?
validates :start_in, absence: true, unless: :delayed?
end
diff --git a/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb b/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb
index a78a85397bd..a3d4432be82 100644
--- a/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb
+++ b/lib/gitlab/ci/config/entry/legacy_validation_helpers.rb
@@ -11,6 +11,15 @@ module Gitlab
false
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)
values.is_a?(Array) && values.all? { |value| validate_string(value) }
end
diff --git a/lib/gitlab/ci/config/entry/validators.rb b/lib/gitlab/ci/config/entry/validators.rb
index b3c889ee92f..f6b4ba7843e 100644
--- a/lib/gitlab/ci/config/entry/validators.rb
+++ b/lib/gitlab/ci/config/entry/validators.rb
@@ -49,6 +49,12 @@ module Gitlab
unless validate_duration(value)
record.errors.add(attribute, 'should be a duration')
end
+
+ if options[:limit]
+ unless validate_duration_limit(value, options[:limit])
+ record.errors.add(attribute, 'should not exceed the limit')
+ end
+ end
end
end
diff --git a/spec/lib/gitlab/ci/config/entry/job_spec.rb b/spec/lib/gitlab/ci/config/entry/job_spec.rb
index d745c4ca2ad..1169938b80c 100644
--- a/spec/lib/gitlab/ci/config/entry/job_spec.rb
+++ b/spec/lib/gitlab/ci/config/entry/job_spec.rb
@@ -44,9 +44,7 @@ describe Gitlab::Ci::Config::Entry::Job do
context 'when start_in is specified' do
let(:config) { { script: 'echo', when: 'delayed', start_in: '1 day' } }
- it 'returns error about invalid type' do
- expect(entry).to be_valid
- end
+ it { expect(entry).to be_valid }
end
end
end
@@ -158,7 +156,7 @@ describe Gitlab::Ci::Config::Entry::Job do
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' } }
it 'returns error about invalid type' do
@@ -166,6 +164,15 @@ describe Gitlab::Ci::Config::Entry::Job do
expect(entry.errors).to include 'job start in should be a duration'
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
context 'when start_in specified without delayed specification' do