Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

1.7 KB
Raw Blame
# frozen_string_literal: true

namespace :pundit do
  desc 'Show the latest Pundit security warnings'
  task :warnings, [:lines] => :environment do |_t, args|
    lines = args[:lines]&.to_i || 50
    log_file = Rails.root.join('log', "pundit_security_#{Rails.env}.log")

    if File.exist?(log_file)
      puts "\nπŸ“‹ Latest #{lines} Pundit security warnings:\n"
      puts '═' * 80
      system("tail -n #{lines} #{log_file}")
      puts '═' * 80

      # Count warnings per controller
      puts "\nπŸ“Š Summary:"
      warnings = File.readlines(log_file).select { |line| line.include?('SECURITY WARNING') }
      controller_counts = warnings.group_by { |w| w[/(\w+Controller)#/, 1] }.transform_values(&:count)
      controller_counts.sort_by { |_, count| -count }.each do |controller, count|
        puts "  β€’ #{controller}: #{count} warnings"
      end
      puts "\nπŸ”΄ Total: #{warnings.count} warnings\n\n"
    else
      puts "❌ No Pundit security log file found: #{log_file}"
    end
  end

  desc 'Clear the Pundit security log'
  task clear_log: :environment do
    log_file = Rails.root.join('log', "pundit_security_#{Rails.env}.log")
    if File.exist?(log_file)
      File.truncate(log_file, 0)
      puts "βœ… Pundit security log cleared: #{log_file}"
    else
      puts "❌ No log file found: #{log_file}"
    end
  end

  desc 'Show live updates of the Pundit warnings'
  task watch: :environment do
    log_file = Rails.root.join('log', "pundit_security_#{Rails.env}.log")

    if File.exist?(log_file)
      puts "πŸ‘€ Watching Pundit security log... (Ctrl+C to quit)\n\n"
      system("tail -f #{log_file}")
    else
      puts "❌ No log file found: #{log_file}"
    end
  end
end