Skip to content

Commit 50ead47

Browse files
committed
Correctly order sections
1 parent 1dbaa79 commit 50ead47

4 files changed

Lines changed: 21 additions & 23 deletions

File tree

gitlab_codeowners_linter/autofix.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +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 != codeowners_data[0]:
118+
if section.codeowner_section != DEFAULT_SECTION:
119119
f.write('\n')
120120
if section.comments:
121121
for comment_line in section.comments:
122122
f.write(f'{comment_line}\n')
123123
if section.codeowner_section != DEFAULT_SECTION:
124124
f.write(f'[{section.codeowner_section}]')
125-
f.write('\n')
126-
for entry in section.entries:
127-
if entry.comments:
128-
for comment_line in entry.comments:
129-
f.write(f'{comment_line}\n')
130-
owners = ' '.join(str(x) for x in entry.owners)
131-
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/parser.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,4 @@ def parse_codeowners(file_path):
101101
if _is_empty_line(section.entries[-1].path):
102102
section.entries = section.entries[:-1]
103103

104-
# let's also check if the default section exists, if not let's remove it
105-
if codeowners_content[0].entries == []:
106-
if codeowners_content[0].comments:
107-
for i in range(0, len(codeowners_content[0].comments)):
108-
codeowners_content[1].comments.insert(
109-
i, codeowners_content[0].comments[i])
110-
codeowners_content.pop(0)
111-
112104
return codeowners_content

tests/resources/no_default_section_autofix.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
# - sections are not ordered alphabetically
55
# - paths within sections are not sorted alphabetically
66
# - paths within sections have blank lines
7+
# - the default section is missing
78
#
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
818
[SECTION_NAME]
919
* test@email.com test1@email.com test2@email.com test3@email.com
1020
# this is a comment for *.md test@email.com
@@ -30,13 +40,6 @@ WORKSPACE test@email.com test1@email.com test2@email.com test3@email.com
3040
/www/ test@email.com
3141
/www/gitlab/test/path test@email.com
3242

33-
# Build Group
34-
[BUILD]
35-
.gitlab/.gitlab-ci.yml test@email.com
36-
.gitlab/ci/ test@email.com test1@email.com test2@email.com test3@email.com
37-
CODEOWNERS @test/teams/build/admin test@email.com test1@email.com test2@email.com test3@email.com
38-
/ops/gitlab/ test@email.com
39-
4043
# Comment for security
4144
[SECURITY]
4245
/ops/terraform/path1 test@email.com

tests/resources/no_default_section_input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# - sections are not ordered alphabetically
55
# - paths within sections are not sorted alphabetically
66
# - paths within sections have blank lines
7+
# - the default section is missing
78
#
89

10+
# Comment for SECTION_NAME
911
[SECTION_NAME]
1012
# this is a comment for *.md test@email.com
1113
*.md test@email.com

0 commit comments

Comments
 (0)