This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove usage of codeowner-checker from project and pulled in required…
… classes (#11)
- Loading branch information
Showing
21 changed files
with
1,211 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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) |
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
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 |
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
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' |
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
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 |
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
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 |
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
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 |
Oops, something went wrong.