diff --git a/src/api/app/models/workflow/step.rb b/src/api/app/models/workflow/step.rb index 2022dc9d571..ff74bf8b7f3 100644 --- a/src/api/app/models/workflow/step.rb +++ b/src/api/app/models/workflow/step.rb @@ -3,6 +3,7 @@ class Workflow::Step include ActiveModel::Model validates :source_project_name, presence: true + validate :validate_step_instructions attr_reader :scm_extractor_payload, :step_instructions, :token @@ -18,6 +19,12 @@ def call(_options) protected + def validate_step_instructions + self.class::REQUIRED_KEYS.each do |required_key| + errors.add(:base, "The '#{required_key}' key is missing") unless step_instructions.key?(required_key) + end + end + def source_project_name step_instructions[:source_project] end diff --git a/src/api/app/models/workflow/step/branch_package_step.rb b/src/api/app/models/workflow/step/branch_package_step.rb index ab7d94e9e21..fdfbc0b8d1e 100644 --- a/src/api/app/models/workflow/step/branch_package_step.rb +++ b/src/api/app/models/workflow/step/branch_package_step.rb @@ -1,4 +1,5 @@ class Workflow::Step::BranchPackageStep < ::Workflow::Step + REQUIRED_KEYS = [:source_project, :source_package].freeze validates :source_package_name, presence: true def call(options = {}) diff --git a/src/api/app/models/workflow/step/configure_repositories.rb b/src/api/app/models/workflow/step/configure_repositories.rb index eb9ddf71b66..1b1f4cb796f 100644 --- a/src/api/app/models/workflow/step/configure_repositories.rb +++ b/src/api/app/models/workflow/step/configure_repositories.rb @@ -2,7 +2,6 @@ class Workflow::Step::ConfigureRepositories < Workflow::Step REQUIRED_KEYS = [:source_project, :repositories].freeze REQUIRED_REPOSITORY_KEYS = [:architectures, :name, :target_project, :target_repository].freeze - validate :validate_step_instructions validate :validate_repositories validate :validate_architectures @@ -25,12 +24,6 @@ def call(_options = {}) private - def validate_step_instructions - REQUIRED_KEYS.each do |required_key| - errors.add(:base, "configure_repositories step: The '#{required_key}' key is missing.") unless step_instructions.key?(required_key) - end - end - def validate_repositories return if step_instructions[:repositories].all? { |repository| repository.keys.sort == REQUIRED_REPOSITORY_KEYS } diff --git a/src/api/app/models/workflow/step/link_package_step.rb b/src/api/app/models/workflow/step/link_package_step.rb index 36e5c5a28d1..3082979d2a3 100644 --- a/src/api/app/models/workflow/step/link_package_step.rb +++ b/src/api/app/models/workflow/step/link_package_step.rb @@ -1,4 +1,5 @@ class Workflow::Step::LinkPackageStep < ::Workflow::Step + REQUIRED_KEYS = [:source_project, :source_package].freeze validates :source_package_name, presence: true def call(options = {}) diff --git a/src/api/spec/models/workflow_spec.rb b/src/api/spec/models/workflow_spec.rb index 8590191eb0a..559f86843d6 100644 --- a/src/api/spec/models/workflow_spec.rb +++ b/src/api/spec/models/workflow_spec.rb @@ -134,14 +134,15 @@ it 'raises an exception for non-present instructions' do expect { subject.valid? }.to raise_error(Token::Errors::InvalidWorkflowStepDefinition, - "Invalid workflow step definition: Source project name can't be blank and Source package name can't be blank") + "Invalid workflow step definition: Source project name can't be blank, The 'source_project' key is missing, The 'source_package' + key is missing, and Source package name can't be blank".squish) end end context 'with a supported step but invalid step configuration' do let(:yaml) do { 'steps' => [{ 'branch_package' => { source_project: nil, - source_package_fake: 'package' } }] } + source_package: nil } }] } end it 'raises an exception for invalid instructions' do