Skip to content

Commit 4c16b5f

Browse files
authored
Add header to housekeeper CSV (#2016)
* Fixes #2015
1 parent bd05ea9 commit 4c16b5f

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

sonar/audit/problem.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def __dump_csv(problems: list[Problem], file: str, server_id: str = None, with_u
8686
"""
8787
with utilities.open_file(file, "w") as fd:
8888
csvwriter = csv.writer(fd, delimiter=separator)
89+
header = ["Server Id"] if server_id else []
90+
header += ["Audit Check", "Category", "Severity", "Message"]
91+
header += ["URL"] if with_url else []
92+
csvwriter.writerow(header)
8993
for p in problems:
9094
data = []
9195
if server_id is not None:

test/unit/test_housekeeper.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ def test_housekeeper() -> None:
4040
for opts in __GOOD_OPTS:
4141
assert tutil.run_cmd(housekeeper.main, f"{CMD} {tutil.SQS_OPTS} {opts}") == errcodes.OK
4242

43+
4344
def test_keep_branches_override(csv_file: Generator[str]) -> None:
4445
"""test_keep_branches_override"""
4546
if tutil.SQ.version() == "community":
4647
pytest.skip("No branches in Community")
47-
assert tutil.run_cmd(housekeeper.main, f"{CMD} {tutil.SQS_OPTS} -P 730 -T 730 -R 730 -B 90 -f {csv_file}") == errcodes.OK
48-
nbr_br = tutil.csv_col_count_values(csv_file, 1, "BRANCH_LAST_ANALYSIS")
49-
assert tutil.run_cmd(housekeeper.main, f"{CMD} {tutil.SQS_OPTS} -P 730 -T 730 -R 730 -B 90 -f {csv_file} --keepWhenInactive 'dontkeepanything'") == errcodes.OK
48+
opts = f"{CMD} {tutil.SQS_OPTS} -P 730 -T 730 -R 730 -B 90 -f {csv_file}"
49+
assert tutil.run_cmd(housekeeper.main, opts) == errcodes.OK
50+
nbr_br = tutil.csv_col_count_values(csv_file, "Audit Check", "BRANCH_LAST_ANALYSIS")
51+
assert tutil.run_cmd(housekeeper.main, f"{opts} --keepWhenInactive 'dontkeepanything'") == errcodes.OK
5052
# With 'dontkeepanything' as branch regexp, more branches to delete should be found
51-
assert tutil.csv_col_count_values(csv_file, 1, "BRANCH_LAST_ANALYSIS") > nbr_br
52-
53+
assert tutil.csv_col_count_values(csv_file, "Audit Check", "BRANCH_LAST_ANALYSIS") > nbr_br

test/unit/utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def csv_col_count_values(csv_file: str, col_name_or_nbr: Union[str, int], *value
304304
if isinstance(col_name_or_nbr, int):
305305
col = col_name_or_nbr - 1
306306
else:
307-
(col,) = get_cols(next(reader, col_name_or_nbr))
307+
(col,) = get_cols(next(reader), col_name_or_nbr)
308308
counter = sum(1 if line[col] in values_to_search else 0 for line in reader)
309309
return counter
310310

0 commit comments

Comments
 (0)