Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #35503 - Notify user only when new errata are added to repo #10328

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/lib/actions/katello/repository/errata_mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ module Actions
module Katello
module Repository
class ErrataMail < Actions::EntryAction
middleware.use Actions::Middleware::ExecuteIfContentsChanged

def plan(repo, contents_changed = nil)
last_updated = repo.repository_errata.order('updated_at ASC').last.try(:updated_at) || Time.now
plan_self(:repo => repo.id, :contents_changed => contents_changed, :last_updated => last_updated.to_s)
def plan(repo)
plan_self(:repo => repo.id, :associated_errata_before_syncing => repo.repository_errata.pluck(:erratum_id).uniq.sort.reverse, :new_associated_errata => [])
Copy link
Member

@pmoravec pmoravec Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the .reverse (dtto for run)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I previously tried a format where the truncated input remains an Array containing the first few IDs, appended with the truncation message. The reversed order made sense because the ordering of the erratum ids corresponds to the order which the erratum records were created, so with a partially truncated output it means we see the most recently created (which in most cases would be the most recently associated) at the top, which provides a "good intuition" for the cutoff between new vs. prior associated errata.

I then leaned towards just replacing the entire Array with a string containing the count, if the length is greater than some number, reasoning that it's simpler for the user to read (the data types were mixed within the Array in the previous way, so the user would see several integers followed by an ugly double-quoted string) and maybe easier to maintain.

But I chose the Array length for truncation to kick in very arbitrarily as 3, and I'm not sure if there is some better value. In particular if we allow the user to see a larger Array then I think the reverse ordering feels more intuitive. Since Array reversal should be cheap (linear complexity) I just left it in.

What do you think about it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, then it makes sense to me.

end

def run
::User.current = ::User.anonymous_admin
repo = ::Katello::Repository.find(input[:repo])
input[:new_associated_errata] = repo.repository_errata.pluck(:erratum_id).uniq.sort.reverse - input[:associated_errata_before_syncing]

users = ::User.select { |user| user.receives?(:sync_errata) && user.organization_ids.include?(repo.organization.id) && user.can?(:view_products, repo.product) }.compact
errata = ::Katello::Erratum.where(:id => repo.repository_errata.where('katello_repository_errata.updated_at > ?', input['last_updated'].to_datetime).pluck(:erratum_id))
errata = ::Katello::Erratum.where(:id => input[:new_associated_errata])

[:associated_errata_before_syncing, :new_associated_errata].each do |key|
input[key] = "Trimmed list... (#{input[key].length} #{key.to_s.gsub('_', ' ')})" if input[key].length > 3
end

begin
MailNotification[:sync_errata].deliver(:users => users, :repo => repo, :errata => errata) unless (users.blank? || errata.blank?)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/actions/katello/repository/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def plan(repo, options = {})
plan_action(Katello::Repository::FetchPxeFiles, :id => repo.id)
plan_action(Katello::Repository::CorrectChecksum, repo)
concurrence do
plan_action(Katello::Repository::ErrataMail, repo, output[:contents_changed])
plan_action(Katello::Repository::ErrataMail, repo)
plan_action(Actions::Katello::Applicability::Repository::Regenerate, :repo_ids => [repo.id]) if generate_applicability
end
plan_self(:id => repo.id, :sync_result => output, :skip_metadata_check => skip_metadata_check, :validate_contents => validate_contents,
Expand Down
6 changes: 3 additions & 3 deletions test/actions/katello/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,11 @@ class ErrataMailerTest < TestBase

it 'plans' do
action = create_action action_class
planned_action = plan_action action, repository, true
last_updated = repository.repository_errata.order("updated_at desc").first.updated_at.to_s
planned_action = plan_action action, repository
old_errata_ids = repository.errata.map(&:id).uniq.sort.reverse
new_errata = ::Katello::Erratum.where.not(:id => repository.repository_errata.pluck(:erratum_id)).first
repository.errata << new_errata
assert_equal planned_action.execution_plan.planned_run_steps.first.input, "repo" => repository.id, "contents_changed" => true, "last_updated" => last_updated
assert_equal planned_action.execution_plan.planned_run_steps.first.input, "repo" => repository.id, "associated_errata_before_syncing" => old_errata_ids, "new_associated_errata" => []
end
end

Expand Down