Skip to content

Commit 01ae24d

Browse files
committed
feat: better csv output
1 parent 7ebdae3 commit 01ae24d

2 files changed

Lines changed: 61 additions & 27 deletions

File tree

main.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import csv
2+
import json
23
import os
3-
import pathlib
44
import subprocess
55
import sys
6-
import typing
76

87
import openai
98
from csvtomd import csv_to_table, md_table
109
from loguru import logger
11-
from pydantic import parse_file_as
1210

13-
from comment import send_comment, send_code_comments
14-
from object import LineStat
11+
from comment import send_comment
12+
from object import FileList
1513

1614
user_dir = "/github/workspace"
1715
support_langs = {"golang", "python"}
@@ -64,6 +62,8 @@ def gen_diff(before_sha: str, after_sha: str):
6462
after_sha,
6563
"--outputCsv",
6664
csv_result_file,
65+
"--outputJson",
66+
json_result_file,
6767
]
6868
)
6969

@@ -97,31 +97,39 @@ def convert_csv_to_md(csv_file) -> str:
9797
return md_table_raw
9898

9999

100-
def process_csv(csv_file):
101-
cols = [
102-
"fileName",
103-
"affectedLinePercent",
104-
"affectedFunctionPercent",
105-
"affectedReferencePercent",
106-
]
107-
origin_cols = cols[:1]
108-
data_cols = cols[1:]
100+
def process_json(input_json, output_csv):
101+
with open(input_json, "r") as f:
102+
json_data = json.load(f)
103+
104+
file_list = FileList.parse_obj({"files": json_data})
109105

110-
with open(csv_file, newline='') as f:
111-
reader = csv.DictReader(f)
112-
rows = list(reader)
106+
def format_percentage(numerator, denominator):
107+
percent = numerator / denominator * 100
108+
return f"{percent:.2f}% ({numerator}/{denominator})"
113109

114-
with open(csv_file, "w", newline='') as f:
110+
for file in file_list.files:
111+
file.affectedLinePercentRepr = format_percentage(file.affectedLines, file.totalLines)
112+
file.affectedFunctionPercentRepr = format_percentage(file.affectedFunctions, file.totalFunctions)
113+
file.affectedReferencePercentRepr = format_percentage(file.affectedReferences, file.totalReferences)
114+
115+
cols = [
116+
"FileName",
117+
"AffectedLines",
118+
"AffectedFunctions",
119+
"AffectedReferences",
120+
]
121+
with open(output_csv, "w", newline='') as f:
115122
writer = csv.DictWriter(f, fieldnames=cols)
116123
writer.writeheader()
117-
for each_row in rows:
118-
row_dict = dict()
119-
for each_col in origin_cols:
120-
row_dict[each_col] = each_row[each_col]
121-
for each_col in data_cols:
122-
row_dict[each_col] = f"{round(float(each_row[each_col]) * 100, 2)}%"
123124

124-
writer.writerow(row_dict)
125+
for file in file_list.files:
126+
row = {
127+
cols[0]: file.fileName,
128+
cols[1]: file.affectedLinePercentRepr,
129+
cols[2]: file.affectedFunctionPercentRepr,
130+
cols[3]: file.affectedReferencePercentRepr,
131+
}
132+
writer.writerow(row)
125133

126134

127135
def main():
@@ -164,11 +172,11 @@ def main():
164172
logger.info(f"ai resp: {ai_content}")
165173

166174
repo_name = os.getenv("GITHUB_REPOSITORY")
167-
process_csv(csv_result_file)
175+
process_json(csv_result_file)
168176
md_table_raw = convert_csv_to_md(csv_result_file)
169177

170178
final_content = f"""
171-
## DiffCtx Report
179+
## [DiffCtx](https://github.com/williamfzc/diffctx) Report
172180
173181
{md_table_raw}
174182
"""

object.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import typing
2+
13
from pydantic import BaseModel, Field
24

35

@@ -18,3 +20,27 @@ class LineStat(BaseModel):
1820
lineNumber: int
1921
refScope: ReferenceScope = Field(alias="ref")
2022
funcScope: FuncReferenceScope = Field(alias="funcRef")
23+
24+
25+
class FileVertex(BaseModel):
26+
fileName: str
27+
affectedLinePercent: float
28+
affectedFunctionPercent: float
29+
affectedReferencePercent: float
30+
31+
affectedLinePercentRepr: str = ""
32+
affectedFunctionPercentRepr: str = ""
33+
affectedReferencePercentRepr: str = ""
34+
35+
affectedLines: int
36+
totalLines: int
37+
38+
affectedFunctions: int
39+
totalFunctions: int
40+
41+
affectedReferences: int
42+
totalReferences: int
43+
44+
45+
class FileList(BaseModel):
46+
files: typing.List[FileVertex]

0 commit comments

Comments
 (0)