Skip to content

Commit ec95ffb

Browse files
committed
providng a for-file verbose option
1 parent 7e6d720 commit ec95ffb

File tree

7 files changed

+100
-12
lines changed

7 files changed

+100
-12
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/code_ownership/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rb-sys = { version = "0.9.111", features = [
1717
magnus = { version = "0.7.1" }
1818
serde = { version = "1.0.219", features = ["derive"] }
1919
serde_magnus = "0.9.0"
20-
codeowners = { git = "https://github.com/rubyatscale/codeowners-rs.git", tag = "v0.2.7" }
20+
codeowners = { git = "https://github.com/rubyatscale/codeowners-rs.git", tag = "v0.2.14" }
2121

2222
[dev-dependencies]
2323
rb-sys = { version = "0.9.117", features = [

ext/code_ownership/src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde_magnus::serialize;
99
pub struct Team {
1010
pub team_name: String,
1111
pub team_config_yml: String,
12+
pub reasons: Vec<String>,
1213
}
1314

1415
fn for_team(team_name: String) -> Result<Value, Error> {
@@ -20,16 +21,24 @@ fn for_team(team_name: String) -> Result<Value, Error> {
2021
fn for_file(file_path: String) -> Result<Option<Value>, Error> {
2122
let run_config = build_run_config();
2223

23-
match runner::team_for_file(&run_config, &file_path) {
24-
Ok(Some(team_rs)) => {
24+
match runner::file_owner_for_file(&run_config, &file_path) {
25+
Ok(owner) => {
26+
if let Some(owner) = owner {
2527
let team = Team {
26-
team_name: team_rs.name,
27-
team_config_yml: team_rs.path.to_string_lossy().to_string(),
28+
team_name: owner.team.name,
29+
team_config_yml: owner.team_config_file_path.to_string(),
30+
reasons: owner
31+
.sources
32+
.iter()
33+
.map(|source| source.to_string())
34+
.collect(),
2835
};
2936
let serialized: Value = serialize(&team)?;
3037
Ok(Some(serialized))
38+
} else {
39+
Ok(None)
40+
}
3141
}
32-
Ok(None) => Ok(None),
3342
Err(e) => Err(Error::new(
3443
magnus::exception::runtime_error(),
3544
e.to_string(),

lib/code_ownership.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def for_file(file)
4444
Private::TeamFinder.for_file(file)
4545
end
4646

47+
sig { params(file: String).returns(T.nilable(T::Hash[Symbol, String])) }
48+
def for_file_verbose(file)
49+
::RustCodeOwners.for_file(file)
50+
end
51+
4752
sig { params(team: T.any(CodeTeams::Team, String)).returns(T::Array[String]) }
4853
def for_team(team)
4954
team = T.must(CodeTeams.find(team)) if team.is_a?(String)

lib/code_ownership/cli.rb

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def self.for_file(argv)
9595
options[:json] = true
9696
end
9797

98+
opts.on('--verbose', 'Output verbose information') do
99+
options[:verbose] = true
100+
end
101+
98102
opts.on('--help', 'Shows this prompt') do
99103
puts opts
100104
exit
@@ -107,18 +111,26 @@ def self.for_file(argv)
107111
raise "Please pass in one file. Use `#{EXECUTABLE} for_file --help` for more info"
108112
end
109113

110-
team = CodeOwnership.for_file(files.first)
114+
if options[:verbose]
115+
for_file_verbose(file: files.first, json: options[:json])
116+
else
117+
for_file_terse(file: files.first, json: options[:json])
118+
end
119+
end
120+
121+
def self.for_file_terse(file:, json:)
122+
team = CodeOwnership.for_file(file)
111123

112124
team_name = team&.name || 'Unowned'
113125
team_yml = team&.config_yml || 'Unowned'
114126

115-
if options[:json]
116-
json = {
127+
if json
128+
json_output = {
117129
team_name: team_name,
118130
team_yml: team_yml
119131
}
120132

121-
puts json.to_json
133+
puts json_output.to_json
122134
else
123135
puts <<~MSG
124136
Team: #{team_name}
@@ -127,6 +139,28 @@ def self.for_file(argv)
127139
end
128140
end
129141

142+
def self.for_file_verbose(file:, json:)
143+
verbose = CodeOwnership.for_file_verbose(file) || {team_name: 'Unowned', team_config_yml: 'Unowned', reasons: []}
144+
145+
if json
146+
json = {
147+
team_name: verbose[:team_name],
148+
team_yml: verbose[:team_config_yml],
149+
reasons: verbose[:reasons]
150+
}
151+
152+
puts json.to_json
153+
else
154+
messages = ["Team: #{verbose[:team_name]}", "Team YML: #{verbose[:team_config_yml]}"]
155+
if verbose[:reasons].any?
156+
messages << "Reasons:\n- #{verbose[:reasons].join("\n-")}"
157+
end
158+
messages.last << "\n"
159+
160+
puts messages.join("\n")
161+
end
162+
end
163+
130164
def self.for_team(argv)
131165
parser = OptionParser.new do |opts|
132166
opts.banner = "Usage: #{EXECUTABLE} for_team 'Team Name'"
58.4 KB
Binary file not shown.

spec/lib/code_ownership/cli_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ def initialize
176176
end
177177
end
178178

179+
context 'when run with --verbose' do
180+
let(:argv) { ['for_file', 'app/services/my_file.rb', '--verbose'] }
181+
182+
it 'outputs the team info in human readable format' do
183+
expect(CodeOwnership::Cli).to receive(:puts).with(<<~MSG)
184+
Team: My Team
185+
Team YML: config/teams/my_team.yml
186+
Reasons:
187+
- Owner specified in Team YML as an owned_glob `app/**/*.rb`
188+
MSG
189+
subject
190+
end
191+
end
192+
179193
context 'when run with no files' do
180194
let(:argv) { ['for_file'] }
181195

@@ -205,6 +219,19 @@ def initialize
205219
expect(CodeOwnership::Cli).to receive(:puts).with(json.to_json)
206220
subject
207221
end
222+
223+
context 'when run with multiple files' do
224+
let(:argv) { ['for_file', '--json', '--verbose', 'app/services/my_file.rb'] }
225+
it 'outputs JSONified information to the console' do
226+
json = {
227+
team_name: 'My Team',
228+
team_yml: 'config/teams/my_team.yml',
229+
reasons: ['Owner specified in Team YML as an owned_glob `app/**/*.rb`']
230+
}
231+
expect(CodeOwnership::Cli).to receive(:puts).with(json.to_json)
232+
subject
233+
end
234+
end
208235
end
209236

210237
context 'when run with no files' do
@@ -235,6 +262,19 @@ def initialize
235262
MSG
236263
subject
237264
end
265+
266+
context 'when run with --verbose' do
267+
let(:argv) { ['for_file', 'app/services/unowned.rb', '--verbose'] }
268+
269+
it 'prints Unowned' do
270+
allow(CodeOwnership).to receive(:for_file_verbose).and_return(nil)
271+
expect(CodeOwnership::Cli).to receive(:puts).with(<<~MSG)
272+
Team: Unowned
273+
Team YML: Unowned
274+
MSG
275+
subject
276+
end
277+
end
238278
end
239279

240280
context 'with --help' do

0 commit comments

Comments
 (0)