From 0ad462b073284354c7948bd87cac55fbe8c7e6c8 Mon Sep 17 00:00:00 2001 From: Jose Ibarra Date: Wed, 29 Jan 2025 10:59:36 -0700 Subject: [PATCH 1/6] Allow custom codeowners path via environment variable --- lib/code_ownership/private/codeowners_file.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/code_ownership/private/codeowners_file.rb b/lib/code_ownership/private/codeowners_file.rb index 8acd3a2..811dc67 100644 --- a/lib/code_ownership/private/codeowners_file.rb +++ b/lib/code_ownership/private/codeowners_file.rb @@ -111,7 +111,10 @@ def self.write! sig { returns(Pathname) } def self.path - Pathname.pwd.join('.github/CODEOWNERS') + Pathname.pwd.join( + ENV.fetch('CODEOWNERS_PATH', '.github'), + 'CODEOWNERS' + ) end sig { params(files: T::Array[String]).void } From 86379ffca258f2413e26e060f99646b5d23be350 Mon Sep 17 00:00:00 2001 From: Jose Ibarra Date: Wed, 29 Jan 2025 10:59:47 -0700 Subject: [PATCH 2/6] Add specs --- .../private/codeowners_file_spec.rb | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 spec/lib/code_ownership/private/codeowners_file_spec.rb diff --git a/spec/lib/code_ownership/private/codeowners_file_spec.rb b/spec/lib/code_ownership/private/codeowners_file_spec.rb new file mode 100644 index 0000000..9efb754 --- /dev/null +++ b/spec/lib/code_ownership/private/codeowners_file_spec.rb @@ -0,0 +1,36 @@ +module CodeOwnership + RSpec.describe Private::CodeownersFile do + describe '.path' do + subject { described_class.path } + + context 'when the environment variable is set' do + before do + allow(ENV).to receive(:fetch).and_call_original + allow(ENV).to receive(:fetch).with('CODEOWNERS_PATH', anything).and_return(path) + end + + context "to 'foo'" do + let(:path) { 'foo' } + + it 'uses the environment variable' do + expect(subject).to eq(Pathname.pwd.join('foo', 'CODEOWNERS')) + end + end + + context 'to empty' do + let(:path) { '' } + + it 'uses the environment variable' do + expect(subject).to eq(Pathname.pwd.join('CODEOWNERS')) + end + end + end + + context 'when the environment variable is not set' do + it 'uses the default' do + expect(subject).to eq(Pathname.pwd.join('.github', 'CODEOWNERS')) + end + end + end + end +end From b7e09608ee4a1183a4ddeac821a530cf0c3e9910 Mon Sep 17 00:00:00 2001 From: Jose Ibarra Date: Wed, 29 Jan 2025 11:02:16 -0700 Subject: [PATCH 3/6] Add documentation to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 027fedb..ca67dba 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,8 @@ bin/codeownership for_team 'My Team' > tmp/ownership_report.md A `CODEOWNERS` file defines who owns specific files or paths in a repository. When you run `bin/codeownership validate`, a `.github/CODEOWNERS` file will automatically be generated and updated. +If the `CODEOWNERS_PATH` environment variable is set, codeowners will use that path to generate the `CODEOWNERS` file. For example, `CODEOWNERS=docs` will generate `docs/CODEOWNERS`. + ## Proper Configuration & Validation CodeOwnership comes with a validation function to ensure the following things are true: From e070b4a0e51842e82fa9e50b345df129659104ad Mon Sep 17 00:00:00 2001 From: Jose Ibarra Date: Wed, 29 Jan 2025 11:03:38 -0700 Subject: [PATCH 4/6] Bump gem version --- code_ownership.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code_ownership.gemspec b/code_ownership.gemspec index e173f8c..5527e78 100644 --- a/code_ownership.gemspec +++ b/code_ownership.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |spec| spec.name = 'code_ownership' - spec.version = '1.38.2' + spec.version = '1.38.3' spec.authors = ['Gusto Engineers'] spec.email = ['dev@gusto.com'] spec.summary = 'A gem to help engineering teams declare ownership of code' From 3a0ec29cdc9faa2500a7959ad4592dadeaf0c122 Mon Sep 17 00:00:00 2001 From: Jose Ibarra Date: Wed, 29 Jan 2025 11:07:51 -0700 Subject: [PATCH 5/6] fixup! Add documentation to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ca67dba..839b6f2 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ bin/codeownership for_team 'My Team' > tmp/ownership_report.md A `CODEOWNERS` file defines who owns specific files or paths in a repository. When you run `bin/codeownership validate`, a `.github/CODEOWNERS` file will automatically be generated and updated. -If the `CODEOWNERS_PATH` environment variable is set, codeowners will use that path to generate the `CODEOWNERS` file. For example, `CODEOWNERS=docs` will generate `docs/CODEOWNERS`. +If the `CODEOWNERS_PATH` environment variable is set, codeowners will use that path to generate the `CODEOWNERS` file. For example, `CODEOWNERS_PATH=docs` will generate `docs/CODEOWNERS`. ## Proper Configuration & Validation From 834cecf916e692e7f27ff979f368622e55b418be Mon Sep 17 00:00:00 2001 From: Jose Ibarra Date: Fri, 31 Jan 2025 16:07:29 -0700 Subject: [PATCH 6/6] Move CODEOWNERS path into code_ownership.yml configuration --- README.md | 2 +- lib/code_ownership/configuration.rb | 4 +++- lib/code_ownership/private/codeowners_file.rb | 2 +- .../private/codeowners_file_spec.rb | 24 ++++++++++++------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 839b6f2..6c12a02 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ bin/codeownership for_team 'My Team' > tmp/ownership_report.md A `CODEOWNERS` file defines who owns specific files or paths in a repository. When you run `bin/codeownership validate`, a `.github/CODEOWNERS` file will automatically be generated and updated. -If the `CODEOWNERS_PATH` environment variable is set, codeowners will use that path to generate the `CODEOWNERS` file. For example, `CODEOWNERS_PATH=docs` will generate `docs/CODEOWNERS`. +If `codeowners_path` is set in `code_ownership.yml` codeowners will use that path to generate the `CODEOWNERS` file. For example, `codeowners_path: docs` will generate `docs/CODEOWNERS`. ## Proper Configuration & Validation diff --git a/lib/code_ownership/configuration.rb b/lib/code_ownership/configuration.rb index 33bef67..a93ff98 100644 --- a/lib/code_ownership/configuration.rb +++ b/lib/code_ownership/configuration.rb @@ -12,6 +12,7 @@ class Configuration < T::Struct const :skip_codeowners_validation, T::Boolean const :raw_hash, T::Hash[T.untyped, T.untyped] const :require_github_teams, T::Boolean + const :codeowners_path, String sig { returns(Configuration) } def self.fetch @@ -29,7 +30,8 @@ def self.fetch js_package_paths: js_package_paths(config_hash), skip_codeowners_validation: config_hash.fetch('skip_codeowners_validation', false), raw_hash: config_hash, - require_github_teams: config_hash.fetch('require_github_teams', false) + require_github_teams: config_hash.fetch('require_github_teams', false), + codeowners_path: config_hash.fetch('codeowners_path', '.github'), ) end diff --git a/lib/code_ownership/private/codeowners_file.rb b/lib/code_ownership/private/codeowners_file.rb index 811dc67..7b1794b 100644 --- a/lib/code_ownership/private/codeowners_file.rb +++ b/lib/code_ownership/private/codeowners_file.rb @@ -112,7 +112,7 @@ def self.write! sig { returns(Pathname) } def self.path Pathname.pwd.join( - ENV.fetch('CODEOWNERS_PATH', '.github'), + CodeOwnership.configuration.codeowners_path, 'CODEOWNERS' ) end diff --git a/spec/lib/code_ownership/private/codeowners_file_spec.rb b/spec/lib/code_ownership/private/codeowners_file_spec.rb index 9efb754..0281f2a 100644 --- a/spec/lib/code_ownership/private/codeowners_file_spec.rb +++ b/spec/lib/code_ownership/private/codeowners_file_spec.rb @@ -3,10 +3,22 @@ module CodeOwnership describe '.path' do subject { described_class.path } - context 'when the environment variable is set' do + context 'when codeowners_path is set in the configuration' do + let(:configuration) do + Configuration.new( + owned_globs: [], + unowned_globs: [], + js_package_paths: [], + unbuilt_gems_path: nil, + skip_codeowners_validation: false, + raw_hash: {}, + require_github_teams: false, + codeowners_path: path + ) + end + before do - allow(ENV).to receive(:fetch).and_call_original - allow(ENV).to receive(:fetch).with('CODEOWNERS_PATH', anything).and_return(path) + allow(CodeOwnership).to receive(:configuration).and_return(configuration) end context "to 'foo'" do @@ -25,12 +37,6 @@ module CodeOwnership end end end - - context 'when the environment variable is not set' do - it 'uses the default' do - expect(subject).to eq(Pathname.pwd.join('.github', 'CODEOWNERS')) - end - end end end end