Last active
July 26, 2021 09:05
-
-
Save keymastervn/f8430e8d5073a3044bfaa03a74d0dfbb to your computer and use it in GitHub Desktop.
Get haml literal string with CODEOWNERS
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
require 'code_owners' | |
require 'open3' | |
require 'pp' | |
UNOWNED = 'UNOWNED' | |
# {:file=>"vendor/assets/stylesheets/token-input.css", :owner=>"UNOWNED", :line=>nil, :pattern=>nil} | |
@codeowners = CodeOwners.ownerships | |
def syscall(*cmd) | |
begin | |
stdout, stderr, status = Open3.capture3(*cmd) | |
status.success? && stdout.slice!(0..-(1 + $/.size)) # strip trailing eol | |
rescue | |
end | |
end | |
def get_owners(file) | |
@codeowners.find { |e| e[:file] == file }&.dig(:owner) || UNOWNED | |
end | |
def to_obj(suspect) | |
space_idx = suspect.index(' ') | |
colon_idx = suspect.index(':') | |
file = suspect[0..(colon_idx -1)].sub('./', '') | |
path = suspect[0..(space_idx -1)] | |
content = suspect[space_idx..-1] | |
{ | |
file: file, | |
path: path, | |
content: content, | |
codeowner: get_owners(file) | |
} | |
end | |
cmd = 'find . -name "*.haml" | xargs grep -n ".\+[ >^=]\([A-Z][a-z]\+\b\)"' | |
suspects = syscall(cmd) | |
suspects = suspects&.split("\n") | |
# pipe = IO.popen('pbcopy', 'w') | |
# suspects.map { |suspect| to_obj(suspect)}.each do |obj| | |
# pipe.puts "\"#{obj[:path]}\"\t\"#{obj[:content]}\"\t\"#{obj[:codeowner]}\"" | |
# end | |
File.open("/tmp/list.csv", "w") do |f| | |
suspects.map { |suspect| to_obj(suspect)}.each do |obj| | |
f.puts "\"#{obj[:path]}\"\t\"#{obj[:content]}\"\t#{obj[:codeowner]}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment