Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 6 additions & 59 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/code_ownership/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rb-sys = { version = "0.9.111", features = [
magnus = { version = "0.8" }
serde = { version = "1.0.219", features = ["derive"] }
serde_magnus = "0.10"
codeowners = { git = "https://github.com/rubyatscale/codeowners-rs.git", tag = "v0.3.3" }
codeowners = { git = "https://github.com/rubyatscale/codeowners-rs.git", tag = "v0.5.0" }

[dev-dependencies]
rb-sys = { version = "0.9.117", features = [
Expand Down
14 changes: 13 additions & 1 deletion ext/code_ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ fn version() -> String {
runner::version()
}

fn clear_team_cache() {
runner::clear_team_cache();
}

fn validate(ruby: &Ruby, files: Option<Vec<String>>) -> Result<Value, Error> {
let run_config = build_run_config();
let files_vec = files.unwrap_or_default();
Expand All @@ -90,9 +94,16 @@ fn generate_and_validate(ruby: &Ruby, files: Option<Vec<String>>, skip_stage: bo

fn validate_result(ruby: &Ruby, run_result: &runner::RunResult) -> Result<Value, Error> {
if !run_result.validation_errors.is_empty() {
// codeowners-rs reports the stale-CODEOWNERS diff via info_messages; the raise is our only channel for it.
let messages: Vec<&str> = run_result
.validation_errors
.iter()
.chain(&run_result.info_messages)
.map(String::as_str)
.collect();
Err(Error::new(
ruby.exception_runtime_error(),
run_result.validation_errors.join("\n"),
messages.join("\n"),
))
} else if !run_result.io_errors.is_empty() {
Err(Error::new(
Expand Down Expand Up @@ -129,6 +140,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
module.define_singleton_method("for_team", function!(for_team, 1))?;
module.define_singleton_method("version", function!(version, 0))?;
module.define_singleton_method("teams_for_files", function!(teams_for_files, 1))?;
module.define_singleton_method("clear_team_cache", function!(clear_team_cache, 0))?;

Ok(())
}
3 changes: 3 additions & 0 deletions lib/code_ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,13 @@ def self.for_package(package)
# 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.
# It also clears the team files that codeowners-rs caches for `for_file(..., from_codeowners: false)`,
# so call it after adding or editing team files in a long-lived process.
sig { void }
def self.bust_caches!
Private::FilePathTeamCache.bust_cache!
Private::FilePathFinder.instance_variable_set(:@pwd, nil)
Private::FilePathFinder.instance_variable_set(:@pwd_prefix, nil)
::RustCodeOwners.clear_team_cache
end
end
Binary file modified lib/code_ownership/code_ownership.bundle
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/code_ownership/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# frozen_string_literal: true

module CodeOwnership
VERSION = '2.1.4'
VERSION = '2.2.0'
end
6 changes: 6 additions & 0 deletions sorbet/rbi/manual.rbi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# typed: false

class Hash
def to_json(*_args); end
end
Expand All @@ -21,5 +23,9 @@ module RustCodeOwners

def teams_for_files(files)
end

sig { void }
def clear_team_cache
end
end
end
67 changes: 67 additions & 0 deletions spec/lib/code_ownership_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@
end
end

describe '.for_file with from_codeowners: false and package ownership' do
subject { CodeOwnership.for_file(file_path, from_codeowners: false) }

let(:file_path) { 'packs/outer/inner/file.rb' }

before do
create_non_empty_application
write_file('packs/outer/package.yml', "owner: Foo\n")
write_file(file_path)
end

context 'when the package sets metadata.owner' do
before { write_file('packs/outer/inner/package.yml', { 'metadata' => { 'owner' => 'Bar' } }.to_yaml) }

it 'returns the metadata owner' do
expect(subject).to eq CodeTeams.find('Bar')
end
end

context 'when the package sets conflicting owners' do
before { write_file('packs/outer/inner/package.yml', { 'owner' => 'Foo', 'metadata' => { 'owner' => 'Bar' } }.to_yaml) }

it 'does not fall through to the enclosing package' do
expect(subject).to be_nil
end
end
end

describe '.for_class' do
subject { described_class.for_class(klass) }

Expand Down Expand Up @@ -578,6 +606,45 @@
expect(error.message).not_to include('`codeowners generate`')
end
end

it 'includes the required CODEOWNERS changes after the headline' do
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error(RuntimeError) do |error|
expect(error.message).to match(/CODEOWNERS out of date.*The following changes are required \(- current, \+ expected\):/m)
expect(error.message).to include("\n+/packs/my_pack/new_file.rb @MyOrg/bar-team")
end
end

it 'regenerates the CODEOWNERS file when autocorrecting' do
expect { CodeOwnership.validate!(stage_changes: false) }.not_to raise_error
expect(codeowners_path.read).to include('/packs/my_pack/new_file.rb @MyOrg/bar-team')
expect { CodeOwnership.validate!(autocorrect: false) }.not_to raise_error
end
end
end

describe '.bust_caches!' do
let(:file_path) { 'app/services/thing.rb' }

def write_team(name, owned_globs: [])
config = { 'name' => name, 'github' => { 'team' => "@MyOrg/#{name.downcase}-team" }, 'owned_globs' => owned_globs }
write_file("config/teams/#{name.downcase}.yml", config.to_yaml)
end

before do
write_configuration
write_file(file_path)
write_team('Foo', owned_globs: ['app/services/**'])
write_team('Bar')
end

it 'makes a team file change visible to for_file within one process' do
expect(CodeOwnership.for_file(file_path, from_codeowners: false)).to eq CodeTeams.find('Foo')

write_team('Foo')
write_team('Bar', owned_globs: ['app/services/**'])
CodeOwnership.bust_caches!

expect(CodeOwnership.for_file(file_path, from_codeowners: false)).to eq CodeTeams.find('Bar')
end
end
end
Loading