lib/checks/format-comment.py
97 lines · 3005 bytes
1"""Aggregates findings from all check jobs into a formatted PR comment."""
2
3import json
4import os
5import sys
6
7ARTIFACTS_DIR = "/tmp/artifacts"
8OUTPUT_DIR = "/tmp/pr-meta"
9
10CONTRIBUTING = "https://github.com/Lissy93/awesome-privacy/blob/main/.github/CONTRIBUTING.md"
11
12COMMENT_TEMPLATE = """<!-- pr-check-bot -->
13Hello @{user}
14
15Thank you for contributing to Awesome Privacy! We will review your PR shortly. In the meantime, please ensure that your submission is inline with our guidelines in our [Contributing Requirements]({contributing}).
16
17Looks like there could be some issues in your PR. Please double check that:
18
19{findings}
20
21> [!NOTE]
22> I am a bot, and sometimes make mistakes in my suggestions. But a human will review your submission shortly!"""
23
24
25def load_findings(filename):
26 """Load a findings JSON array from the artifacts directory, or empty list on error."""
27 try:
28 with open(os.path.join(ARTIFACTS_DIR, filename)) as f:
29 data = json.load(f)
30 return data if isinstance(data, list) else []
31 except Exception:
32 return []
33
34
35def collect_findings():
36 """Gather all findings in display order: compliance, data, project."""
37 all_findings = []
38 all_findings.extend(load_findings("findings-compliance.json"))
39 all_findings.extend(load_findings("findings-data.json"))
40 all_findings.extend(load_findings("findings-project.json"))
41 return all_findings
42
43
44def format_comment(findings, user):
45 """Build the markdown comment from findings."""
46 bullet_list = "\n".join(f"- {f}" for f in findings)
47 return COMMENT_TEMPLATE.format(
48 user=user, contributing=CONTRIBUTING, findings=bullet_list,
49 )
50
51
52def write_step_summary(findings):
53 """Write a summary to GITHUB_STEP_SUMMARY."""
54 summary_file = os.environ.get("GITHUB_STEP_SUMMARY")
55 if not summary_file:
56 return
57 lines = ["## PR Check Summary\n"]
58 if findings:
59 lines.append(f"⚠️ Found {len(findings)} issue(s):\n")
60 for f in findings:
61 lines.append(f"- {f}")
62 else:
63 lines.append("✅ All checks passed.\n")
64 with open(summary_file, "a") as f:
65 f.write("\n".join(lines) + "\n")
66
67
68def main():
69 try:
70 user = os.environ.get("PR_USER", "contributor")
71 pr_number = os.environ.get("PR_NUMBER", "")
72 run_id = os.environ.get("RUN_ID", "")
73
74 os.makedirs(OUTPUT_DIR, exist_ok=True)
75
76 if pr_number:
77 with open(os.path.join(OUTPUT_DIR, "number.txt"), "w") as f:
78 f.write(pr_number)
79 if run_id:
80 with open(os.path.join(OUTPUT_DIR, "run-id.txt"), "w") as f:
81 f.write(run_id)
82
83 findings = collect_findings()
84 write_step_summary(findings)
85
86 if findings:
87 comment = format_comment(findings, user)
88 with open(os.path.join(OUTPUT_DIR, "comment.md"), "w") as f:
89 f.write(comment)
90 except Exception:
91 pass
92
93 sys.exit(0)
94
95
96if __name__ == "__main__":
97 main()