Module: CodeOwnership::Private::TeamFinder

Extended by:
T::Helpers, T::Sig
Defined in:
lib/code_ownership/private/team_finder.rb

Class Method Summary collapse

Class Method Details

.first_owned_file_for_backtrace(backtrace, excluded_teams: []) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/code_ownership/private/team_finder.rb', line 82

def first_owned_file_for_backtrace(backtrace, excluded_teams: [])
  FilePathFinder.from_backtrace(backtrace).each do |file|
    team = for_file(file)
    if team && !excluded_teams.include?(team)
      return [team, file]
    end
  end

  nil
end

.for_backtrace(backtrace, excluded_teams: []) ⇒ Object



77
78
79
# File 'lib/code_ownership/private/team_finder.rb', line 77

def for_backtrace(backtrace, excluded_teams: [])
  first_owned_file_for_backtrace(backtrace, excluded_teams: excluded_teams)&.first
end

.for_class(klass) ⇒ Object



61
62
63
64
65
66
# File 'lib/code_ownership/private/team_finder.rb', line 61

def for_class(klass)
  file_path = FilePathFinder.path_from_klass(klass)
  return nil if file_path.nil?

  for_file(file_path)
end

.for_file(file_path, allow_raise: false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/code_ownership/private/team_finder.rb', line 16

def for_file(file_path, allow_raise: false)
  return nil if file_path.start_with?('./')

  return FilePathTeamCache.get(file_path) if FilePathTeamCache.cached?(file_path)

  result = T.let(RustCodeOwners.for_file(file_path), T.nilable(T::Hash[Symbol, String]))
  return if result.nil?

  if result[:team_name].nil?
    FilePathTeamCache.set(file_path, nil)
  else
    FilePathTeamCache.set(file_path, T.let(find_team!(T.must(result[:team_name]), allow_raise: allow_raise), T.nilable(CodeTeams::Team)))
  end

  FilePathTeamCache.get(file_path)
end

.for_package(package) ⇒ Object



69
70
71
72
73
74
# File 'lib/code_ownership/private/team_finder.rb', line 69

def for_package(package)
  owner_name = package.raw_hash['owner'] || package.['owner']
  return nil if owner_name.nil?

  find_team!(owner_name, allow_raise: true)
end

.teams_for_files(files, allow_raise: false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/code_ownership/private/team_finder.rb', line 34

def teams_for_files(files, allow_raise: false)
  result = {}

  # Collect cached results and identify non-cached files
  not_cached_files = []
  files.each do |file_path|
    if FilePathTeamCache.cached?(file_path)
      result[file_path] = FilePathTeamCache.get(file_path)
    else
      not_cached_files << file_path
    end
  end

  return result if not_cached_files.empty?

  # Process non-cached files
  ::RustCodeOwners.teams_for_files(not_cached_files).each do |path_team|
    file_path, team = path_team
    found_team = team ? find_team!(team[:team_name], allow_raise: allow_raise) : nil
    FilePathTeamCache.set(file_path, found_team)
    result[file_path] = found_team
  end

  result
end