Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment variable to set CODEOWNERS write path #114

Merged
merged 6 commits into from
Feb 3, 2025
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `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

CodeOwnership comes with a validation function to ensure the following things are true:
Expand Down
2 changes: 1 addition & 1 deletion code_ownership.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ['[email protected]']
spec.summary = 'A gem to help engineering teams declare ownership of code'
Expand Down
4 changes: 3 additions & 1 deletion lib/code_ownership/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion lib/code_ownership/private/codeowners_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def self.write!

sig { returns(Pathname) }
def self.path
Pathname.pwd.join('.github/CODEOWNERS')
Pathname.pwd.join(
CodeOwnership.configuration.codeowners_path,
'CODEOWNERS'
)
end

sig { params(files: T::Array[String]).void }
Expand Down
42 changes: 42 additions & 0 deletions spec/lib/code_ownership/private/codeowners_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module CodeOwnership
RSpec.describe Private::CodeownersFile do
describe '.path' do
subject { described_class.path }

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(CodeOwnership).to receive(:configuration).and_return(configuration)
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
end
end
end