Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
Fix matching generic patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ekadlecova committed Feb 22, 2019
1 parent 33f3d71 commit 2115f48
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ GEM
fuzzy_match (2.1.0)
git (1.5.0)
jaro_winkler (1.5.1)
method_source (0.9.2)
json (2.1.0)
method_source (0.9.2)
parallel (1.12.1)
parser (2.5.3.0)
ast (~> 2.4.0)
Expand Down Expand Up @@ -72,7 +72,7 @@ DEPENDENCIES
rspec (~> 3.0)
rubocop (~> 0.61.1)
rubocop-rspec (~> 1.30)
simplecov
simplecov (~> 0.16.1)

BUNDLED WITH
1.17.3
8 changes: 4 additions & 4 deletions codeowners-checker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require 'codeowners/checker/version'
Gem::Specification.new do |spec|
spec.name = 'codeowners-checker'
spec.version = Codeowners::Checker::VERSION
spec.authors = ['Jônatas Davi Paganini', 'Eva Kadlecova', 'Michal Papis']
spec.authors = ['Jônatas Davi Paganini', 'Eva Kadlecová', 'Michal Papis']
spec.email = ['[email protected]']

spec.summary = 'Check consistency of Github CODEOWNERS and git changes.'
Expand All @@ -24,11 +24,11 @@ Gem::Specification.new do |spec|
spec.add_dependency 'git', '~> 1.5'
spec.add_dependency 'thor', '~> 0.20.3'
spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'pry', '~> 0.12.2'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rb-readline', '~> 0.5.5'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 0.61.1'
spec.add_development_dependency 'rubocop-rspec', '~> 1.30'
spec.add_development_dependency 'pry', '~> 0.12.2'
spec.add_development_dependency 'rb-readline', '~> 0.5.5'
spec.add_development_dependency 'simplecov', '~> 0.16.1'
end
2 changes: 1 addition & 1 deletion lib/codeowners/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def defined_owner?(file)
codeowners.find do |line|
next unless line.pattern?

return true if file == line.pattern
return true if line.match_file?(file)
end

@when_new_file&.call(file) if @when_new_file
Expand Down
10 changes: 10 additions & 0 deletions lib/codeowners/checker/group/pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ def parse(line)
@pattern, *@owners = line.split(/\s+/)
end

def match_file?(file)
regex.match(file)
end

def to_s
[@pattern, @owners].join(' ')
end

private

def regex
Regexp.new(pattern.gsub(%r{/\*\*}, '(/[^/]+)+').gsub(/\*/, '[^/]+'))
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/codeowners/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def write_codeowners
end

def suggest_add_to_codeowners(file)
return unless yes?("File added: #{file.inspect}. Add owner to CODEOWNERS?")
return unless yes?("File added: #{file.inspect}. Add owner to the CODEOWNERS file?")

owner = ask('File owner(s): ')
new_line = create_new_pattern(file, owner)
Expand All @@ -73,7 +73,7 @@ def add_pattern(pattern, subgroups)
return if insert_pattern_into_subgroup(pattern, subgroups) == true
end

@checker.main_group.add(pattern) if yes?('Add to the end of the codeowners file?')
@checker.main_group.add(pattern) if yes?('Add to the end of the CODEOWNERS file?')
end

def insert_pattern_into_subgroup(pattern, subgroups)
Expand Down Expand Up @@ -120,7 +120,7 @@ def apply_suggestion(line, suggestion)

def make_suggestion(suggestion)
ask(<<~QUESTION, limited_to: %w[y i e d])
Replace with: #{suggestion}?
Replace with: #{suggestion.inspect}?
(y) yes
(i) ignore
(e) edit the pattern
Expand Down
58 changes: 58 additions & 0 deletions spec/codeowners/checker/group/pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,64 @@
require 'codeowners/checker/group/pattern'

RSpec.describe Codeowners::Checker::Group::Pattern do
describe '#owner' do
subject { described_class.build(line) }

let(:line) { 'pattern @owner @owner1 @owner2' }

it 'returns the first owner' do
expect(subject.owner).to eq('@owner')
end
end

describe '#match_file?' do
subject { described_class.build(line) }

{
'directory/* @owner @owner2' => {
'file.rb' => false,
'directory/file.rb' => true,
'directory/subdirectory/file.rb' => false
},
'* @owner' => {
'file.rb' => true,
'directory/file.rb' => true,
'directory/subdirectory/file.rb' => true
},
'dir/dir1/* @owner' => {
'file.rb' => false,
'dir/file.rb' => false,
'dir/dir1/file.rb' => true,
'dir/dir1/dir2/file.rb' => false
},
'dir/dir1/file.rb @owner' => {
'file.rb' => false,
'dir/file.rb' => false,
'dir/dir1/file.rb' => true,
'dir/dir1/file1.rb' => false,
'dir/dir1/dir2/file.rb' => false
},
'*.js @owner' => {
'file.rb' => false,
'dir/file.js' => true,
'dir/dir1/file.rb' => false,
'dir/dir1/file1.js' => true,
'dir/dir1/dir2/file.js' => true
}
}.each do |content, tests|
context "when the line is #{content.inspect}" do
let(:line) { content }
tests.each do |file, result|
if result
it { is_expected.to be_match_file(file) }
else
it { is_expected.not_to be_match_file(file) }
end
end
end
end
end

describe '#to_s' do
subject { described_class.build(line) }

Expand Down
45 changes: 35 additions & 10 deletions spec/codeowners/checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,46 @@ def setup_git_for_project
end

context 'when introducing a new file in the git tree' do
before do
on_project_folder do
filename = 'lib/new_file.rb'
File.open(filename, 'w+') do |file|
file.puts '# add some ruby code here'
context 'when the file is not in the CODEOWNERS' do
before do
on_project_folder do
filename = 'lib/new_file.rb'
File.open(filename, 'w+') do |file|
file.puts '# add some ruby code here'
end
git.add filename
git.commit('New ruby file on lib')
end
git.add filename
git.commit('New ruby file on lib')
end

let(:from) { 'HEAD~1' }

it 'fails if the file is not referenced in .github/CODEOWNERS' do
expect(subject).to eq(missing_ref: ['lib/new_file.rb'], useless_pattern: [])
end
end

let(:from) { 'HEAD~1' }
context 'when the files are not in the CODEOWNERS but generic patterns are' do
before do
on_project_folder do
filename = 'lib/billing/new_file.rb'
filename2 = 'app/file.js'
Dir.mkdir('app')
File.open(filename, 'w+') { |file| file.puts '# add some ruby code here' }
File.open(filename2, 'w+') { |file| file.puts '# add some ruby code here' }
File.open('.github/CODEOWNERS', 'a+') { |f| f.puts '*.js @owner3' }

git.add filename
git.add filename2
git.commit('New files')
end
end

let(:from) { 'HEAD~1' }

it 'fails if the file is not referenced in .github/CODEOWNERS' do
expect(subject).to eq(missing_ref: ['lib/new_file.rb'], useless_pattern: [])
it 'does not list the files as missing reference' do
expect(subject).to eq(missing_ref: [], useless_pattern: [])
end
end
end

Expand Down

0 comments on commit 2115f48

Please sign in to comment.