Skip to content

Commit 3b8784f

Browse files
committed
chore: sync with 0.4.1 srctx
1 parent a60a576 commit 3b8784f

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM williamfzc/srctx:v0.3.3
1+
FROM williamfzc/srctx:v0.4.1
22

33
RUN apk add --no-cache python3 py3-pip
44

main.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import csv
12
import os
23
import pathlib
34
import subprocess
@@ -63,8 +64,6 @@ def gen_diff(before_sha: str, after_sha: str):
6364
after_sha,
6465
"--outputCsv",
6566
csv_result_file,
66-
"--outputJson",
67-
json_result_file,
6867
]
6968
)
7069

@@ -73,20 +72,9 @@ def process_with_ai(raw: str) -> str:
7372
req = f"""
7473
You are a bot for helping code review.
7574
Standard csv report was generated from diff analysis tool.
76-
Some descriptions:
77-
78-
- it contains all the influenced lines by this commit
79-
- each row = each line
80-
- RefScope.TotalRefCount: total variable references in this line
81-
- RefScope.CrossFileRefCount: variable references by other files
82-
- RefScope.CrossDirRefCount: variable references by other directories
83-
- FuncRefScope.TotalFuncRefCount: total functions influenced by this line
84-
- FuncRefScope.CrossFuncFileRefCount: functions in other files influenced by this line
85-
- FUncRefScope.crossFuncDirRefCount: functions in other dirs influenced by this line
8675
8776
Here is a csv report below for a specific commit.
8877
Evaluate it and indicate the most important parts which reviewers should care.
89-
All the file paths should be wrapped with markdown link for navigation.
9078
Empty report means that there are no dangerous changes.
9179
9280
--- report start ---
@@ -109,6 +97,33 @@ def convert_csv_to_md(csv_file) -> str:
10997
return md_table_raw
11098

11199

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:]
109+
110+
with open(csv_file, newline='') as f:
111+
reader = csv.DictReader(f)
112+
rows = list(reader)
113+
114+
with open(csv_file, "w", newline='') as f:
115+
writer = csv.DictWriter(f, fieldnames=cols)
116+
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+
writer.writerow(row_dict)
125+
126+
112127
def main():
113128
args = sys.argv[1:]
114129
lang = args[0]
@@ -137,27 +152,25 @@ def main():
137152
with open(csv_result_file, encoding="utf-8") as f:
138153
content = f.read()
139154

140-
ai_content = ""
155+
ai_content = "-"
141156
if not openai_api_key:
142157
logger.warning("no openai api key found. Use raw data.")
143158
else:
144159
logger.info("process with openai")
145160
openai.api_key = openai_api_key
146161
ai_content = process_with_ai(content)
147162

163+
# todo: add to feedback?
164+
logger.info(f"ai resp: {ai_content}")
165+
148166
repo_name = os.getenv("GITHUB_REPOSITORY")
167+
process_csv(csv_result_file)
149168
md_table_raw = convert_csv_to_md(csv_result_file)
150169

151170
final_content = f"""
152171
## DiffCtx Report
153172
154-
{ai_content}
155-
156-
<details><summary>Details</summary>
157-
158173
{md_table_raw}
159-
160-
</details>
161174
"""
162175
logger.info(f"final comment: {final_content}")
163176

0 commit comments

Comments
 (0)