From 0c19ab5c2b2d673854a553decd491d52f5c33325 Mon Sep 17 00:00:00 2001 From: kaede10 Date: Tue, 28 Nov 2023 16:51:45 +0800 Subject: [PATCH] update comment line number --- pr_review/src/gitee/gitee_api.py | 2 +- pr_review/src/review_code/prompts.py | 2 +- pr_review/src/review_code/review.py | 74 +++++++--------------------- 3 files changed, 21 insertions(+), 57 deletions(-) diff --git a/pr_review/src/gitee/gitee_api.py b/pr_review/src/gitee/gitee_api.py index c217401..1d45acc 100644 --- a/pr_review/src/gitee/gitee_api.py +++ b/pr_review/src/gitee/gitee_api.py @@ -92,7 +92,7 @@ class GiteeApi(GiteeCaller): url = f'{self.gitee_url}/{self.owner}/{self.repo}/compare/{formerSha}...{latterSha}' params = { 'access_token': self.access_token, - 'straight': True, + 'straight': False, } return json.loads(requests.get(url = url, params=params).content.decode('utf-8')) diff --git a/pr_review/src/review_code/prompts.py b/pr_review/src/review_code/prompts.py index a931eaf..fc34dc6 100644 --- a/pr_review/src/review_code/prompts.py +++ b/pr_review/src/review_code/prompts.py @@ -89,7 +89,7 @@ class Prompts: LGTM! --- - ## Changes made to \`$filename\` for your review + ## Changes made to `$filename` for your review $patches ''' diff --git a/pr_review/src/review_code/review.py b/pr_review/src/review_code/review.py index 0126931..4268912 100644 --- a/pr_review/src/review_code/review.py +++ b/pr_review/src/review_code/review.py @@ -74,6 +74,7 @@ class CodeReview: # 比较highrestReviewedCommitId及headSha incrementalDiff = self.giteeApi.compare(highrestReviewedCommitId, headSha) targetBranchDiff = self.giteeApi.compare(baseSha, headSha) + logger.info(f'baseSha = {baseSha}, headSha = {headSha}') incrementalFiles = incrementalDiff.get('files', None) targetBranchFiles = targetBranchDiff.get('files', None) @@ -134,7 +135,10 @@ class CodeReview: except Exception as e: logger.warning('failed to get file contents: %s'%(e)) - fileDiff = aFile.get('patch', '') + fileDiff = aFile.get('patch') + if not aFile.get('patch'): + logger.info(f"{aFile.get('filename')} has no patch") + continue file_comment_line[aFile.get('filename')] = self.get_diff_new_line_dic(file_patches=fileDiff) patches = [] diff_num = 0 @@ -152,6 +156,7 @@ class CodeReview: if len(patches) > 0: filteredFiles.append([aFile.get('filename', None), fileContent, fileDiff, patches]) + logger.info(file_comment_line) filesAndChanges = filteredFiles if len(filesAndChanges) == 0: logger.error('skipped: no files to review') @@ -164,33 +169,6 @@ class CodeReview: lgtm_num = self.do_review(filename, self.input, patches, file_comment_line[filename]) if lgtm_num == len(patches): self.giteeApi.submit_review(body = "There is no issue found", commitId = self.commits[0], filename=filename, line=file_comment_line[filename].get(patches[-1][1])) - - def get_patch_diff_line(self, diff_num, diff_lines, file_diff): - line_no = 0 - hit_no = 0 - diff_lines = self.reduce(diff_lines) - for line in diff_lines: - if line.startswith('@@'): - if hit_no == diff_num and self.modify_line(file_diff): - return line_no - 1 - elif hit_no == diff_num: - return line_no - hit_no += 1 - line_no += 1 - if self.only_add_or_delate(diff_lines, '-') or self.only_add_or_delate(diff_lines, '+'): - line_no -= 1 - elif self.modify_line(file_diff): - line_no -= 1 - return line_no - - # 判断第一个修改行在文件中的位置 - # 如果在前四行有修改,则在gitee上不会显示 @@ -0,0 +0,0 @@ - # 则行号应该减1 - def modify_line(self, file_diff): - re_split = re.split('^@@ -(\d+),(\d+) \+(\d+),(\d+) @@', file_diff) - if len(re_split) > 1 and int(re_split[1]) == 1: - return True - return False def splitPatch(self, patch): if not patch: @@ -213,23 +191,6 @@ class CodeReview: results.append('\n'.join(splitLines[lastLine:])) return results - def only_add_or_delate(self, diff_lines, operator): - for file_diff in diff_lines: - if not file_diff.startswith(operator) and not file_diff.startswith('@@'): - return False - return True - - # 处理 \ No newline at end of file - def reduce(self, diff_lines): - ans = [] - line = 0 - for file_diff in diff_lines: - line += 1 - if '\\ No newline at end of file' == file_diff and line == len(diff_lines): - continue - ans.append(file_diff) - return ans - def patchStartEndLine(self, patch): reSplit = re.split('^@@ -(\d+),(\d+) \+(\d+),(\d+) @@', patch) if len(reSplit) > 1: @@ -370,24 +331,21 @@ class CodeReview: lines = diff.split('\n') line_numbers = {} - current_line_number = 0 new_line = 0 - line_numbers.update({patch_lines['newHunk']['startLine']: 2}) - for line in lines: current_line_number += 1 if line.startswith('@@'): - current_line_number += 1 continue if line.startswith('-'): continue + if line == '\\ No newline at end of file': + continue else: - # Context line, increment line number - new_line += 1 line_numbers.update({patch_lines['newHunk']['startLine'] + new_line: current_line_number}) - + new_line += 1 + return line_numbers def get_diff_new_line_dic(self, file_patches): @@ -399,7 +357,7 @@ class CodeReview: for patch in patches: patch_lines = self.patchStartEndLine(patch) diff_new_line_dic.update(self.delta(self.parse(patch, patch_lines), step[i])) - end_line = patch_lines['newHunk']['startLine'] + patch_lines['newHunk']['endLine'] + end_line = patch_lines['newHunk']['startLine'] + patch_lines['newHunk']['endLine'] - 1 step.append(diff_new_line_dic.get(end_line)) i += 1 except Exception as e: @@ -408,9 +366,15 @@ class CodeReview: # If there are modifications in the first four lines, # the first line "@@" will not be displayed, # all line numbers need to be decremented by 1. - if self.modify_line(file_patches): + if self.modifications_position(file_patches): for key in diff_new_line_dic: diff_new_line_dic.update({key: diff_new_line_dic[key] - 1}) - logger.info(f'file_line : diff_line ====> {diff_new_line_dic}') return diff_new_line_dic + + # Modifications were made to the first four lines of the file or not + def modifications_position(self, patch): + patch_lines = self.patchStartEndLine(patch) + if patch_lines['newHunk']['startLine'] <= 4 or patch_lines['newHunk']['startLine'] <= 4: + return True + return False \ No newline at end of file -- Gitee