-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarkdown-summary.py
104 lines (91 loc) · 3.43 KB
/
markdown-summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
import json
import sys
## Example input data
# [
# {
# "profileName": "redhat-enterprise-linux-8-stig-baseline",
# "resultSets": ["rhel-8_hardened.json"],
# "compliance": 66,
# "passed": {"critical": 0, "high": 11, "medium": 208, "low": 8, "total": 227},
# "failed": {"critical": 0, "high": 6, "medium": 87, "low": 19, "total": 112},
# "skipped": {"critical": 0, "high": 1, "medium": 1, "low": 1, "total": 3},
# "error": {"critical": 0, "high": 0, "medium": 0, "low": 0, "total": 0},
# "no_impact": {"none": 33, "total": 33},
# }
# ]
## Desired output Table
# # redhat-enterprise-linux-8-stig-baseline
# ## Result Sets: rhel-8_hardened.json
# | | Compliance (%) | Passed | Failed | Not Reviewed | Not Applicable | Error |
# | ---------------- | -------------- | ------ | ------ | ------------ | -------------- | ----- |
# | **Total** | 66 | 227 | 112 | 3 | 33 | 0 |
# | **Critical** | - | 0 | 0 | 0 | - | 0 |
# | **High** | - | 11 | 6 | 1 | - | 0 |
# | **Medium** | - | 208 | 87 | 1 | - | 0 |
# | **Low** | - | 8 | 19 | 1 | - | 0 |
# | **Not Applicable** | - | - | - | - | 33 | - |
import json
import sys
# Load your JSON data from standard input
data = json.load(sys.stdin)
# Extract the first item from the list (assuming there's only one item)
data = data[0]
# Define the order of the rows and columns
row_order = ["Total", "Critical", "High", "Medium", "Low", "Not Applicable"]
column_order = [
"Passed :white_check_mark:",
"Failed :x:",
"Not Reviewed :leftwards_arrow_with_hook:",
"Not Applicable :heavy_minus_sign:",
"Error :warning:",
]
# Calculate the maximum width of each column
column_widths = [max(len(row), len(col)) for row, col in zip(row_order, column_order)]
column_widths = [max(column_widths)] * len(
column_widths
) # Make all columns the same width for a cleaner look
# Generate the Markdown table
table = (
"| "
+ "Compliance: "
+ str(data["compliance"])
+ "% :test_tube:"
+ " | "
+ " | ".join(col.ljust(width) for col, width in zip(column_order, column_widths))
+ " |\n"
)
table += (
"| "
+ "-".ljust(max(column_widths), "-")
+ " | "
+ " | ".join("-".ljust(width, "-") for width in column_widths)
+ " |\n"
)
for row in row_order:
if row == "Total":
values = [
str(data["passed"]["total"]),
str(data["failed"]["total"]),
str(data["skipped"]["total"]),
str(data["no_impact"]["total"]),
str(data["error"]["total"]),
]
elif row == "Not Applicable":
values = ["-", "-", "-", str(data["no_impact"]["total"]), "-"]
else:
values = [
str(data["passed"][row.lower()]),
str(data["failed"][row.lower()]),
str(data["skipped"][row.lower()]),
"-",
str(data["error"][row.lower()]),
]
table += (
"| "
+ ("**" + row + "**").ljust(max(column_widths) + 2)
+ " | "
+ " | ".join(val.ljust(width) for val, width in zip(values, column_widths))
+ " |\n"
)
print(table)