Skip to content

Commit

Permalink
Add logging to file and exception handling
Browse files Browse the repository at this point in the history
- Logging allows us to gather a list of documents that cannot be found
- Failure to find is typically because the path/slug has changed.
  • Loading branch information
ellohez committed Nov 21, 2024
1 parent 40d9f35 commit 3f1505e
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/tasks/access_and_permissions.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,51 @@ require "csv"

namespace :permissions do
desc "Add an organisation to an document's access permissions list"
task :add_organisation_access, %i[document_content_id org_content_id] => :environment do |_, args|
task :add_organisation_access, %i[document_content_id org_content_id log_file] => :environment do |_, args|
document = Artefact.find_by(id: args[:document_content_id])
if document.nil?
puts "Document not found, no permissions changed."
if args[:log_file]
args[:log_file].puts "Document ID #{args[:org_content_id]} not found, no permissions changed."
else
puts "Document with ID #{args[:org_content_id]} not found, no permissions changed."
end
elsif document.latest_edition.owning_org_content_ids.include? args[:org_content_id]
puts "Organisation already has permission to access this document"
if args[:log_file]
args[:log_file].puts "Organisation already has permission to access the document with ID - #{document.id}"
else
puts "Organisation already has permission to access the document with ID - #{document.id}"
end
else
Edition.where(panopticon_id: document.id).each do |edition|
edition.owning_org_content_ids << args[:org_content_id]
edition.save!(validate: false)
end
document.save_as_task!("PermissionsAddition")
puts "Access permission successfully assigned"
if args[:log_file]
args[:log_file].puts("Access permission successfully assigned to document with ID - #{document.id}")
else
puts "Access permission successfully assigned to document with ID - #{document.id}"
end
end
end

desc "Bulk process access permissions from CSV of URLs - See doc"
task :bulk_process_access_flags, %i[csv_filename organisation_id] => :environment do |_, args|
log_file = File.new("permissions_rake_log.txt","w")

Check failure on line 35 in lib/tasks/access_and_permissions.rake

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/SpaceAfterComma: Space missing after comma. (https://rubystyle.guide#spaces-operators)
CSV.foreach(args[:csv_filename], headers: true) do |row|
path = row[1]
path.slice! "https://www.gov.uk/"
path&.slice! "https://www.gov.uk/"
document = Artefact.find_by(slug: path)

next if document.nil?

Rake::Task["permissions:add_organisation_access"].reenable # I prefer to do this first but can be done after if cleaner
Rake::Task["permissions:add_organisation_access"].invoke(document.id, args[:organisation_id])
Rake::Task["permissions:add_organisation_access"].reenable # Can be done after if cleaner
Rake::Task["permissions:add_organisation_access"].invoke(document.id, args[:organisation_id], log_file)
rescue Mongoid::Errors::DocumentNotFound => e
log_file.puts "--- Document not found error ---"
log_file.puts e.detailed_message
log_file.puts "------"
end
log_file.close
end
end

0 comments on commit 3f1505e

Please sign in to comment.