Skip to content

Commit

Permalink
Merge pull request #11580 from danidoni/refactor-configure-repositori…
Browse files Browse the repository at this point in the history
…es-validations

Generalize workflow step validations
  • Loading branch information
dmarcoux committed Sep 7, 2021
2 parents f4dc113 + c3fa655 commit 9a92201
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/api/app/models/workflow/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Workflow::Step
include ActiveModel::Model

validates :source_project_name, presence: true
validate :validate_step_instructions

attr_accessor :scm_webhook, :step_instructions, :token

Expand All @@ -17,6 +18,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
Expand Down
1 change: 1 addition & 0 deletions src/api/app/models/workflow/step/branch_package_step.rb
Original file line number Diff line number Diff line change
@@ -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 = {})
Expand Down
7 changes: 0 additions & 7 deletions src/api/app/models/workflow/step/configure_repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,12 +27,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 }

Expand Down
1 change: 1 addition & 0 deletions src/api/app/models/workflow/step/link_package_step.rb
Original file line number Diff line number Diff line change
@@ -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 = {})
Expand Down
5 changes: 3 additions & 2 deletions src/api/spec/models/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9a92201

Please sign in to comment.