@@ -12,55 +12,34 @@ class GithubCodeownersUpToDate
1212 def validation_errors ( files :, autocorrect : true , stage_changes : true )
1313 return [ ] if Private . configuration . skip_codeowners_validation
1414
15- codeowners_filepath = Pathname . pwd . join ( '.github/CODEOWNERS' )
16- FileUtils . mkdir_p ( codeowners_filepath . dirname ) if !codeowners_filepath . dirname . exist?
17-
18- header = <<~HEADER
19- # STOP! - DO NOT EDIT THIS FILE MANUALLY
20- # This file was automatically generated by "bin/codeownership validate".
21- #
22- # CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub
23- # teams. This is useful when developers create Pull Requests since the
24- # code/file owner is notified. Reference GitHub docs for more details:
25- # https://help.github.com/en/articles/about-code-owners
26- HEADER
27-
28- expected_content_lines = [
29- *header . split ( "\n " ) ,
30- nil , # For line between header and codeowners_file_lines
31- *codeowners_file_lines ,
32- nil , # For end-of-file newline
33- ]
34-
35- expected_contents = expected_content_lines . join ( "\n " )
36- actual_contents = codeowners_filepath . exist? ? codeowners_filepath . read : ""
37- actual_content_lines = actual_contents . split ( "\n " )
38-
39- codeowners_up_to_date = actual_contents == expected_contents
15+ actual_content_lines = CodeownersFile . actual_contents_lines
16+ expected_content_lines = CodeownersFile . expected_contents_lines
17+ codeowners_up_to_date = actual_content_lines == expected_content_lines
4018
4119 errors = T . let ( [ ] , T ::Array [ String ] )
4220
4321 if !codeowners_up_to_date
4422 if autocorrect
45- codeowners_filepath . write ( expected_contents )
23+ CodeownersFile . write!
4624 if stage_changes
47- `git add #{ codeowners_filepath } `
25+ `git add #{ CodeownersFile . path } `
4826 end
4927 else
5028 # If there is no current file or its empty, display a shorter message.
5129 missing_lines = expected_content_lines - actual_content_lines
5230 extra_lines = actual_content_lines - expected_content_lines
31+
5332 missing_lines_text = if missing_lines . any?
5433 <<~COMMENT
5534 CODEOWNERS should contain the following lines, but does not:
56- #{ ( expected_content_lines - actual_content_lines ) . map { |line | "- \" #{ line } \" " } . join ( "\n " ) }
35+ #{ ( missing_lines ) . map { |line | "- \" #{ line } \" " } . join ( "\n " ) }
5736 COMMENT
5837 end
5938
6039 extra_lines_text = if extra_lines . any?
6140 <<~COMMENT
6241 CODEOWNERS should not contain the following lines, but it does:
63- #{ ( actual_content_lines - expected_content_lines ) . map { |line | "- \" #{ line } \" " } . join ( "\n " ) }
42+ #{ ( extra_lines ) . map { |line | "- \" #{ line } \" " } . join ( "\n " ) }
6443 COMMENT
6544 end
6645
@@ -74,7 +53,7 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
7453 ""
7554 end
7655
77- if actual_contents == ""
56+ if actual_content_lines == [ ]
7857 errors << <<~CODEOWNERS_ERROR
7958 CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
8059 CODEOWNERS_ERROR
@@ -90,37 +69,6 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
9069
9170 errors
9271 end
93-
94- private
95-
96- # Generate the contents of a CODEOWNERS file that GitHub can use to
97- # automatically assign reviewers
98- # https://help.github.com/articles/about-codeowners/
99- sig { returns ( T ::Array [ String ] ) }
100- def codeowners_file_lines
101- github_team_map = CodeTeams . all . each_with_object ( { } ) do |team , map |
102- team_github = TeamPlugins ::Github . for ( team ) . github
103- next if team_github . do_not_add_to_codeowners_file
104-
105- map [ team . name ] = team_github . team
106- end
107-
108- Mapper . all . flat_map do |mapper |
109- codeowners_lines = mapper . codeowners_lines_to_owners . filter_map do |line , team |
110- team_mapping = github_team_map [ team &.name ]
111- next unless team_mapping
112-
113- "/#{ line } #{ team_mapping } "
114- end
115- next [ ] if codeowners_lines . empty?
116-
117- [
118- '' ,
119- "# #{ mapper . description } " ,
120- *codeowners_lines . sort ,
121- ]
122- end
123- end
12472 end
12573 end
12674 end
0 commit comments