forked from localstack/localstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_marker_report.py
136 lines (109 loc) · 4.15 KB
/
render_marker_report.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
This script generates a markdown file with a summary of the current pytest marker usage,
as well as a list of certain markers, their corresponding tests and the CODEOWNERs of these tests.
It takes a pytest marker report generated by localstack.testing.pytest.marker_report
and extends it with data from the CODEOWNERS file.
The resulting data is processed by using a jinja2 template for the resulting GH issue template.
Example on how to run this script manually:
$ MARKER_REPORT_PATH='./target/marker-report.json' \
CODEOWNERS_PATH=./CODEOWNERS \
TEMPLATE_PATH=./.github/bot_templates/MARKER_REPORT_ISSUE.md.j2 \
OUTPUT_PATH=./target/MARKER_REPORT_ISSUE.md \
GITHUB_REPO=localstack/localstack \
COMMIT_SHA=e62e04509d0f950af3027c0f6df4e18c7385c630 \
python scripts/render_marker_report.py
"""
import dataclasses
import datetime
import json
import os
import jinja2
from codeowners import CodeOwners
@dataclasses.dataclass
class EnrichedReportMeta:
timestamp: str
repo_url: str
commit_sha: str
@dataclasses.dataclass
class TestEntry:
file_path: str
pytest_node_id: str
owners: list[str]
file_url: str
@dataclasses.dataclass
class EnrichedReport:
"""an object of this class is passed for template rendering"""
meta: EnrichedReportMeta
aggregated: dict[str, int]
owners_aws_unknown: list[TestEntry]
owners_aws_needs_fixing: list[TestEntry]
def load_file(filepath: str) -> str:
with open(filepath, "r") as fd:
return fd.read()
def load_codeowners(codeowners_path):
return CodeOwners(load_file(codeowners_path))
def render_template(*, template: str, enriched_report: EnrichedReport) -> str:
return jinja2.Template(source=template).render(data=enriched_report)
def create_test_entry(entry, *, code_owners: CodeOwners, commit_sha: str, github_repo: str):
rel_path = "".join(entry["file_path"].partition("tests/")[1:])
return TestEntry(
pytest_node_id=entry["node_id"],
file_path=rel_path,
owners=[o[1] for o in code_owners.of(rel_path)] or ["?"],
file_url=f"https://github.com/{github_repo}/blob/{commit_sha}/{rel_path}",
)
def enrich_with_codeowners(
*, input_data: dict, github_repo: str, commit_sha: str, code_owners: CodeOwners
) -> EnrichedReport:
return EnrichedReport(
meta=EnrichedReportMeta(
timestamp=datetime.datetime.utcnow().isoformat(),
repo_url=f"https://github.com/{github_repo}",
commit_sha=commit_sha,
),
aggregated={
k: v for k, v in input_data["aggregated_report"].items() if k.startswith("aws_")
},
owners_aws_unknown=sorted(
[
create_test_entry(
e, code_owners=code_owners, github_repo=github_repo, commit_sha=commit_sha
)
for e in input_data["entries"]
if "aws_unknown" in e["markers"]
],
key=lambda x: x.file_path,
),
owners_aws_needs_fixing=sorted(
[
create_test_entry(
e, code_owners=code_owners, github_repo=github_repo, commit_sha=commit_sha
)
for e in input_data["entries"]
if "aws_needs_fixing" in e["markers"]
],
key=lambda x: x.file_path,
),
)
def main():
marker_report_path = os.environ["MARKER_REPORT_PATH"]
codeowners_path = os.environ["CODEOWNERS_PATH"]
template_path = os.environ["TEMPLATE_PATH"]
output_path = os.environ["OUTPUT_PATH"]
github_repo = os.environ["GITHUB_REPO"]
commit_sha = os.environ["COMMIT_SHA"]
code_owners = CodeOwners(load_file(codeowners_path))
marker_report = json.loads(load_file(marker_report_path))
enriched_report = enrich_with_codeowners(
input_data=marker_report,
github_repo=github_repo,
commit_sha=commit_sha,
code_owners=code_owners,
)
rendered_markdown = render_template(
template=load_file(template_path), enriched_report=enriched_report
)
with open(output_path, "wt") as outfile:
outfile.write(rendered_markdown)
if __name__ == "__main__":
main()