summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorblackst0ne <blackst0ne.ru@gmail.com>2018-05-12 00:07:23 +1100
committerblackst0ne <blackst0ne.ru@gmail.com>2018-05-12 00:07:23 +1100
commit7f6691dde7be7c024d3ed9f9fee8b56813a45e51 (patch)
treea35042bdc6ac5ed6c2dd7e52d27b43f27ab4c8da
parent8c2b73dc8c5adf41b94033fe1d0a265524e304c4 (diff)
downloadgitlab-ce-blackst0ne-rails5-fix-spec-models-ci-pipeline_spec-rb.tar.gz
[Rails5] Fix Ci::Pipeline validator for sourceblackst0ne-rails5-fix-spec-models-ci-pipeline_spec-rb
In Rails 5 enum returns value instead of key. For this case, the `NilClass` is returned instead of `unknown` which breaks validation of the `source` attribute. This commit adds a custom validatior that returns the correct result for both rails4 and rails5.
-rw-r--r--app/models/ci/pipeline.rb12
1 files changed, 11 insertions, 1 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 0b90834d415..1f49764e7cc 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -37,12 +37,16 @@ module Ci
delegate :id, to: :project, prefix: true
delegate :full_path, to: :project, prefix: true
- validates :source, exclusion: { in: %w(unknown), unless: :importing? }, on: :create
validates :sha, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? }
validates :status, presence: { unless: :importing? }
validate :valid_commit_sha, unless: :importing?
+ # Replace validator below with
+ # `validates :source, presence: { unless: :importing? }, on: :create`
+ # when removing Gitlab.rails5? code.
+ validate :valid_source, unless: :importing?, on: :create
+
after_create :keep_around_commits, unless: :importing?
enum source: {
@@ -601,5 +605,11 @@ module Ci
project.repository.keep_around(self.sha)
project.repository.keep_around(self.before_sha)
end
+
+ def valid_source
+ if source.nil? || source == "unknown"
+ errors.add(:source, "invalid source")
+ end
+ end
end
end