Skip to content

Instantly share code, notes, and snippets.

@fredplante
Last active November 30, 2022 10:44
Show Gist options
  • Save fredplante/93c647e89618b4bc0c0c63d2bd7e0967 to your computer and use it in GitHub Desktop.
Save fredplante/93c647e89618b4bc0c0c63d2bd7e0967 to your computer and use it in GitHub Desktop.
Checks if entries can be removed from CODEOWNERS file
#!/usr/bin/env ruby
# This script assumes CODEOWNERS files can be found in "../.github/
lines = File.readlines(File.join(__dir__, "..", ".github", "CODEOWNERS"))
stale_entries = []
lines.each_with_index do |line, index|
original_line = line
# Normalize the beginning of each entries
line = line.gsub(/[[:space:]]+/, " ").strip.delete_prefix(".").delete_prefix("/")
# Skip if blank line or comment line
next if line.empty? || line.start_with?("#")
# Skip if entry has a wildcard character
next if line.include?("*")
# Extract the path from the entry
path = line.match(/^(\S+)\s+.*$/).captures.first rescue line
# Skip if it's a file
next if FileTest.file?(path)
# Skip if it's a non empty directory
next if (FileTest.directory?(path) && !Dir.empty?(path))
padding = lines.size.to_s.size
stale_entries << "#{(index+1).to_s.rjust(padding, " ")}: #{original_line}"
end
puts "Following lines can be removed from CODEOWNERS:"
puts stale_entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment