mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-05 05:10:38 +08:00
Add diff code generation for Bitbucket code suggestions and improve logging
This commit is contained in:
@ -1,4 +1,6 @@
|
|||||||
|
import difflib
|
||||||
import json
|
import json
|
||||||
|
import re
|
||||||
from typing import Optional, Tuple
|
from typing import Optional, Tuple
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
@ -72,24 +74,38 @@ class BitbucketProvider(GitProvider):
|
|||||||
post_parameters_list = []
|
post_parameters_list = []
|
||||||
for suggestion in code_suggestions:
|
for suggestion in code_suggestions:
|
||||||
body = suggestion["body"]
|
body = suggestion["body"]
|
||||||
|
original_suggestion = suggestion.get('original_suggestion', None) # needed for diff code
|
||||||
|
if original_suggestion:
|
||||||
|
try:
|
||||||
|
existing_code = original_suggestion['existing_code'].rstrip() + "\n"
|
||||||
|
improved_code = original_suggestion['improved_code'].rstrip() + "\n"
|
||||||
|
diff = difflib.unified_diff(existing_code.split('\n'),
|
||||||
|
improved_code.split('\n'), n=999)
|
||||||
|
patch_orig = "\n".join(diff)
|
||||||
|
patch = "\n".join(patch_orig.splitlines()[5:]).strip('\n')
|
||||||
|
diff_code = f"\n\n```diff\n{patch.rstrip()}\n```"
|
||||||
|
# replace ```suggestion ... ``` with diff_code, using regex:
|
||||||
|
body = re.sub(r'```suggestion.*?```', diff_code, body, flags=re.DOTALL)
|
||||||
|
except Exception as e:
|
||||||
|
get_logger().exception(f"Bitbucket failed to get diff code for publishing, error: {e}")
|
||||||
|
continue
|
||||||
|
|
||||||
relevant_file = suggestion["relevant_file"]
|
relevant_file = suggestion["relevant_file"]
|
||||||
relevant_lines_start = suggestion["relevant_lines_start"]
|
relevant_lines_start = suggestion["relevant_lines_start"]
|
||||||
relevant_lines_end = suggestion["relevant_lines_end"]
|
relevant_lines_end = suggestion["relevant_lines_end"]
|
||||||
|
|
||||||
if not relevant_lines_start or relevant_lines_start == -1:
|
if not relevant_lines_start or relevant_lines_start == -1:
|
||||||
if get_settings().config.verbosity_level >= 2:
|
get_logger().exception(
|
||||||
get_logger().exception(
|
f"Failed to publish code suggestion, relevant_lines_start is {relevant_lines_start}"
|
||||||
f"Failed to publish code suggestion, relevant_lines_start is {relevant_lines_start}"
|
)
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if relevant_lines_end < relevant_lines_start:
|
if relevant_lines_end < relevant_lines_start:
|
||||||
if get_settings().config.verbosity_level >= 2:
|
get_logger().exception(
|
||||||
get_logger().exception(
|
f"Failed to publish code suggestion, "
|
||||||
f"Failed to publish code suggestion, "
|
f"relevant_lines_end is {relevant_lines_end} and "
|
||||||
f"relevant_lines_end is {relevant_lines_end} and "
|
f"relevant_lines_start is {relevant_lines_start}"
|
||||||
f"relevant_lines_start is {relevant_lines_start}"
|
)
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if relevant_lines_end > relevant_lines_start:
|
if relevant_lines_end > relevant_lines_start:
|
||||||
@ -113,8 +129,7 @@ class BitbucketProvider(GitProvider):
|
|||||||
self.publish_inline_comments(post_parameters_list)
|
self.publish_inline_comments(post_parameters_list)
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if get_settings().config.verbosity_level >= 2:
|
get_logger().error(f"Bitbucket failed to publish code suggestion, error: {e}")
|
||||||
get_logger().error(f"Failed to publish code suggestion, error: {e}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def publish_file_comments(self, file_comments: list) -> bool:
|
def publish_file_comments(self, file_comments: list) -> bool:
|
||||||
@ -122,7 +137,7 @@ class BitbucketProvider(GitProvider):
|
|||||||
|
|
||||||
def is_supported(self, capability: str) -> bool:
|
def is_supported(self, capability: str) -> bool:
|
||||||
if capability in ['get_issue_comments', 'publish_inline_comments', 'get_labels', 'gfm_markdown',
|
if capability in ['get_issue_comments', 'publish_inline_comments', 'get_labels', 'gfm_markdown',
|
||||||
'publish_file_comments']:
|
'publish_file_comments']:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import difflib
|
||||||
|
import re
|
||||||
|
|
||||||
from packaging.version import parse as parse_version
|
from packaging.version import parse as parse_version
|
||||||
from typing import Optional, Tuple
|
from typing import Optional, Tuple
|
||||||
from urllib.parse import quote_plus, urlparse
|
from urllib.parse import quote_plus, urlparse
|
||||||
@ -67,24 +70,37 @@ class BitbucketServerProvider(GitProvider):
|
|||||||
post_parameters_list = []
|
post_parameters_list = []
|
||||||
for suggestion in code_suggestions:
|
for suggestion in code_suggestions:
|
||||||
body = suggestion["body"]
|
body = suggestion["body"]
|
||||||
|
original_suggestion = suggestion.get('original_suggestion', None) # needed for diff code
|
||||||
|
if original_suggestion:
|
||||||
|
try:
|
||||||
|
existing_code = original_suggestion['existing_code'].rstrip() + "\n"
|
||||||
|
improved_code = original_suggestion['improved_code'].rstrip() + "\n"
|
||||||
|
diff = difflib.unified_diff(existing_code.split('\n'),
|
||||||
|
improved_code.split('\n'), n=999)
|
||||||
|
patch_orig = "\n".join(diff)
|
||||||
|
patch = "\n".join(patch_orig.splitlines()[5:]).strip('\n')
|
||||||
|
diff_code = f"\n\n```diff\n{patch.rstrip()}\n```"
|
||||||
|
# replace ```suggestion ... ``` with diff_code, using regex:
|
||||||
|
body = re.sub(r'```suggestion.*?```', diff_code, body, flags=re.DOTALL)
|
||||||
|
except Exception as e:
|
||||||
|
get_logger().exception(f"Bitbucket failed to get diff code for publishing, error: {e}")
|
||||||
|
continue
|
||||||
relevant_file = suggestion["relevant_file"]
|
relevant_file = suggestion["relevant_file"]
|
||||||
relevant_lines_start = suggestion["relevant_lines_start"]
|
relevant_lines_start = suggestion["relevant_lines_start"]
|
||||||
relevant_lines_end = suggestion["relevant_lines_end"]
|
relevant_lines_end = suggestion["relevant_lines_end"]
|
||||||
|
|
||||||
if not relevant_lines_start or relevant_lines_start == -1:
|
if not relevant_lines_start or relevant_lines_start == -1:
|
||||||
if get_settings().config.verbosity_level >= 2:
|
get_logger().warning(
|
||||||
get_logger().exception(
|
f"Failed to publish code suggestion, relevant_lines_start is {relevant_lines_start}"
|
||||||
f"Failed to publish code suggestion, relevant_lines_start is {relevant_lines_start}"
|
)
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if relevant_lines_end < relevant_lines_start:
|
if relevant_lines_end < relevant_lines_start:
|
||||||
if get_settings().config.verbosity_level >= 2:
|
get_logger().warning(
|
||||||
get_logger().exception(
|
f"Failed to publish code suggestion, "
|
||||||
f"Failed to publish code suggestion, "
|
f"relevant_lines_end is {relevant_lines_end} and "
|
||||||
f"relevant_lines_end is {relevant_lines_end} and "
|
f"relevant_lines_start is {relevant_lines_start}"
|
||||||
f"relevant_lines_start is {relevant_lines_start}"
|
)
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if relevant_lines_end > relevant_lines_start:
|
if relevant_lines_end > relevant_lines_start:
|
||||||
|
Reference in New Issue
Block a user