Skip to content

Commit 75829ef

Browse files
authored
Merge pull request #26 from Qarik-Group/revert-25-revert-24-gasparev/manage-missing-default-section
Manage missing default section
2 parents ec955b8 + 50ead47 commit 75829ef

7 files changed

Lines changed: 165 additions & 16 deletions

File tree

gitlab_codeowners_linter/autofix.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,18 @@ def _update_codeowners_file(codeowners_data, file_path):
115115
with open(file_path, 'w') as f:
116116
for section in codeowners_data:
117117
# if the default section is empty let's skip it
118-
if section.codeowner_section == DEFAULT_SECTION and section.entries == []:
119-
continue
120118
if section.codeowner_section != DEFAULT_SECTION:
121119
f.write('\n')
122120
if section.comments:
123121
for comment_line in section.comments:
124122
f.write(f'{comment_line}\n')
125123
if section.codeowner_section != DEFAULT_SECTION:
126124
f.write(f'[{section.codeowner_section}]')
127-
f.write('\n')
128-
for entry in section.entries:
129-
if entry.comments:
130-
for comment_line in entry.comments:
131-
f.write(f'{comment_line}\n')
132-
owners = ' '.join(str(x) for x in entry.owners)
133-
f.write(f'{entry.path} {owners}\n')
125+
if section.entries:
126+
f.write('\n')
127+
for entry in section.entries:
128+
if entry.comments:
129+
for comment_line in entry.comments:
130+
f.write(f'{comment_line}\n')
131+
owners = ' '.join(str(x) for x in entry.owners)
132+
f.write(f'{entry.path} {owners}\n')

gitlab_codeowners_linter/checks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ def check(codeowners_data):
7171

7272
def _is_codeowners_empty(codeowners_data):
7373
empty = False
74-
for section in codeowners_data:
75-
if not section.get_paths():
76-
empty = True
74+
if all(not section.get_paths() for section in codeowners_data):
75+
empty = True
7776
return empty
7877

7978

gitlab_codeowners_linter/codeowners_linter.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
# - there must be no empty lines between paths
77
# - paths in a section must be unique
88
# - paths must exist
9-
# Note: there's the assumption that on the top of the file there are paths that
10-
# are not under any section. This may not be always true, like in a CODEOWNERS
11-
# file that only uses sections TODO: manage this corner case
129
#
1310
from __future__ import annotations
1411

gitlab_codeowners_linter/parser.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def parse_codeowners(file_path):
5656
continue
5757
if _is_empty_line(line):
5858
if _is_top_of_section(codeowners_content):
59-
# TODO: note that if we have a CODEOWNERS file with a black line on top, that line will be threated as a comment line for the first general section
6059
codeowners_content[-1].comments.extend(comments_block)
6160
comments_block = []
6261
continue

tests/codeowners_linter_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,23 @@ class TestCase:
275275
'resources/unformatted_autofix.txt',
276276
),
277277
),
278+
TestCase(
279+
name='not_formatted_no_default_section',
280+
input=os.path.join(
281+
os.path.dirname(os.path.abspath(__file__)),
282+
'resources/no_default_section_input.txt',
283+
),
284+
expected_check=[
285+
'Sections are not sorted',
286+
'There are blank lines in the sections SECTION_NAME, BUILD, SECURITY',
287+
'The paths in sections SECTION_NAME, BUILD, SYSTEM, TEST_SECTION are not sorted',
288+
'The sections SECTION_NAME have duplicate paths',
289+
],
290+
expected_fix=os.path.join(
291+
os.path.dirname(os.path.abspath(__file__)),
292+
'resources/no_default_section_autofix.txt',
293+
),
294+
),
278295
TestCase(
279296
name='empty_file',
280297
input=os.path.join(
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
### CODEOWNERS ###
2+
#
3+
# This is a test case with the following issues:
4+
# - sections are not ordered alphabetically
5+
# - paths within sections are not sorted alphabetically
6+
# - paths within sections have blank lines
7+
# - the default section is missing
8+
#
9+
10+
# Build Group
11+
[BUILD]
12+
.gitlab/.gitlab-ci.yml test@email.com
13+
.gitlab/ci/ test@email.com test1@email.com test2@email.com test3@email.com
14+
CODEOWNERS @test/teams/build/admin test@email.com test1@email.com test2@email.com test3@email.com
15+
/ops/gitlab/ test@email.com
16+
17+
# Comment for SECTION_NAME
18+
[SECTION_NAME]
19+
* test@email.com test1@email.com test2@email.com test3@email.com
20+
# this is a comment for *.md test@email.com
21+
*.md test@email.com
22+
.gitlab test@email.com
23+
WORKSPACE test@email.com test1@email.com test2@email.com test3@email.com
24+
# this is a comment
25+
/.pylintrc test@email.com
26+
/analytics/ test@email.com
27+
/go/src/github.com/test/base/sync/ test@email.com
28+
# this is a multi-
29+
# line comment for /go/src/github.com/test/path1/
30+
/go/src/github.com/test/path1/ test@email.com
31+
/go/src/github.com/test/path2/ test@email.com
32+
/go/src/github.com/test/path3/ test@email.com
33+
# This is a comment for a non existing line
34+
/go/src/github.com/test/path4/ test@email.com
35+
/ui test@email.com test1@email.com test2@email.com test3@email.com
36+
/ui/components/ test_a@email.com test_b@email.com test_c@email.com
37+
/ui/lighting test@email.com
38+
/ui/lighting/client test@email.com
39+
/WORKSPACE test@email.com test1@email.com test2@email.com test3@email.com
40+
/www/ test@email.com
41+
/www/gitlab/test/path test@email.com
42+
43+
# Comment for security
44+
[SECURITY]
45+
/ops/terraform/path1 test@email.com
46+
/ops/terraform/path2/ test@email.com
47+
48+
# Comment for [SYSTEM]
49+
# This is also a comment for [SYSTEM]
50+
[SYSTEM]
51+
/docker/pipeline/ test@email.com
52+
/go/src/repo/pipeline/ test@email.com
53+
/ops/terraform/pipeline/ test@email.com
54+
/pipeline/ test@email.com test1@email.com test2@email.com test3@email.com
55+
/release/release_script.sh test@email.com
56+
57+
# This is a comment for [TEST_SECTION]
58+
[TEST_SECTION]
59+
/docker/pipeline/ test@email.com
60+
/go/src/repo/pipeline/ test@email.com
61+
/ops/terraform/pipeline/ test@email.com test1@email.com test2@email.com test3@email.com
62+
/pipeline/ test@email.com
63+
/release/release_script.sh test@email.com
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
### CODEOWNERS ###
2+
#
3+
# This is a test case with the following issues:
4+
# - sections are not ordered alphabetically
5+
# - paths within sections are not sorted alphabetically
6+
# - paths within sections have blank lines
7+
# - the default section is missing
8+
#
9+
10+
# Comment for SECTION_NAME
11+
[SECTION_NAME]
12+
# this is a comment for *.md test@email.com
13+
*.md test@email.com
14+
* test@email.com test1@email.com test2@email.com test3@email.com
15+
# this is a comment
16+
/.pylintrc test@email.com
17+
WORKSPACE test@email.com test1@email.com test2@email.com test3@email.com
18+
/WORKSPACE test@email.com test1@email.com test2@email.com test3@email.com
19+
/analytics/ test@email.com
20+
/go/src/github.com/test/base/sync/ test@email.com
21+
22+
# this is a multi-
23+
# line comment for /go/src/github.com/test/path1/
24+
/go/src/github.com/test/path1/ test@email.com
25+
/go/src/github.com/test/path2/ test@email.com
26+
/go/src/github.com/test/path3/ test@email.com
27+
# This is a comment for a non existing line
28+
/go/src/github.com/test/path4/ test@email.com
29+
/ui/lighting/client test@email.com
30+
/ui/lighting test@email.com
31+
/ui/components/ test_a@email.com
32+
/www/ test@email.com
33+
/www/gitlab/test/path test@email.com
34+
/ui/components/ test_c@email.com
35+
.gitlab test@email.com
36+
/ui test@email.com test1@email.com test2@email.com test3@email.com
37+
/ui/components/ test_a@email.com test_b@email.com
38+
39+
# Build Group
40+
[BUILD] #this is a comment
41+
CODEOWNERS @test/teams/build/admin test@email.com test1@email.com test2@email.com test3@email.com
42+
.gitlab/.gitlab-ci.yml test@email.com
43+
.gitlab/ci/ test@email.com test1@email.com test2@email.com test3@email.com
44+
45+
/ops/gitlab/ test@email.com
46+
47+
48+
# Comment for [SYSTEM]
49+
50+
[SYSTEM]
51+
# This is also a comment for [SYSTEM]
52+
53+
/go/src/repo/pipeline/ test@email.com
54+
/ops/terraform/pipeline/ test@email.com
55+
/pipeline/ test@email.com test1@email.com test2@email.com test3@email.com
56+
/docker/pipeline/ test@email.com
57+
/release/release_script.sh test@email.com
58+
59+
[SECURITY]
60+
# Comment for security
61+
62+
/ops/terraform/path1 test@email.com
63+
64+
65+
/ops/terraform/path2/ test@email.com
66+
67+
68+
[TEST_SECTION]
69+
# This is a comment for [TEST_SECTION]
70+
71+
/go/src/repo/pipeline/ test@email.com
72+
/ops/terraform/pipeline/ test@email.com test1@email.com test2@email.com test3@email.com
73+
/pipeline/ test@email.com
74+
/docker/pipeline/ test@email.com
75+
/release/release_script.sh test@email.com

0 commit comments

Comments
 (0)