Add validation for protected attributes

This commit is contained in:
Shinya Maeda 2017-08-31 21:13:40 +09:00
parent f3d3cecf5a
commit ce7c0ac3db
6 changed files with 20 additions and 4 deletions

View File

@ -26,6 +26,7 @@ module Ci
validates :coverage, numericality: true, allow_blank: true validates :coverage, numericality: true, allow_blank: true
validates :ref, presence: true validates :ref, presence: true
validates :protected, inclusion: { in: [ true, false ] }, on: :create
scope :unstarted, ->() { where(runner_id: nil) } scope :unstarted, ->() { where(runner_id: nil) }
scope :ignore_failures, ->() { where(allow_failure: false) } scope :ignore_failures, ->() { where(allow_failure: false) }

View File

@ -36,6 +36,7 @@ module Ci
validates :sha, presence: { unless: :importing? } validates :sha, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? } validates :ref, presence: { unless: :importing? }
validates :status, presence: { unless: :importing? } validates :status, presence: { unless: :importing? }
validates :protected, inclusion: { in: [ true, false ], unless: :importing? }, on: :create
validate :valid_commit_sha, unless: :importing? validate :valid_commit_sha, unless: :importing?
after_create :keep_around_commits, unless: :importing? after_create :keep_around_commits, unless: :importing?

View File

@ -3,7 +3,7 @@ module Ci
CLONE_ACCESSORS = %i[pipeline project ref tag options commands name CLONE_ACCESSORS = %i[pipeline project ref tag options commands name
allow_failure stage_id stage stage_idx trigger_request allow_failure stage_id stage stage_idx trigger_request
yaml_variables when environment coverage_regex yaml_variables when environment coverage_regex
description tag_list].freeze description tag_list protected].freeze
def execute(build) def execute(build)
reprocess!(build).tap do |new_build| reprocess!(build).tap do |new_build|

View File

@ -12,6 +12,7 @@ FactoryGirl.define do
started_at 'Di 29. Okt 09:51:28 CET 2013' started_at 'Di 29. Okt 09:51:28 CET 2013'
finished_at 'Di 29. Okt 09:53:28 CET 2013' finished_at 'Di 29. Okt 09:53:28 CET 2013'
commands 'ls -a' commands 'ls -a'
protected false
options do options do
{ {
@ -227,11 +228,11 @@ FactoryGirl.define do
self.when 'manual' self.when 'manual'
end end
trait(:protected) do trait :protected do
protected true protected true
end end
trait(:unprotected) do trait :unprotected do
protected false protected false
end end
end end

View File

@ -4,6 +4,7 @@ FactoryGirl.define do
ref 'master' ref 'master'
sha '97de212e80737a608d939f648d959671fb0a0142' sha '97de212e80737a608d939f648d959671fb0a0142'
status 'pending' status 'pending'
protected false
project project
@ -59,6 +60,14 @@ FactoryGirl.define do
trait :failed do trait :failed do
status :failed status :failed
end end
trait :protected do
protected true
end
trait :unprotected do
protected false
end
end end
end end
end end

View File

@ -59,7 +59,11 @@ describe Ci::Build do
end end
context 'when protected is false' do context 'when protected is false' do
let!(:job) { create(:ci_build, protected: nil) } let!(:job) { create(:ci_build) }
before do
job.update_attribute(:protected, nil)
end
it { is_expected.not_to include(job) } it { is_expected.not_to include(job) }
end end