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

Commit

Permalink
Remove usage of codeowner-checker from project and pulled in required…
Browse files Browse the repository at this point in the history
… classes (#11)
  • Loading branch information
poloka authored Oct 16, 2023
1 parent de0cfef commit bb4290c
Show file tree
Hide file tree
Showing 21 changed files with 1,211 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
- Update to support ruby 3.2 ([#9](https://github.com/cerner/codeowner_validator/pull/9))

# 0.3.1
- Back version of ruby to be in RVM supported set ([#10](https://github.com/cerner/codeowner_validator/pull/10))
- Back version of ruby to be in RVM supported set ([#10](https://github.com/cerner/codeowner_validator/pull/10))

# 0.4.0
- Remove usage of codeowner-checker from project and pulled in required classes ([#11](https://github.com/cerner/codeowner_validator/pull/11))
4 changes: 2 additions & 2 deletions codeowner_validator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Gem::Specification.new do |spec|
# rubocop:enable Gemspec/RequiredRubyVersion

spec.add_dependency 'rainbow', '>= 2.0', '< 4.0.0'
spec.add_dependency 'thor', '>= 0.19'
spec.add_dependency 'thor', '>= 1.0'

spec.add_dependency 'tty-prompt', '~> 0.12'
spec.add_dependency 'tty-spinner', '~> 0.4'
spec.add_dependency 'tty-table', '~> 0.8'

spec.add_dependency 'codeowners-checker', '~> 1.1'
spec.add_dependency 'git', '~> 1.0'
spec.add_dependency 'pathspec', '>= 0.2'
end
4 changes: 1 addition & 3 deletions lib/codeowner_validator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

require 'thor'
require 'codeowners/checker'
# pull in monkeypatch for codeowners-checker
require_relative 'codeowners/checker/group/line'
Dir.glob(File.join(File.dirname(__FILE__), 'codeowner_validator', '**/*.rb'), &method(:require))
Dir.glob(File.join(File.dirname(__FILE__), 'codeowners', '**/*.rb'), &method(:require))

# Public: The code owner validator space is utilized for validations against
# the code owner file for a given repository.
Expand Down
3 changes: 1 addition & 2 deletions lib/codeowner_validator/code_owners.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
require 'pathname'
require_relative 'helpers/utility_helper'
require 'codeowner_validator/lists/whitelist'
require 'codeowners/checker/group'
require_relative '../codeowners/checker/group/line'
require_relative '../codeowners/checker/group'

# rubocop:disable Style/ImplicitRuntimeError
module CodeownerValidator
Expand Down
9 changes: 8 additions & 1 deletion lib/codeowner_validator/helpers/utility_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ module UtilityHelper
def in_folder(folder)
raise "The folder location '#{folder}' does not exists" unless File.directory?(folder)

with_clean_env do
if defined?(Bundler)
method = Bundler.respond_to?(:with_unbundled_env) ? :with_unbundled_env : :with_clean_env
Bundler.send(method) do
Dir.chdir folder do
yield
end
end
else
Dir.chdir folder do
yield
end
Expand Down
2 changes: 1 addition & 1 deletion lib/codeowner_validator/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module CodeownerValidator
VERSION = '0.3.1'
VERSION = '0.4.0'

# version module
module Version
Expand Down
15 changes: 15 additions & 0 deletions lib/codeowners/checker/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Codeowners
class Checker
# Array.delete in contrary to Ruby documentation uses == instead of equal? for comparison.
# safe_delete removes an object from an array comparing objects by equal? method.
module Array
def safe_delete(object)
delete_at(index { |item| item.equal?(object) })
end
end
end
end

Array.prepend(Codeowners::Checker::Array)
158 changes: 158 additions & 0 deletions lib/codeowners/checker/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# frozen_string_literal: true

require_relative 'line_grouper'
require_relative 'group/line'
require_relative 'array'

module Codeowners
class Checker
# Manage the groups content and handle operations on the groups.
class Group
include Enumerable

attr_accessor :parent

def self.parse(lines)
new.parse(lines)
end

def initialize
@list = []
end

def each(&block)
@list.each do |object|
if object.is_a?(Group)
object.each(&block)
else
yield(object)
end
end
end

def parse(lines)
LineGrouper.call(self, lines)
end

def to_content
@list.flat_map(&:to_content)
end

def to_file
@list.flat_map(&:to_file)
end

# Returns an array of strings representing the structure of the group.
# It indent internal subgroups for readability and debugging purposes.
def to_tree(indentation = '')
@list.each_with_index.flat_map do |item, index|
if indentation.empty?
item.to_tree(indentation + ' ')
elsif index.zero?
item.to_tree(indentation + '+ ')
elsif index == @list.length - 1
item.to_tree(indentation + '\\ ')
else
item.to_tree(indentation + '| ')
end
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

def owner
owners.first
end

# Owners are ordered by the amount of occurrences
def owners
all_owners.group_by(&:itself).sort_by do |_owner, occurrences|
-occurrences.count
end.map(&:first)
end

def subgroups_owned_by(owner)
@list.flat_map do |item|
next unless item.is_a?(Group)

a = []
a << item if item.owner == owner
a += item.subgroups_owned_by(owner)
a
end.compact
end

def title
@list.first.to_s
end

def create_subgroup
group = self.class.new
group.parent = self
@list << group
group
end

def add(line)
line.parent = self
@list << line
end

def insert(line)
line.parent = self
index = insert_at_index(line)
@list.insert(index, line)
end

def remove(line)
@list.safe_delete(line)
remove! unless any? { |object| object.is_a? Pattern }
end

def remove!
@list.clear
parent&.remove(self)
self.parent = nil
end

def ==(other)
other.is_a?(Group) && other.list == list
end

protected

attr_accessor :list

private

def all_owners
flat_map do |item|
item.owners if item.pattern?
end.compact
end

# rubocop:disable Metrics/AbcSize
def insert_at_index(line)
new_patterns_sorted = @list.grep(Pattern).dup.push(line).sort
previous_line_index = new_patterns_sorted.index { |l| l.equal? line } - 1
previous_line = new_patterns_sorted[previous_line_index]
padding = previous_line.pattern.size + previous_line.whitespace - line.pattern.size
line.whitespace = [1, padding].max

if previous_line_index >= 0
@list.index { |l| l.equal? previous_line } + 1
else
find_last_line_of_initial_comments
end
end
# rubocop:enable Metrics/AbcSize

def find_last_line_of_initial_comments
@list.each_with_index do |item, index|
return index unless item.is_a?(Comment)
end
0
end
end
end
end
32 changes: 32 additions & 0 deletions lib/codeowners/checker/group/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require_relative 'line'

module Codeowners
class Checker
class Group
# Define and manage comment line.
class Comment < Line
# Matches if the line is a comment.
# @return [Boolean] if the line start with `#`
def self.match?(line)
line.start_with?('#')
end

# Return the comment level if the comment works like a markdown
# headers.
# @return [Integer] with the heading level.
#
# @example
# Comment.new('# First level').level # => 1
# Comment.new('## Second').level # => 2
def level
(@line[/^#+/] || '').size
end
end
end
end
end

require_relative 'group_begin_comment'
require_relative 'group_end_comment'
16 changes: 16 additions & 0 deletions lib/codeowners/checker/group/empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative 'line'

module Codeowners
class Checker
class Group
# Define line type empty line.
class Empty < Line
def self.match?(line)
line.empty?
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/codeowners/checker/group/group_begin_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require_relative 'comment'

module Codeowners
class Checker
class Group
# Define line type GroupBeginComment which is used for defining the beggining
# of a group.
class GroupBeginComment < Comment
def self.match?(line)
line.lstrip =~ /^#+ BEGIN/
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/codeowners/checker/group/group_end_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require_relative 'comment'

module Codeowners
class Checker
class Group
# Define line type GroupEndComment which is used for defining the end
# of a group.
class GroupEndComment < Comment
def self.match?(line)
line.lstrip =~ /^#+ END/
end
end
end
end
end
Loading

0 comments on commit bb4290c

Please sign in to comment.