Class: Codeowners::DefinitionsFile

Inherits:
Object
  • Object
show all
Defined in:
lib/codeowners/definitions_file.rb

Overview

Parses Cleo Codeowners YAML files

Defined Under Namespace

Classes: Match

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_glob = '.cleo/codeowners/**/*.y*ml') ⇒ DefinitionsFile

Returns a new instance of DefinitionsFile.

Parameters:

  • definition_glob (String) (defaults to: '.cleo/codeowners/**/*.y*ml')

    String containing the glob pattern for the Cleo codeowner YAML files



12
13
14
15
16
# File 'lib/codeowners/definitions_file.rb', line 12

def initialize(definition_glob = '.cleo/codeowners/**/*.y*ml')
  @definition = Dir[definition_glob].inject({}) do |yaml, filepath|
    yaml.deep_merge(YAML.load_file(filepath))
  end
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



18
19
20
# File 'lib/codeowners/definitions_file.rb', line 18

def definition
  @definition
end

Instance Method Details

#feature_paths(feature:, directory: nil) ⇒ Array, <Pathname>

Finds files defined under a feature, expands all globs, and returns all of them

Returns:

  • (Array, <Pathname>)


34
35
36
37
# File 'lib/codeowners/definitions_file.rb', line 34

def feature_paths(feature:, directory: nil)
  relative_globs = Array(file_config[feature]).map { |path| "./#{path}" }
  resolve_local_paths(relative_globs:, directory: directory || Pathname.pwd)
end

#features_configObject



20
21
22
# File 'lib/codeowners/definitions_file.rb', line 20

def features_config
  definition['features'] || {}
end

#file_configObject



28
29
30
# File 'lib/codeowners/definitions_file.rb', line 28

def file_config
  definition['files'] || {}
end

#find_feature_for_file(path:) ⇒ nil, String

Finds a feature the file belongs to

Returns:

  • (nil, String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/codeowners/definitions_file.rb', line 41

def find_feature_for_file(path:)
  return nil unless path

  target_path = File.join('.', path)
  file_config.each do |feature, paths|
    next unless paths

    paths.each do |feature_path|
      path_pattern = File.join('.', feature_path)
      path_pattern = "#{path_pattern}/*" if path_pattern.end_with?('/')
      path_pattern.gsub!('//', '/')

      return feature if File.fnmatch(path_pattern, target_path)
    end
  end

  nil
end

#find_features_for_file(path:) ⇒ Object

Finds a feature the file belongs to

Returns:

  • Array



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/codeowners/definitions_file.rb', line 64

def find_features_for_file(path:)
  return [] unless path

  target_path = File.join('.', path)
  file_config.filter_map do |feature, paths|
    next unless paths

    paths.filter_map do |feature_path|
      path_pattern = File.join('.', feature_path)
      path_pattern = "#{path_pattern}/*" if path_pattern.end_with?('/')
      path_pattern.gsub!('//', '/')

      Match.new(feature, path_pattern) if File.fnmatch(path_pattern, target_path)
    end
  end.flatten.uniq
end

#owners_configObject



24
25
26
# File 'lib/codeowners/definitions_file.rb', line 24

def owners_config
  definition['owners'] || {}
end