Skip to content

Commit

Permalink
Remove duplicated validation in branch and link package step
Browse files Browse the repository at this point in the history
The super class of the steps already takes care of validating
the presence of the source package and source project names through
the REQUIRED_KEYS constant.
Additionally we have to add a validation to check for the
presence of the value for a required key.
  • Loading branch information
krauselukas authored and Dany Marcoux committed Sep 21, 2021
1 parent 3f8bf8a commit 00fc18f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
11 changes: 10 additions & 1 deletion src/api/app/models/workflow/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ def create_or_update_subscriptions(package, workflow_filters)

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)
unless step_instructions.key?(required_key)
errors.add(:base, "The '#{required_key}' key is missing")
next
end

errors.add(:base, "The '#{required_key}' key must provide a value") if step_instructions[required_key].blank?
end
end

def source_package_name
step_instructions[:source_package]
end

def source_project_name
step_instructions[:source_project]
end

def target_package_name
return step_instructions[:target_package] if step_instructions[:target_package].present?

Expand Down
7 changes: 0 additions & 7 deletions src/api/app/models/workflow/step/branch_package_step.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
class Workflow::Step::BranchPackageStep < ::Workflow::Step
REQUIRED_KEYS = [:source_project, :source_package].freeze

validates :source_project_name, presence: true
validates :source_package_name, presence: true

def call(options = {})
return unless valid?

workflow_filters = options.fetch(:workflow_filters, {})
branch_package(workflow_filters)
end

def source_project_name
step_instructions[:source_project]
end

private

def branch_package(workflow_filters = {})
Expand Down
7 changes: 0 additions & 7 deletions src/api/app/models/workflow/step/link_package_step.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
class Workflow::Step::LinkPackageStep < ::Workflow::Step
REQUIRED_KEYS = [:source_project, :source_package].freeze

validates :source_project_name, presence: true
validates :source_package_name, presence: true

def call(options = {})
return unless valid?

workflow_filters = options.fetch(:workflow_filters, {})
link_package(workflow_filters)
end

def source_project_name
step_instructions[:source_project]
end

private

def link_package(workflow_filters = {})
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/validators/workflow_steps_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
it 'is not valid and has an error message' do
subject.valid?
expect(subject.errors.full_messages.to_sentence).to eq("The following workflow steps are unsupported: 'unsupported_step' and " \
"The 'source_package' key is missing and Source package name can't be blank")
"The 'source_package' key is missing")
end
end
end
Expand Down

0 comments on commit 00fc18f

Please sign in to comment.