Skip to content

Commit 5dea641

Browse files
authored
Update Github CodeTeam Plugin to enforce github.team presence (#79)
* Update Github CodeTeam Plugin to enforce `github.team` presence * Use GithubStruct * Ignore CodeTeams without a `github.team` key * Add `require_github_teams` configuration option * Update Github Plugin to enforce required_github_teams * Fix github_codeowners_up_to_date_spec.rb:759 * Add spec for require_github_teams configuration * Update uniqueness of github teams spec * Bump minor version to 1.35.0 * Update Gemfile.lock
1 parent 2407ddb commit 5dea641

File tree

5 files changed

+91
-16
lines changed

5 files changed

+91
-16
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
code_ownership (1.34.2)
4+
code_ownership (1.35.0)
55
code_teams (~> 1.0)
66
packs-specification
77
sorbet-runtime (>= 0.5.10821)

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.34.2'
3+
spec.version = '1.35.0'
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
@@ -11,6 +11,7 @@ class Configuration < T::Struct
1111
const :unbuilt_gems_path, T.nilable(String)
1212
const :skip_codeowners_validation, T::Boolean
1313
const :raw_hash, T::Hash[T.untyped, T.untyped]
14+
const :require_github_teams, T::Boolean
1415

1516
sig { returns(Configuration) }
1617
def self.fetch
@@ -27,7 +28,8 @@ def self.fetch
2728
unowned_globs: config_hash.fetch('unowned_globs', []),
2829
js_package_paths: js_package_paths(config_hash),
2930
skip_codeowners_validation: config_hash.fetch('skip_codeowners_validation', false),
30-
raw_hash: config_hash
31+
raw_hash: config_hash,
32+
require_github_teams: config_hash.fetch('require_github_teams', false)
3133
)
3234
end
3335

lib/code_ownership/private/team_plugins/github.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,26 @@ def github
2121

2222
sig { override.params(teams: T::Array[CodeTeams::Team]).returns(T::Array[String]) }
2323
def self.validation_errors(teams)
24-
all_github_teams = teams.flat_map { |team| self.for(team).github.team }
24+
all_github_teams = teams.flat_map { |team| self.for(team).github.team }.compact
2525

2626
teams_used_more_than_once = all_github_teams.tally.select do |_team, count|
2727
count > 1
2828
end
2929

3030
errors = T.let([], T::Array[String])
3131

32+
if require_github_teams?
33+
missing_github_teams = teams.select { |team| self.for(team).github.team.nil? }
34+
35+
if missing_github_teams.any?
36+
errors << <<~ERROR
37+
The following teams are missing `github.team` entries:
38+
39+
#{missing_github_teams.map(&:config_yml).join("\n")}
40+
ERROR
41+
end
42+
end
43+
3244
if teams_used_more_than_once.any?
3345
errors << <<~ERROR
3446
The following teams are specified multiple times:
@@ -40,6 +52,11 @@ def self.validation_errors(teams)
4052

4153
errors
4254
end
55+
56+
sig { returns(T::Boolean) }
57+
def self.require_github_teams?
58+
CodeOwnership.configuration.require_github_teams
59+
end
4360
end
4461
end
4562
end

spec/lib/code_ownership/private/validations/github_codeowners_up_to_date_spec.rb

+68-12
Original file line numberDiff line numberDiff line change
@@ -757,25 +757,81 @@ module CodeOwnership
757757
end
758758

759759
describe 'uniqueness of github teams' do
760+
context 'when the CodeTeam has a github.team key' do
761+
before do
762+
write_configuration
763+
764+
write_file('config/teams/bar.yml', <<~CONTENTS)
765+
name: Bar
766+
github:
767+
team: '@MyOrg/bar-team'
768+
CONTENTS
769+
770+
write_file('config/teams/foo.yml', <<~CONTENTS)
771+
name: Bar
772+
github:
773+
team: '@MyOrg/bar-team'
774+
CONTENTS
775+
end
776+
777+
it 'expect code teams validations to fail' do
778+
expect(CodeTeams.validation_errors(CodeTeams.all)).to eq([
779+
"More than 1 definition for Bar found",
780+
"The following teams are specified multiple times:\nEach code team must have a unique GitHub team in order to write the CODEOWNERS file correctly.\n\n@MyOrg/bar-team\n"
781+
])
782+
end
783+
end
784+
785+
context 'when the CodeTeam does not have a github.team key' do
786+
before do
787+
write_configuration
788+
789+
write_file('config/teams/bar.yml', <<~CONTENTS)
790+
name: Bar
791+
CONTENTS
792+
793+
write_file('config/teams/foo.yml', <<~CONTENTS)
794+
name: Bar
795+
CONTENTS
796+
end
797+
798+
it 'does not report CodeTeams without github.teams key' do
799+
expect(CodeTeams.validation_errors(CodeTeams.all)).to eq([
800+
"More than 1 definition for Bar found"
801+
])
802+
end
803+
end
804+
end
805+
806+
describe 'require_github_teams configuration option' do
760807
before do
761-
write_file('config/teams/bar.yml', <<~CONTENTS)
762-
name: Bar
763-
github:
764-
team: '@MyOrg/bar-team'
765-
CONTENTS
808+
write_configuration('require_github_teams' => require_github_teams)
766809

767810
write_file('config/teams/foo.yml', <<~CONTENTS)
811+
name: Foo
812+
CONTENTS
813+
814+
write_file('config/teams/bar.yml', <<~CONTENTS)
768815
name: Bar
769-
github:
770-
team: '@MyOrg/bar-team'
771816
CONTENTS
772817
end
773818

774-
it 'expect code teams validations to fail' do
775-
expect(CodeTeams.validation_errors(CodeTeams.all)).to eq([
776-
"More than 1 definition for Bar found",
777-
"The following teams are specified multiple times:\nEach code team must have a unique GitHub team in order to write the CODEOWNERS file correctly.\n\n@MyOrg/bar-team\n"
778-
])
819+
context 'when require_github_teams is enabled' do
820+
let(:require_github_teams) { true }
821+
822+
it 'reports CodeTeams without github.team keys' do
823+
expect(CodeTeams.validation_errors(CodeTeams.all)).to eq([
824+
"The following teams are missing `github.team` entries:\n\nconfig/teams/bar.yml\nconfig/teams/foo.yml\n"
825+
])
826+
end
827+
end
828+
829+
context 'when require_github_teams is disabled' do
830+
let(:require_github_teams) { false }
831+
832+
it 'does not report any errors' do
833+
expect(CodeTeams.validation_errors(CodeTeams.all)).to be_empty
834+
end
779835
end
780836
end
781837
end

0 commit comments

Comments
 (0)