audit-labs/audit-tools

A collection of scripts, queries, and other goodies you can use in an audit.

clone: git clone https://gitbay.org/audit-labs/audit-tools.git

v1.0.0: applications/gitlab/reporters/csv_reporter.py · raw

 1"""CSV reporter: writes one CSV file per data section into an output directory."""
 2
 3import csv
 4import os
 5
 6
 7def write(output_dir, filename, rows):
 8    """
 9    Write a list of dicts to a CSV file in output_dir.
10    Skips writing if rows is empty, but logs the skip.
11    """
12    if not rows:
13        print(f"  {filename}: no data, skipping")
14        return
15
16    os.makedirs(output_dir, exist_ok=True)
17    path = os.path.join(output_dir, filename)
18
19    with open(path, "w", newline="", encoding="utf-8") as f:
20        writer = csv.DictWriter(f, fieldnames=rows[0].keys())
21        writer.writeheader()
22        writer.writerows(rows)
23
24    print(f"  {filename}: {len(rows)} rows -> {path}")
25
26
27def write_summary(output_dir, group, sections):
28    """
29    Write a plain-text summary file listing section names and row counts.
30    sections: list of (label, row_count) tuples
31    """
32    path = os.path.join(output_dir, "summary.txt")
33    lines = [
34        "GitLab Audit Package",
35        f"Group: {group}",
36        "",
37        "Section                        Rows",
38        f"{'' * 40}",
39    ]
40    for label, count in sections:
41        lines.append(f"{label:<35}{count}")
42
43    with open(path, "w", encoding="utf-8") as f:
44        f.write("\n".join(lines) + "\n")
45
46    print(f"  summary.txt -> {path}")