Skip to content

Commit

Permalink
Merge pull request #541 from Cloud-Code-AI/540-update-code-review-pro…
Browse files Browse the repository at this point in the history
…mpt-and-the-diff-transformation

feat: updated the parser format
  • Loading branch information
sauravpanda authored Sep 14, 2024
2 parents 49bbe7b + d3692b9 commit 9f6540c
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 200 deletions.
1 change: 0 additions & 1 deletion .github/workflows/kaizen-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ jobs:
- name: Run Kaizen Unit Tests
uses: Cloud-Code-AI/kaizen-unittest-runner-action@v1.0.9
continue-on-error: true
id: kaizen_tests

- name: Generate test report
Expand Down
2 changes: 1 addition & 1 deletion kaizen/generator/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def _write_test_file(self, test_file_path, test_code):
print("• Writing test file...")
with open(test_file_path, "w") as test_file:
test_file.write(test_code)
print(f" ✓ Test file written successfully")
print(" ✓ Test file written successfully")
self.log_step("Write test file", f"Test file written to: {test_file_path}")

def update_usage(self, usage):
Expand Down
4 changes: 2 additions & 2 deletions kaizen/helpers/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_parent_folder():

def create_folder(folder_path):
if not folder_path:
raise ValueError(f"Folder path cannot be empty")
raise ValueError("Folder path cannot be empty")
if not os.path.exists(folder_path):
os.makedirs(folder_path)
logger.debug(f"Folder '{folder_path}' created successfully.")
Expand All @@ -129,7 +129,7 @@ def create_test_files(json_tests, folder_path):
with open(file_path, "w") as f:
cleaned_code = general.clean_python_code(test["code"])
if not cleaned_code:
logger.info(f"Failed to clean code")
logger.info("Failed to clean code")
else:
cleaned_code = (
f"'''Importance: {module['importance']}\
Expand Down
83 changes: 39 additions & 44 deletions kaizen/helpers/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,99 +161,94 @@ def patch_to_numbered_lines(patch_text):


def format_change(old_num, new_num, change_type, content, ignore_deletions=False):
old_num_str = f"{old_num:<5}" if old_num is not None else "_ "
new_num_str = f"{new_num:<5}" if new_num is not None else "_ "
if ignore_deletions:
old_num_str = "_ "
return f"{old_num_str} {new_num_str} {change_type} {content}"
return f"[LINE {new_num_str}] [{change_type}] {content}"


def patch_to_combined_chunks(patch_text, ignore_deletions=False):
lines = patch_text.split("\n")
patch_text = patch_text.replace("\r\n", "\n")
lines = patch_text.splitlines(keepends=True)
changes = []
metadata = []
removal_line_num = 0
addition_line_num = 0
unedited_removal_num = 0
unedited_addition_num = 0
unedited_count = 0
current_hunk = None
is_diff = False
first_transition = True
removed_hunk = False
current_file_name = ""

for line in lines:
line = line.rstrip("\n")
if "diff --git" in line:
is_diff = True
if not first_transition:
changes.append("\n</change_block>\n\n")
first_transition = False

if not removed_hunk:
changes = []
removed_hunk = True
changes.append("\n")
elif is_diff:
is_diff = False
elif line.startswith("@"):
if current_hunk:
metadata.append(current_hunk)
current_hunk = line
match = re.match(r"@@ -(\d+),\d+ \+(\d+),\d+ @@", line)
elif line.startswith("@@"):
match = re.match(r"@@ -(\d+),\d+ \+(\d+),\d+ @@ (.*)", line)
if match:
removal_line_num = int(match.group(1))
addition_line_num = int(match.group(2))
unedited_removal_num = removal_line_num
unedited_addition_num = addition_line_num
if changes and "\n<change_block>" not in changes:
changes = []
changes.append("\n<change_block>")
content = match.group(3)
changes.append("\n")
changes.append(
f"\n<file_start>\nFilename: {current_file_name}\n<file_end>\n"
format_change(
None, addition_line_num, "CONTEXT", content, ignore_deletions
)
)
elif line.startswith("index "):
continue
elif line.startswith("---"):
line = line.replace("a/", "").replace("b/", "").replace("--- ", "")
elif line.startswith("--- a"):
line = line.replace("--- a/", "")
current_file_name = line
elif line.startswith("+++"):
line = line.replace("a/", "").replace("b/", "").replace("+++ ", "")
if (
current_file_name != "/dev/null"
and changes
and "[FILE_START]" not in changes[-1]
):
changes.append("\n[FILE_END]\n")
changes.append(f"\n[FILE_START] {current_file_name}\n")
elif line.startswith("+++ b"):
line = line.replace("+++ b/", "")
current_file_name = line
if (
current_file_name != "/dev/null"
and changes
and "[FILE_START]" not in changes[-1]
):
changes.append("\n[FILE_END]\n")
changes.append(f"\n[FILE_START] {current_file_name}\n")
elif line.startswith("-"):
content = line[1:]
if not ignore_deletions:
changes.append(
format_change(
removal_line_num, None, "-1:[-]", content, ignore_deletions
None, removal_line_num, "REMOVED", content, ignore_deletions
)
)
removal_line_num += 1
unedited_removal_num = removal_line_num
elif line.startswith("+"):
content = line[1:]
changes.append(
format_change(
None, addition_line_num, "+1:[+]", content, ignore_deletions
None, addition_line_num, "UPDATED", content, ignore_deletions
)
)
addition_line_num += 1
unedited_addition_num = addition_line_num
else:
content = line
changes.append(
format_change(
unedited_removal_num,
unedited_addition_num,
" 0:[.]",
None,
addition_line_num,
"CONTEXT",
content,
ignore_deletions,
)
)
unedited_removal_num += 1
unedited_addition_num += 1
removal_line_num += 1
addition_line_num += 1
unedited_count += 1

if current_hunk:
metadata.append(current_hunk)
changes.append("\n</change_block>\n\n")

output = changes

Expand Down
Loading

0 comments on commit 9f6540c

Please sign in to comment.