Class: CodeownerValidator::CodeOwners
- Inherits:
-
Object
- Object
- CodeownerValidator::CodeOwners
- Includes:
- UtilityHelper
- Defined in:
- lib/codeowner_validator/code_owners.rb
Overview
Public: Manages the interactions with the GitHub CODEOWNERS file. Information such as assignments, missing assignments, etc are retrieved though the usage of this class.
Instance Attribute Summary collapse
-
#repo_path ⇒ Object
readonly
Public: The absolute path the the repository for evaluation.
Class Method Summary collapse
-
.persist!(repo_path:, **args) ⇒ Object
Public: Returns a instance of the [CodeOwners] object.
Instance Method Summary collapse
-
#changes_to_analyze(from: 'HEAD', to: 'HEAD^') ⇒ Hash
Public: Returns a [Hash] of key/value pairs of relative file name to git status (‘A’, ‘M’, ‘D’) between two commits.
-
#codeowner_file ⇒ String
Public: Returns a [String] for the code owner file if it exists; otherwise, raise exception.
-
#defined_owner?(file) ⇒ true
Public: Returns <true> if there is a defined owner for a given file.
-
#duplicated_patterns ⇒ Hash
Public: Returns a [Hash] of keyed item to array of patterns that are duplicated within the code owners file.
-
#find_by_owner(owner) ⇒ Array
Public: Returns the lines associated to a specific owner.
-
#included_files ⇒ Array
Public: Return all relative paths to files for evaluation if to be owned by a set of code owners.
-
#initialize(repo_path:, **_args) ⇒ CodeOwners
constructor
Public: Returns a instance of the [CodeOwners] object.
-
#invalid_reference_lines ⇒ Array
Public: Return a list of files from the codeowners file that are noted but do not exist.
-
#main_group ⇒ Array
Public: Returns an [Array] of [Codeowners::Checker::Group] objects indicating the grouping of code owners.
-
#missing_assignments ⇒ Array
Public: Return a list of files from the repository that do not have an owner assigned.
-
#pattern_has_files(pattern) ⇒ Hash
Public: Returns a [Hash] of key/value pairs of relative file name to git details for a supplied pattern.
-
#patterns_by_owner ⇒ Hash
Public: Returns the patterns utilized with an array association to those patterns.
-
#unrecognized_assignments ⇒ Array
Public: Return a list of files from the codowners file that are missing the owner assignment.
-
#useless_pattern ⇒ Array
Public: Returns an [Array] of patterns that have no files associated to them.
-
#whitelisted?(file) ⇒ true|false
Public: Returns <true> if the provided file is deemed whitelisted per the configuration; otherwise, <false>.
Methods included from UtilityHelper
Constructor Details
#initialize(repo_path:, **_args) ⇒ CodeOwners
Public: Returns a instance of the [CodeOwners] object
32 33 34 35 36 37 38 39 40 |
# File 'lib/codeowner_validator/code_owners.rb', line 32 def initialize(repo_path:, **_args) @repo_path = repo_path # initialize params to suppress warnings about instance variables not initialized @list = nil @codeowner_file = nil @codeowner_file_paths = nil @included_files = nil end |
Instance Attribute Details
#repo_path ⇒ Object (readonly)
Public: The absolute path the the repository for evaluation
16 17 18 |
# File 'lib/codeowner_validator/code_owners.rb', line 16 def repo_path @repo_path end |
Class Method Details
.persist!(repo_path:, **args) ⇒ Object
Public: Returns a instance of the [CodeOwners] object
23 24 25 |
# File 'lib/codeowner_validator/code_owners.rb', line 23 def persist!(repo_path:, **args) new(repo_path: repo_path, **args) end |
Instance Method Details
#changes_to_analyze(from: 'HEAD', to: 'HEAD^') ⇒ Hash
Public: Returns a [Hash] of key/value pairs of relative file name to git status (‘A’, ‘M’, ‘D’) between two commits.
54 55 56 |
# File 'lib/codeowner_validator/code_owners.rb', line 54 def changes_to_analyze(from: 'HEAD', to: 'HEAD^') git.diff(from, to).name_status.select(&whitelist) end |
#codeowner_file ⇒ String
Public: Returns a [String] for the code owner file if it exists; otherwise, raise exception
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/codeowner_validator/code_owners.rb', line 226 def codeowner_file return @codeowner_file if @codeowner_file codeowner_file_paths.each do |path| current_file_path = File.join(repo_path, path) return current_file_path if File.exist?(current_file_path) end raise "Unable to locate a code owners file located [#{codeowner_file_paths.join(',')}]" end |
#defined_owner?(file) ⇒ true
Public: Returns <true> if there is a defined owner for a given file
204 205 206 207 208 209 210 211 212 |
# File 'lib/codeowner_validator/code_owners.rb', line 204 def defined_owner?(file) main_group.find do |line| next unless line.pattern? return true if line.match_file?(file) end false end |
#duplicated_patterns ⇒ Hash
Public: Returns a [Hash] of keyed item to array of patterns that are duplicated within the code owners file
Example:
"config/domains/cert-int/CaseCartCoordinator_CERTIFICATION.yml": [
{Codeowners::Checker::Group::Pattern,
<span class='object_link'><a href="/gems/codeowner_validator/Codeowners/Checker/Group/Pattern" title="Codeowners::Checker::Group::Pattern (class)">Codeowners::Checker::Group::Pattern</a></span>
]
}
134 135 136 |
# File 'lib/codeowner_validator/code_owners.rb', line 134 def duplicated_patterns list.select { |l| l.pattern? }.group_by { |e| e.pattern }.select { |_k, v| v.size > 1 } end |
#find_by_owner(owner) ⇒ Array
Public: Returns the lines associated to a specific owner
192 193 194 195 196 197 198 |
# File 'lib/codeowner_validator/code_owners.rb', line 192 def find_by_owner(owner) main_group.find.select do |line| next unless line.pattern? line.owner == owner end end |
#included_files ⇒ Array
Public: Return all relative paths to files for evaluation if to be owned by a set of code owners
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/codeowner_validator/code_owners.rb', line 141 def included_files return @included_files if @included_files @included_files = [] in_folder repo_path do Dir.glob(File.join(Dir.pwd, '**/*')) do |f| p = Pathname.new(f) # only return files for evaluation next unless p.file? # in order to properly match, must evaluate relative to the repo location that is # being evaluated. absolute paths of the files do not match with relative matches relative_path_to_file = p.relative_path_from(Dir.pwd) @included_files << relative_path_to_file.to_s if whitelist.whitelisted?(relative_path_to_file) end end @included_files end |
#invalid_reference_lines ⇒ Array
Public: Return a list of files from the codeowners file that are noted but do not exist
114 115 116 117 118 119 120 121 |
# File 'lib/codeowner_validator/code_owners.rb', line 114 def invalid_reference_lines list.select do |line| next unless line.pattern? || line.is_a?(Codeowners::Checker::Group::UnrecognizedLine) filename = line.pattern? ? line.pattern : line.to_file File.exist?(File.join(repo_path, filename)) == false end end |
#main_group ⇒ Array
Public: Returns an [Array] of [Codeowners::Checker::Group] objects indicating the grouping of code owners
of code owners
219 220 221 |
# File 'lib/codeowner_validator/code_owners.rb', line 219 def main_group @main_group ||= ::Codeowners::Checker::Group.parse(list) end |
#missing_assignments ⇒ Array
Public: Return a list of files from the repository that do not have an owner assigned
98 99 100 |
# File 'lib/codeowner_validator/code_owners.rb', line 98 def missing_assignments @missing_assignments ||= included_files.reject(&method(:defined_owner?)) end |
#pattern_has_files(pattern) ⇒ Hash
Public: Returns a [Hash] of key/value pairs of relative file name to git details for a supplied pattern
91 92 93 |
# File 'lib/codeowner_validator/code_owners.rb', line 91 def pattern_has_files(pattern) git.ls_files(pattern.gsub(/^\//, '')).reject(&whitelist).any? end |
#patterns_by_owner ⇒ Hash
Public: Returns the patterns utilized with an array association to those patterns
Example Response:
"@orion-delivery/delivery-team": [
"*"
],
"@orion-delivery/orion-delivery-ets": [
"config/domains/production/**/*",
"config/domains/sandbox/**/*"
],
"@orion-delivery/orion-shells": [
"config/feature_definitions/authn-android-enable_biometric_unlock.yml",
"config/domains/cert-int/IONServer_CERTIFICATION.yml"
]
179 180 181 182 183 184 185 186 |
# File 'lib/codeowner_validator/code_owners.rb', line 179 def patterns_by_owner @patterns_by_owner ||= main_group.each_with_object(hash_of_arrays) do |line, patterns_by_owner| next unless line.pattern? line.owners.each { |owner| patterns_by_owner[owner] << line.pattern.gsub(/^\//, '') } end end |
#unrecognized_assignments ⇒ Array
Public: Return a list of files from the codowners file that are missing the owner assignment
105 106 107 108 109 |
# File 'lib/codeowner_validator/code_owners.rb', line 105 def unrecognized_assignments list.select do |line| line.is_a?(Codeowners::Checker::Group::UnrecognizedLine) end end |
#useless_pattern ⇒ Array
Public: Returns an [Array] of patterns that have no files associated to them
61 62 63 64 65 66 |
# File 'lib/codeowner_validator/code_owners.rb', line 61 def useless_pattern @useless_pattern ||= list.select do |line| line.pattern? && !pattern_has_files(line.pattern) end end |
#whitelisted?(file) ⇒ true|false
Public: Returns <true> if the provided file is deemed whitelisted per the configuration; otherwise, <false>
241 242 243 |
# File 'lib/codeowner_validator/code_owners.rb', line 241 def whitelisted?(file) whitelist.whitelisted?(file) end |