-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcode_ownership.rb
More file actions
113 lines (95 loc) · 3.6 KB
/
code_ownership.rb
File metadata and controls
113 lines (95 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# frozen_string_literal: true
# typed: strict
require 'set'
require 'code_teams'
require 'sorbet-runtime'
require 'json'
require 'packs-specification'
require 'code_ownership/version'
require 'code_ownership/private/file_path_finder'
require 'code_ownership/private/file_path_team_cache'
require 'code_ownership/private/team_finder'
require 'code_ownership/private/for_file_output_builder'
require 'code_ownership/cli'
begin
RUBY_VERSION =~ /(\d+\.\d+)/
require "code_ownership/#{Regexp.last_match(1)}/code_ownership"
rescue LoadError
require 'code_ownership/code_ownership'
end
if defined?(Packwerk)
require 'code_ownership/private/permit_pack_owner_top_level_key'
end
module CodeOwnership
module_function
extend T::Sig
extend T::Helpers
requires_ancestor { Kernel }
GlobsToOwningTeamMap = T.type_alias { T::Hash[String, CodeTeams::Team] }
sig { returns(T::Array[String]) }
def version
["code_ownership version: #{VERSION}",
"codeowners-rs version: #{::RustCodeOwners.version}"]
end
sig { params(file: String).returns(T.nilable(CodeTeams::Team)) }
def for_file(file)
Private::TeamFinder.for_file(file)
end
sig { params(file: String).returns(T.nilable(T::Hash[Symbol, String])) }
def for_file_verbose(file)
::RustCodeOwners.for_file(file)
end
sig { params(team: T.any(CodeTeams::Team, String)).returns(T::Array[String]) }
def for_team(team)
team = T.must(CodeTeams.find(team)) if team.is_a?(String)
::RustCodeOwners.for_team(team.name)
end
class InvalidCodeOwnershipConfigurationError < StandardError
end
sig do
params(
autocorrect: T::Boolean,
stage_changes: T::Boolean,
files: T.nilable(T::Array[String])
).void
end
def validate!(
autocorrect: true,
stage_changes: true,
files: nil
)
if autocorrect
::RustCodeOwners.generate_and_validate(!stage_changes)
else
::RustCodeOwners.validate
end
end
# Given a backtrace from either `Exception#backtrace` or `caller`, find the
# first line that corresponds to a file with assigned ownership
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::CodeTeams::Team]).returns(T.nilable(::CodeTeams::Team)) }
def for_backtrace(backtrace, excluded_teams: [])
Private::TeamFinder.for_backtrace(backtrace, excluded_teams: excluded_teams)
end
# Given a backtrace from either `Exception#backtrace` or `caller`, find the
# first owned file in it, useful for figuring out which file is being blamed.
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::CodeTeams::Team]).returns(T.nilable([::CodeTeams::Team, String])) }
def first_owned_file_for_backtrace(backtrace, excluded_teams: [])
Private::TeamFinder.first_owned_file_for_backtrace(backtrace, excluded_teams: excluded_teams)
end
sig { params(klass: T.nilable(T.any(T::Class[T.anything], Module))).returns(T.nilable(::CodeTeams::Team)) }
def for_class(klass)
Private::TeamFinder.for_class(klass)
end
sig { params(package: Packs::Pack).returns(T.nilable(::CodeTeams::Team)) }
def for_package(package)
Private::TeamFinder.for_package(package)
end
# Generally, you should not ever need to do this, because once your ruby process loads, cached content should not change.
# Namely, the set of files, packages, and directories which are tracked for ownership should not change.
# The primary reason this is helpful is for clients of CodeOwnership who want to test their code, and each test context
# has different ownership and tracked files.
sig { void }
def self.bust_caches!
Private::FilePathTeamCache.bust_cache!
end
end