-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproces_coverage_report.py
More file actions
86 lines (66 loc) · 2.69 KB
/
proces_coverage_report.py
File metadata and controls
86 lines (66 loc) · 2.69 KB
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
#!/usr/bin/env python
import sys
import xml.etree.ElementTree as ET
from typing import Any
from codegen import Agent
from generate_codecov_agent_prompt import generate_codecov_agent_prompt
ORG_ID = 1
def parse_coverage_xml(xml_file: str) -> dict[str, Any]:
"""Parse the coverage.xml file and extract relevant information.
Args:
xml_file: Path to the coverage XML file.
Returns:
Dictionary containing parsed coverage data.
"""
try:
tree = ET.parse(xml_file)
root = tree.getroot()
# Extract overall coverage statistics
coverage_data = {
"version": root.get("version"),
"timestamp": root.get("timestamp"),
"lines_covered": int(root.get("lines-covered", 0)),
"lines_valid": int(root.get("lines-valid", 0)),
"line_rate": float(root.get("line-rate", 0)),
"branches_covered": int(root.get("branches-covered", 0)),
"branches_valid": int(root.get("branches-valid", 0)),
"branch_rate": float(root.get("branch-rate", 0)),
"complexity": float(root.get("complexity", 0)),
}
# Calculate overall coverage percentage
if coverage_data["lines_valid"] > 0:
coverage_data["coverage_percentage"] = coverage_data["lines_covered"] / coverage_data["lines_valid"] * 100
else:
coverage_data["coverage_percentage"] = 0
return coverage_data
except Exception as e:
print(f"Error parsing coverage XML: {e}")
return {}
def main():
"""Main function to process the coverage report."""
if len(sys.argv) < 5:
print("Usage: python process_coverage_report.py <coverage_xml_file> <pr_number> <repo> <token>")
sys.exit(1)
xml_file = sys.argv[1]
pr_number = sys.argv[2]
repo = sys.argv[3]
token = sys.argv[4]
coverage_data = parse_coverage_xml(xml_file)
coverage_data["pr_number"] = pr_number
coverage_data["repo"] = repo
coverage_data["token"] = token
if not coverage_data:
print("Failed to parse coverage data")
sys.exit(1)
# Example: Check if coverage meets a threshold
threshold = 77 # 77% coverage threshold
if coverage_data["coverage_percentage"] < threshold:
print(f"\nWARNING: Coverage {coverage_data['coverage_percentage']:.2f}% is below threshold of {threshold}%")
print("Agent will be notified.")
new_agent = Agent(token=token, org_id=ORG_ID)
second_task = new_agent.run(generate_codecov_agent_prompt(pr_number, repo))
print(f"Agent has been notified. URL: {second_task.web_url}")
else:
print("Coverage is above threshold.")
if __name__ == "__main__":
main()