[Rails5] Fix Ci::Pipeline validator for source

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.
This commit is contained in:
blackst0ne 2018-05-12 00:07:23 +11:00
parent 8c2b73dc8c
commit 7f6691dde7
1 changed files with 11 additions and 1 deletions

View File

@ -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