|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# typed: true |
| 4 | + |
| 5 | +module CodeOwnership |
| 6 | + module Private |
| 7 | + module OwnershipMappers |
| 8 | + class DirectoryOwnership |
| 9 | + extend T::Sig |
| 10 | + include Mapper |
| 11 | + |
| 12 | + CODEOWNERS_DIRECTORY_FILE_NAME = '.codeowner' |
| 13 | + |
| 14 | + @@directory_cache = T.let({}, T::Hash[String, T.nilable(CodeTeams::Team)]) # rubocop:disable Style/ClassVars |
| 15 | + |
| 16 | + sig do |
| 17 | + override.params(file: String). |
| 18 | + returns(T.nilable(::CodeTeams::Team)) |
| 19 | + end |
| 20 | + def map_file_to_owner(file) |
| 21 | + map_file_to_relevant_owner(file) |
| 22 | + end |
| 23 | + |
| 24 | + sig do |
| 25 | + override.params(cache: GlobsToOwningTeamMap, files: T::Array[String]).returns(GlobsToOwningTeamMap) |
| 26 | + end |
| 27 | + def update_cache(cache, files) |
| 28 | + globs_to_owner(files) |
| 29 | + end |
| 30 | + |
| 31 | + # |
| 32 | + # Directory ownership ignores the passed in files when generating code owners lines. |
| 33 | + # This is because Directory ownership knows that the fastest way to find code owners for directory based ownership |
| 34 | + # is to simply iterate over the directories and grab the owner, rather than iterating over each file just to get what directory it is in |
| 35 | + # In theory this means that we may generate code owners lines that cover files that are not in the passed in argument, |
| 36 | + # but in practice this is not of consequence because in reality we never really want to generate code owners for only a |
| 37 | + # subset of files, but rather we want code ownership for all files. |
| 38 | + # |
| 39 | + sig do |
| 40 | + override.params(files: T::Array[String]). |
| 41 | + returns(T::Hash[String, ::CodeTeams::Team]) |
| 42 | + end |
| 43 | + def globs_to_owner(files) |
| 44 | + # The T.unsafe is because the upstream RBI is wrong for Pathname.glob |
| 45 | + T |
| 46 | + .unsafe(Pathname) |
| 47 | + .glob(File.join('**/', CODEOWNERS_DIRECTORY_FILE_NAME)) |
| 48 | + .map(&:cleanpath) |
| 49 | + .each_with_object({}) do |pathname, res| |
| 50 | + owner = owner_for_codeowners_file(pathname) |
| 51 | + res[pathname.dirname.cleanpath.join('**/**').to_s] = owner |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + sig { override.returns(String) } |
| 56 | + def description |
| 57 | + 'Owner in .codeowner' |
| 58 | + end |
| 59 | + |
| 60 | + sig { override.void } |
| 61 | + def bust_caches! |
| 62 | + @@directory_cache = {} # rubocop:disable Style/ClassVars |
| 63 | + end |
| 64 | + |
| 65 | + private |
| 66 | + |
| 67 | + sig { params(codeowners_file: Pathname).returns(CodeTeams::Team) } |
| 68 | + def owner_for_codeowners_file(codeowners_file) |
| 69 | + raw_owner_value = File.foreach(codeowners_file).first.strip |
| 70 | + |
| 71 | + Private.find_team!( |
| 72 | + raw_owner_value, |
| 73 | + codeowners_file.to_s |
| 74 | + ) |
| 75 | + end |
| 76 | + |
| 77 | + # takes a file and finds the relevant `.codeowner` file by walking up the directory |
| 78 | + # structure. Example, given `a/b/c.rb`, this looks for `a/b/.codeowner`, `a/.codeowner`, |
| 79 | + # and `.codeowner` in that order, stopping at the first file to actually exist. |
| 80 | + # We do additional caching so that we don't have to check for file existence every time |
| 81 | + sig { params(file: String).returns(T.nilable(CodeTeams::Team)) } |
| 82 | + def map_file_to_relevant_owner(file) |
| 83 | + file_path = Pathname.new(file) |
| 84 | + path_components = file_path.each_filename.to_a.map { |path| Pathname.new(path) } |
| 85 | + |
| 86 | + (path_components.length - 1).downto(0).each do |i| |
| 87 | + potential_relative_path_name = T.must(path_components[0...i]).reduce(Pathname.new('')) { |built_path, path| built_path.join(path) } |
| 88 | + potential_codeowners_file = potential_relative_path_name.join(CODEOWNERS_DIRECTORY_FILE_NAME) |
| 89 | + |
| 90 | + potential_codeowners_file_name = potential_codeowners_file.to_s |
| 91 | + |
| 92 | + team = nil |
| 93 | + if @@directory_cache.key?(potential_codeowners_file_name) |
| 94 | + team = @@directory_cache[potential_codeowners_file_name] |
| 95 | + elsif potential_codeowners_file.exist? |
| 96 | + team = owner_for_codeowners_file(potential_codeowners_file) |
| 97 | + |
| 98 | + @@directory_cache[potential_codeowners_file_name] = team |
| 99 | + else |
| 100 | + @@directory_cache[potential_codeowners_file_name] = nil |
| 101 | + end |
| 102 | + |
| 103 | + return team unless team.nil? |
| 104 | + end |
| 105 | + |
| 106 | + nil |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments