Skip to content

Commit d4eccb5

Browse files
Add rubocop (#102)
* Add rubocop and all the things that could be autocorrected * fix poorly named vars praise our robot overlords rubocop happy Add version Remove empty block * Use files_by_mappers_via_expanded_cache in more places Co-authored-by: Ashley Willard <[email protected]>
1 parent fd21e7d commit d4eccb5

31 files changed

+373
-266
lines changed

.rubocop.yml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# The behavior of RuboCop can be controlled via the .rubocop.yml
2+
# configuration file. It makes it possible to enable/disable
3+
# certain cops (checks) and to alter their behavior if they accept
4+
# any parameters. The file can be placed either in your home
5+
# directory or in some project directory.
6+
#
7+
# RuboCop will start looking for the configuration file in the directory
8+
# where the inspected file is and continue its way up to the root directory.
9+
#
10+
# See https://docs.rubocop.org/rubocop/configuration
11+
AllCops:
12+
NewCops: enable
13+
Exclude:
14+
- vendor/bundle/**/**
15+
TargetRubyVersion: 2.6
16+
17+
Metrics/ParameterLists:
18+
Enabled: false
19+
20+
# This cop is annoying with typed configuration
21+
Style/TrivialAccessors:
22+
Enabled: false
23+
24+
# This rubocop is annoying when we use interfaces a lot
25+
Lint/UnusedMethodArgument:
26+
Enabled: false
27+
28+
Gemspec/RequireMFA:
29+
Enabled: false
30+
31+
Lint/DuplicateBranch:
32+
Enabled: false
33+
34+
# If is sometimes easier to think about than unless sometimes
35+
Style/NegatedIf:
36+
Enabled: false
37+
38+
# Disabling for now until it's clearer why we want this
39+
Style/FrozenStringLiteralComment:
40+
Enabled: false
41+
42+
# It's nice to be able to read the condition first before reading the code within the condition
43+
Style/GuardClause:
44+
Enabled: false
45+
46+
#
47+
# Leaving length metrics to human judgment for now
48+
#
49+
Metrics/ModuleLength:
50+
Enabled: false
51+
52+
Layout/LineLength:
53+
Enabled: false
54+
55+
Metrics/BlockLength:
56+
Enabled: false
57+
58+
Metrics/MethodLength:
59+
Enabled: false
60+
61+
Metrics/AbcSize:
62+
Enabled: false
63+
64+
Metrics/ClassLength:
65+
Enabled: false
66+
67+
# This doesn't feel useful
68+
Metrics/CyclomaticComplexity:
69+
Enabled: false
70+
71+
# This doesn't feel useful
72+
Metrics/PerceivedComplexity:
73+
Enabled: false
74+
75+
# It's nice to be able to read the condition first before reading the code within the condition
76+
Style/IfUnlessModifier:
77+
Enabled: false
78+
79+
# This leads to code that is not very readable at times (very long lines)
80+
Style/ConditionalAssignment:
81+
Enabled: false
82+
83+
# For now, we prefer to lean on clean method signatures as documentation. We may change this later.
84+
Style/Documentation:
85+
Enabled: false
86+
87+
# Sometimes we leave comments in empty else statements intentionally
88+
Style/EmptyElse:
89+
Enabled: false
90+
91+
# Sometimes we want to more explicitly list out a condition
92+
Style/RedundantCondition:
93+
Enabled: false
94+
95+
# This leads to code that is not very readable at times (very long lines)
96+
Layout/MultilineMethodCallIndentation:
97+
Enabled: false
98+
99+
# Blocks across lines are okay sometimes
100+
Style/BlockDelimiters:
101+
Enabled: false
102+
103+
# Sometimes we like methods like `get_packages`
104+
Naming/AccessorMethodName:
105+
Enabled: false
106+
107+
# This leads to code that is not very readable at times (very long lines)
108+
Layout/FirstArgumentIndentation:
109+
Enabled: false
110+
111+
# This leads to code that is not very readable at times (very long lines)
112+
Layout/ArgumentAlignment:
113+
Enabled: false
114+
115+
Style/AccessorGrouping:
116+
Enabled: false
117+
118+
Style/HashSyntax:
119+
Enabled: false
120+
121+
Gemspec/DevelopmentDependencies:
122+
Enabled: true
123+
EnforcedStyle: gemspec

code_ownership.gemspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Gem::Specification.new do |spec|
77
spec.description = 'A gem to help engineering teams declare ownership of code'
88
spec.homepage = 'https://github.com/rubyatscale/code_ownership'
99
spec.license = 'MIT'
10+
spec.required_ruby_version = '>= 2.6'
1011

1112
if spec.respond_to?(:metadata)
1213
spec.metadata['homepage_uri'] = spec.homepage
@@ -31,10 +32,11 @@ Gem::Specification.new do |spec|
3132
spec.add_dependency 'sorbet-runtime', '>= 0.5.11249'
3233

3334
spec.add_development_dependency 'debug'
35+
spec.add_development_dependency 'packwerk'
36+
spec.add_development_dependency 'railties'
3437
spec.add_development_dependency 'rake'
3538
spec.add_development_dependency 'rspec', '~> 3.0'
39+
spec.add_development_dependency 'rubocop'
3640
spec.add_development_dependency 'sorbet'
3741
spec.add_development_dependency 'tapioca'
38-
spec.add_development_dependency 'packwerk'
39-
spec.add_development_dependency 'railties'
4042
end

lib/code_ownership.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
end
1919

2020
module CodeOwnership
21-
extend self
21+
module_function
22+
2223
extend T::Sig
2324
extend T::Helpers
2425

@@ -57,6 +58,7 @@ def for_team(team)
5758
ownership_for_mapper = []
5859
glob_to_owning_team_map.each do |glob, owning_team|
5960
next if owning_team != team
61+
6062
ownership_for_mapper << "- #{glob}"
6163
end
6264

@@ -66,7 +68,7 @@ def for_team(team)
6668
ownership_information += ownership_for_mapper.sort
6769
end
6870

69-
ownership_information << ""
71+
ownership_information << ''
7072
end
7173

7274
ownership_information.join("\n")
@@ -84,7 +86,7 @@ def self.remove_file_annotation!(filename)
8486
params(
8587
autocorrect: T::Boolean,
8688
stage_changes: T::Boolean,
87-
files: T.nilable(T::Array[String]),
89+
files: T.nilable(T::Array[String])
8890
).void
8991
end
9092
def validate!(
@@ -95,10 +97,10 @@ def validate!(
9597
Private.load_configuration!
9698

9799
tracked_file_subset = if files
98-
files.select{|f| Private.file_tracked?(f)}
99-
else
100-
Private.tracked_files
101-
end
100+
files.select { |f| Private.file_tracked?(f) }
101+
else
102+
Private.tracked_files
103+
end
102104

103105
Private.validate!(files: tracked_file_subset, autocorrect: autocorrect, stage_changes: stage_changes)
104106
end
@@ -150,7 +152,7 @@ def backtrace_with_ownership(backtrace)
150152

151153
[
152154
CodeOwnership.for_file(file),
153-
file,
155+
file
154156
]
155157
end
156158
end
@@ -161,15 +163,15 @@ def for_class(klass)
161163
@memoized_values ||= T.let(@memoized_values, T.nilable(T::Hash[String, T.nilable(::CodeTeams::Team)]))
162164
@memoized_values ||= {}
163165
# We use key because the memoized value could be `nil`
164-
if !@memoized_values.key?(klass.to_s)
166+
if @memoized_values.key?(klass.to_s)
167+
@memoized_values[klass.to_s]
168+
else
165169
path = Private.path_from_klass(klass)
166170
return nil if path.nil?
167171

168172
value_to_memoize = for_file(path)
169173
@memoized_values[klass.to_s] = value_to_memoize
170174
value_to_memoize
171-
else
172-
@memoized_values[klass.to_s]
173175
end
174176
end
175177

lib/code_ownership/cli.rb

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def self.run!(argv)
1414
for_file(argv)
1515
elsif command == 'for_team'
1616
for_team(argv)
17-
elsif [nil, "help"].include?(command)
17+
elsif [nil, 'help'].include?(command)
1818
puts <<~USAGE
1919
Usage: bin/codeownership <subcommand>
2020
@@ -27,7 +27,6 @@ def self.run!(argv)
2727
else
2828
puts "'#{command}' is not a code_ownership command. See `bin/codeownership help`."
2929
end
30-
3130
end
3231

3332
def self.validate!(argv)
@@ -53,16 +52,16 @@ def self.validate!(argv)
5352
exit
5453
end
5554
end
56-
args = parser.order!(argv) {}
55+
args = parser.order!(argv)
5756
parser.parse!(args)
5857

5958
files = if options[:diff]
60-
ENV.fetch('CODEOWNERS_GIT_STAGED_FILES') { `git diff --staged --name-only` }.split("\n").select do |file|
61-
File.exist?(file)
62-
end
63-
else
64-
nil
65-
end
59+
ENV.fetch('CODEOWNERS_GIT_STAGED_FILES') { `git diff --staged --name-only` }.split("\n").select do |file|
60+
File.exist?(file)
61+
end
62+
else
63+
nil
64+
end
6665

6766
CodeOwnership.validate!(
6867
files: files,
@@ -79,7 +78,7 @@ def self.for_file(argv)
7978
# Long-term, we probably want to use something like `thor` so we don't have to implement logic
8079
# like this. In the short-term, this is a simple way for us to use the built-in OptionParser
8180
# while having an ergonomic CLI.
82-
files = argv.select { |arg| !arg.start_with?('--') }
81+
files = argv.reject { |arg| arg.start_with?('--') }
8382

8483
parser = OptionParser.new do |opts|
8584
opts.banner = 'Usage: bin/codeownership for_file [options]'
@@ -93,22 +92,22 @@ def self.for_file(argv)
9392
exit
9493
end
9594
end
96-
args = parser.order!(argv) {}
95+
args = parser.order!(argv)
9796
parser.parse!(args)
9897

9998
if files.count != 1
100-
raise "Please pass in one file. Use `bin/codeownership for_file --help` for more info"
99+
raise 'Please pass in one file. Use `bin/codeownership for_file --help` for more info'
101100
end
102-
101+
103102
team = CodeOwnership.for_file(files.first)
104103

105-
team_name = team&.name || "Unowned"
106-
team_yml = team&.config_yml || "Unowned"
104+
team_name = team&.name || 'Unowned'
105+
team_yml = team&.config_yml || 'Unowned'
107106

108107
if options[:json]
109108
json = {
110109
team_name: team_name,
111-
team_yml: team_yml,
110+
team_yml: team_yml
112111
}
113112

114113
puts json.to_json
@@ -121,8 +120,6 @@ def self.for_file(argv)
121120
end
122121

123122
def self.for_team(argv)
124-
options = {}
125-
126123
parser = OptionParser.new do |opts|
127124
opts.banner = 'Usage: bin/codeownership for_team \'Team Name\''
128125

@@ -131,14 +128,14 @@ def self.for_team(argv)
131128
exit
132129
end
133130
end
134-
teams = argv.select { |arg| !arg.start_with?('--') }
135-
args = parser.order!(argv) {}
131+
teams = argv.reject { |arg| arg.start_with?('--') }
132+
args = parser.order!(argv)
136133
parser.parse!(args)
137134

138135
if teams.count != 1
139-
raise "Please pass in one team. Use `bin/codeownership for_team --help` for more info"
136+
raise 'Please pass in one team. Use `bin/codeownership for_team --help` for more info'
140137
end
141-
138+
142139
puts CodeOwnership.for_team(teams.first)
143140
end
144141

lib/code_ownership/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class Configuration < T::Struct
1717
def self.fetch
1818
config_hash = YAML.load_file('config/code_ownership.yml')
1919

20-
if config_hash.key?("require")
21-
config_hash["require"].each do |require_directive|
20+
if config_hash.key?('require')
21+
config_hash['require'].each do |require_directive|
2222
Private::ExtensionLoader.load(require_directive)
2323
end
2424
end

lib/code_ownership/mapper.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,33 @@ def all
2929
# This should be fast when run with ONE file
3030
#
3131
sig do
32-
abstract.params(file: String).
33-
returns(T.nilable(::CodeTeams::Team))
34-
end
35-
def map_file_to_owner(file)
32+
abstract.params(file: String)
33+
.returns(T.nilable(::CodeTeams::Team))
3634
end
35+
def map_file_to_owner(file); end
3736

3837
#
3938
# This should be fast when run with MANY files
4039
#
4140
sig do
42-
abstract.params(files: T::Array[String]).
43-
returns(T::Hash[String, ::CodeTeams::Team])
44-
end
45-
def globs_to_owner(files)
41+
abstract.params(files: T::Array[String])
42+
.returns(T::Hash[String, ::CodeTeams::Team])
4643
end
44+
def globs_to_owner(files); end
4745

4846
#
4947
# This should be fast when run with MANY files
5048
#
5149
sig do
5250
abstract.params(cache: GlobsToOwningTeamMap, files: T::Array[String]).returns(GlobsToOwningTeamMap)
5351
end
54-
def update_cache(cache, files)
55-
end
52+
def update_cache(cache, files); end
5653

5754
sig { abstract.returns(String) }
58-
def description
59-
end
55+
def description; end
6056

6157
sig { abstract.void }
62-
def bust_caches!
63-
end
58+
def bust_caches!; end
6459

6560
sig { returns(Private::GlobCache) }
6661
def self.to_glob_cache
@@ -72,6 +67,7 @@ def self.to_glob_cache
7267

7368
mapped_files.each do |glob, owner|
7469
next if owner.nil?
70+
7571
glob_to_owner_map_by_mapper_description.fetch(mapper.description)[glob] = owner
7672
end
7773
end

0 commit comments

Comments
 (0)