Skip to content

Commit

Permalink
Fixes #37721 - Hammer erratum list output is wrong when using content…
Browse files Browse the repository at this point in the history
…-view filter
  • Loading branch information
sjha4 committed Aug 9, 2024
1 parent f65f4b2 commit 3df9384
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/hammer_cli_katello/erratum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@ class ListCommand < HammerCLIKatello::ListCommand
validate_options :before, 'IdResolution' do
organization_options = [:option_organization_id, :option_organization_name, \
:option_organization_label]
environment_options = [:option_environment_name, :option_environment_id]

if option(:option_product_name).exist?
any(*organization_options).required
end

if option(:option_content_view_name).exist?
any(*organization_options).required
end

if option(:option_content_view_id).exist? || option(:option_content_view_name).exist?
any(*environment_options).required
end
end

build_options do |o|
Expand Down
18 changes: 12 additions & 6 deletions lib/hammer_cli_katello/id_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,21 @@ def repository_ids(options)
def content_view_version_id(options)
key_id = HammerCLI.option_accessor_name("id")
key_content_view_id = HammerCLI.option_accessor_name("content_view_id")
key_environment_id = HammerCLI.option_accessor_name("environment_id")
from_environment_id = HammerCLI.option_accessor_name("from_environment_id")

return options[key_id] if options[key_id]

options[key_content_view_id] ||= search_and_rescue(:content_view_id, "content_view", options)
options[key_environment_id] ||= search_and_rescue(:environment_id, "environment", options)

results = find_resources(:content_view_versions, options)
options[from_environment_id] ||= from_lifecycle_environment_id(options)

return pick_result(results, @api.resource(:content_view_versions))['id'] if results.size == 1

if results.size > 1 && options[from_environment_id]
results_in_from_environment = results.select do |version|
member_of_environment_ids = version['environments'].map { |env| env['id'].to_s }
member_of_environment_ids.include? options[from_environment_id].to_s
end
results_in_from_environment
.sort { |a, b| a['version'].to_f <=> b['version'].to_f }.last['id']
filter_versions_in_from_environment(results, options)
else
nil
end
Expand All @@ -148,6 +145,15 @@ def content_view_version_id(options)

private

def filter_versions_in_from_environment(results, options)
results_in_from_environment = results.select do |version|
member_of_environment_ids = version['environments'].map { |env| env['id'].to_s }
member_of_environment_ids.include? options[from_environment_id].to_s
end
results_in_from_environment
.sort { |a, b| a['version'].to_f <=> b['version'].to_f }.last['id']
end

def from_lifecycle_environment_id(options)
environment_id = HammerCLI.option_accessor_name("environment_id")
environment_name = HammerCLI.option_accessor_name("environment_name")
Expand Down

0 comments on commit 3df9384

Please sign in to comment.