Created
February 12, 2020 13:14
-
-
Save jonatas/5b6ab7a5565326271ed2a58898037bad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'pathspec', require: 'pathspec' | |
gem 'pry' | |
end | |
module CodeownersToDatabase | |
module_function | |
def patterns_with_owners | |
@patterns_with_owners ||= | |
File.readlines('.github/CODEOWNERS').map do |line| | |
line = line.strip.chomp | |
next if line.empty? | |
next if line.start_with?('#') | |
pattern, *owners = line.split ' ' | |
[PathSpec.from_lines(pattern), owners] | |
end.compact | |
end | |
def import(file) | |
target = patterns_with_owners.find do |spec, owners| | |
spec.match(file) | |
end | |
owners = target ? target.last : [] | |
insert_database(file, owners) | |
end | |
def insert_database(file, owners) | |
puts "INSERT INTO owners (file, owners) values ('#{file}', '#{owners.join(', ')}');" | |
end | |
def import_all!(files=Dir.glob('**/*')) | |
puts <<~SQL | |
CREATE TABLE owners (file TEXT, owners TEXT); | |
CREATE VIEW expanded_owners AS | |
SELECT unnest(regexp_split_to_array(file, '/')) AS fragment, | |
owners FROM owners; | |
CREATE VIEW keyword_map AS SELECT COUNT(1) AS occurrences, fragment, owners | |
from expanded_owners | |
where fragment !~ '\.' | |
group by fragment, owners order by 2; | |
SQL | |
files.each(&method(:import)) | |
puts "select * from keyword_map;" | |
end | |
end | |
CodeownersToDatabase.import_all! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment