Skip to content

Commit 828a4cd

Browse files
author
Alex Evanczuk
authored
Add validation to ensure GitHub teams are unique (#62)
* Add a vaildation for unique github teams * bump version * fix readme typo
1 parent 727b083 commit 828a4cd

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
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.32.14)
4+
code_ownership (1.32.15)
55
code_teams (~> 1.0)
66
packs
77
sorbet-runtime

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ CodeOwnership comes with a validation function to ensure the following things ar
125125
1) Only one mechanism is defining file ownership. That is -- you can't have a file annotation on a file owned via package-based or glob-based ownership. This helps make ownership behavior more clear by avoiding concerns about precedence.
126126
2) All teams referenced as an owner for any file or package is a valid team (i.e. it's in the list of `CodeTeams.all`).
127127
3) All files have ownership. You can specify in `unowned_globs` to represent a TODO list of files to add ownership to.
128-
3) The `.github/CODEOWNERS` file is up to date. This is automatically corrected and staged unless specified otherwise with `bin/codeownership validate --skip-autocorrect --skip-stage`. You can turn this validation off by setting `skip_codeowners_validation: true` in `code_ownership.yml`.
128+
3) The `.github/CODEOWNERS` file is up to date. This is automatically corrected and staged unless specified otherwise with `bin/codeownership validate --skip-autocorrect --skip-stage`. You can turn this validation off by setting `skip_codeowners_validation: true` in `config/code_ownership.yml`.
129129

130130
CodeOwnership also allows you to specify which globs and file extensions should be considered ownable.
131131

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.32.14'
3+
spec.version = '1.32.15'
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/private/team_plugins/github.rb

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ def github
1818
raw_github['do_not_add_to_codeowners_file'] || false
1919
)
2020
end
21+
22+
sig { override.params(teams: T::Array[CodeTeams::Team]).returns(T::Array[String]) }
23+
def self.validation_errors(teams)
24+
all_github_teams = teams.flat_map { |team| self.for(team).github.team }
25+
26+
teams_used_more_than_once = all_github_teams.tally.select do |_team, count|
27+
count > 1
28+
end
29+
30+
errors = T.let([], T::Array[String])
31+
32+
if teams_used_more_than_once.any?
33+
errors << <<~ERROR
34+
The following teams are specified multiple times:
35+
Each code team must have a unique GitHub team in order to write the CODEOWNERS file correctly.
36+
37+
#{teams_used_more_than_once.keys.join("\n")}
38+
ERROR
39+
end
40+
41+
errors
42+
end
2143
end
2244
end
2345
end

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

+23
Original file line numberDiff line numberDiff line change
@@ -592,5 +592,28 @@ module CodeOwnership
592592
end
593593
end
594594
end
595+
596+
describe 'uniqueness of github teams' do
597+
before do
598+
write_file('config/teams/bar.yml', <<~CONTENTS)
599+
name: Bar
600+
github:
601+
team: '@MyOrg/bar-team'
602+
CONTENTS
603+
604+
write_file('config/teams/foo.yml', <<~CONTENTS)
605+
name: Bar
606+
github:
607+
team: '@MyOrg/bar-team'
608+
CONTENTS
609+
end
610+
611+
it 'expect code teams validations to fail' do
612+
expect(CodeTeams.validation_errors(CodeTeams.all)).to eq([
613+
"More than 1 definition for Bar found",
614+
"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"
615+
])
616+
end
617+
end
595618
end
596619
end

0 commit comments

Comments
 (0)