|
1 | 1 | import csv |
| 2 | +import json |
2 | 3 | import os |
3 | | -import pathlib |
4 | 4 | import subprocess |
5 | 5 | import sys |
6 | | -import typing |
7 | 6 |
|
8 | 7 | import openai |
9 | 8 | from csvtomd import csv_to_table, md_table |
10 | 9 | from loguru import logger |
11 | | -from pydantic import parse_file_as |
12 | 10 |
|
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 |
15 | 13 |
|
16 | 14 | user_dir = "/github/workspace" |
17 | 15 | support_langs = {"golang", "python"} |
@@ -64,6 +62,8 @@ def gen_diff(before_sha: str, after_sha: str): |
64 | 62 | after_sha, |
65 | 63 | "--outputCsv", |
66 | 64 | csv_result_file, |
| 65 | + "--outputJson", |
| 66 | + json_result_file, |
67 | 67 | ] |
68 | 68 | ) |
69 | 69 |
|
@@ -97,31 +97,39 @@ def convert_csv_to_md(csv_file) -> str: |
97 | 97 | return md_table_raw |
98 | 98 |
|
99 | 99 |
|
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}) |
109 | 105 |
|
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})" |
113 | 109 |
|
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: |
115 | 122 | writer = csv.DictWriter(f, fieldnames=cols) |
116 | 123 | 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)}%" |
123 | 124 |
|
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) |
125 | 133 |
|
126 | 134 |
|
127 | 135 | def main(): |
@@ -164,11 +172,11 @@ def main(): |
164 | 172 | logger.info(f"ai resp: {ai_content}") |
165 | 173 |
|
166 | 174 | repo_name = os.getenv("GITHUB_REPOSITORY") |
167 | | - process_csv(csv_result_file) |
| 175 | + process_json(csv_result_file) |
168 | 176 | md_table_raw = convert_csv_to_md(csv_result_file) |
169 | 177 |
|
170 | 178 | final_content = f""" |
171 | | -## DiffCtx Report |
| 179 | +## [DiffCtx](https://github.com/williamfzc/diffctx) Report |
172 | 180 |
|
173 | 181 | {md_table_raw} |
174 | 182 | """ |
|
0 commit comments