Skip to content

Commit

Permalink
[cli] Fix gerrit output
Browse files Browse the repository at this point in the history
- Use source line in the gerrit output instead of the line number at the
end of the report message.
- If a report not found in the changed files list, add report to the main
message.
  • Loading branch information
csordasmarton committed Jul 12, 2021
1 parent 53fc3be commit d48c4e1
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 98 deletions.
45 changes: 24 additions & 21 deletions codechecker_common/output/gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __convert_reports(reports: List[Report],
review_comments = {}

report_count = 0
report_messages_in_unchanged_files = []
for report in reports:
bug_line = report.line
bug_col = report.col
Expand All @@ -86,29 +87,30 @@ def __convert_reports(reports: List[Report],
severity = severity_map.get(check_name, "UNSPECIFIED")
file_name = report.file_path
check_msg = report.description
source_line = report.line

# Skip the report if it is not in the changed files.
if changed_file_path and not \
any([file_name.endswith(c) for c in changed_files]):
LOG.debug("Skip report from '%s' file because this file has not "
"changed.", file_name)
continue
source_line = report.source_line

report_count += 1

# file_name can be without a path in the report.
rel_file_path = os.path.relpath(file_name, repo_dir) \
if repo_dir and os.path.dirname(file_name) != "" else file_name

checked_file = rel_file_path \
+ ':' + str(bug_line) + ":" + str(bug_col)

review_comment_msg = \
f"[{severity}] {checked_file}: {check_msg} [{check_name}]\n" \
f"{source_line}"

# Skip the report if it is not in the changed files.
if changed_file_path and not \
any([file_name.endswith(c) for c in changed_files]):
report_messages_in_unchanged_files.append(review_comment_msg)
continue

if rel_file_path not in review_comments:
review_comments[rel_file_path] = []

review_comment_msg = "[{0}] {1}: {2} [{3}]\n{4}".format(
severity, checked_file, check_msg, check_name, source_line)

review_comments[rel_file_path].append({
"range": {
"start_line": bug_line,
Expand All @@ -117,8 +119,13 @@ def __convert_reports(reports: List[Report],
"end_character": bug_col},
"message": review_comment_msg})

message = "CodeChecker found {0} issue(s) in the code.".format(
report_count)
message = f"CodeChecker found {report_count} issue(s) in the code."

if report_messages_in_unchanged_files:
message += ("\n\nThere following reports are introduced in files "
"which are not changed and can't be shown as individual "
"reports:\n{0}\n".format('\n'.join(
report_messages_in_unchanged_files)))

if report_url:
message += " See: '{0}'".format(report_url)
Expand All @@ -144,19 +151,17 @@ def __get_changed_files(changed_file_path: Union[None, str]) -> List[str]:
changed_files = []

if not changed_file_path or not os.path.exists(changed_file_path):
LOG.warning("CC_CHANGED_FILES is specified but the file (%s) doesn't "
"exist.", changed_file_path)
return changed_files

with open(changed_file_path, encoding='utf-8', errors='ignore') as f:
content = f.read()
with open(changed_file_path,
encoding='utf-8',
errors='ignore') as changed_file:
content = changed_file.read()

# The file can contain some garbage values at start, so we use
# regex search to find a json object.
match = re.search(r'\{[\s\S]*\}', content)
if not match:
LOG.debug("The content of the given changed file (%s) is invalid!",
changed_file_path)
return changed_files

for filename in json.loads(match.group(0)):
Expand All @@ -165,6 +170,4 @@ def __get_changed_files(changed_file_path: Union[None, str]) -> List[str]:

changed_files.append(filename)

LOG.debug("Changed file paths: %s", changed_files)

return changed_files
4 changes: 4 additions & 0 deletions codechecker_common/tests/unit/test_files/lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
double foo(int param)
{
return 1 / param;
}
4 changes: 4 additions & 0 deletions codechecker_common/tests/unit/test_files/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main()
{
sizeof(42);
}
Loading

0 comments on commit d48c4e1

Please sign in to comment.