Skip to content

Commit 2b233e5

Browse files
authored
Use environment variable to set CODEOWNERS write path (#114)
* Allow custom codeowners path via environment variable * Add specs * Add documentation to README * Bump gem version * fixup! Add documentation to README * Move CODEOWNERS path into code_ownership.yml configuration
1 parent c56c55d commit 2b233e5

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ bin/codeownership for_team 'My Team' > tmp/ownership_report.md
164164

165165
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.
166166

167+
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`.
168+
167169
## Proper Configuration & Validation
168170

169171
CodeOwnership comes with a validation function to ensure the following things are true:

code_ownership.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = 'code_ownership'
3-
spec.version = '1.38.2'
3+
spec.version = '1.38.3'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66
spec.summary = 'A gem to help engineering teams declare ownership of code'

lib/code_ownership/configuration.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Configuration < T::Struct
1212
const :skip_codeowners_validation, T::Boolean
1313
const :raw_hash, T::Hash[T.untyped, T.untyped]
1414
const :require_github_teams, T::Boolean
15+
const :codeowners_path, String
1516

1617
sig { returns(Configuration) }
1718
def self.fetch
@@ -29,7 +30,8 @@ def self.fetch
2930
js_package_paths: js_package_paths(config_hash),
3031
skip_codeowners_validation: config_hash.fetch('skip_codeowners_validation', false),
3132
raw_hash: config_hash,
32-
require_github_teams: config_hash.fetch('require_github_teams', false)
33+
require_github_teams: config_hash.fetch('require_github_teams', false),
34+
codeowners_path: config_hash.fetch('codeowners_path', '.github'),
3335
)
3436
end
3537

lib/code_ownership/private/codeowners_file.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ def self.write!
111111

112112
sig { returns(Pathname) }
113113
def self.path
114-
Pathname.pwd.join('.github/CODEOWNERS')
114+
Pathname.pwd.join(
115+
CodeOwnership.configuration.codeowners_path,
116+
'CODEOWNERS'
117+
)
115118
end
116119

117120
sig { params(files: T::Array[String]).void }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module CodeOwnership
2+
RSpec.describe Private::CodeownersFile do
3+
describe '.path' do
4+
subject { described_class.path }
5+
6+
context 'when codeowners_path is set in the configuration' do
7+
let(:configuration) do
8+
Configuration.new(
9+
owned_globs: [],
10+
unowned_globs: [],
11+
js_package_paths: [],
12+
unbuilt_gems_path: nil,
13+
skip_codeowners_validation: false,
14+
raw_hash: {},
15+
require_github_teams: false,
16+
codeowners_path: path
17+
)
18+
end
19+
20+
before do
21+
allow(CodeOwnership).to receive(:configuration).and_return(configuration)
22+
end
23+
24+
context "to 'foo'" do
25+
let(:path) { 'foo' }
26+
27+
it 'uses the environment variable' do
28+
expect(subject).to eq(Pathname.pwd.join('foo', 'CODEOWNERS'))
29+
end
30+
end
31+
32+
context 'to empty' do
33+
let(:path) { '' }
34+
35+
it 'uses the environment variable' do
36+
expect(subject).to eq(Pathname.pwd.join('CODEOWNERS'))
37+
end
38+
end
39+
end
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)