diff --git a/.github/code_analysis.py b/.github/code_analysis.py index 8f08784c..83d7a82e 100644 --- a/.github/code_analysis.py +++ b/.github/code_analysis.py @@ -8,7 +8,7 @@ # Input variables from Github action GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") PR_NUM = os.getenv("PR_NUMBER", "-1") -WORK_DIR = os.getenv("GITHUB_WORKSPACE") +WORK_DIR = f'{os.getenv("GITHUB_WORKSPACE")}' REPO_NAME = os.getenv("REPO") TARGET_REPO_NAME = os.getenv("REPO", "") SHA = os.getenv("GITHUB_SHA") @@ -207,9 +207,10 @@ def is_excluded_dir(line): if not exclude_dir: return False - debug_print(f"{line} and {exclude_dir} with result {line.startswith(exclude_dir)}") + excluded_dir = f"{WORK_DIR}/{exclude_dir}" + debug_print(f"{line} and {excluded_dir} with result {line.startswith(excluded_dir)}") - return line.startswith(exclude_dir) + return line.startswith(excluded_dir) def get_file_line_end(file_in, file_line_start_in): @@ -354,12 +355,13 @@ def generate_output(is_note, file_path, file_line_start, file_line_end, descript return new_line -def extract_info(line): +def extract_info(line, prefix): """ Extracts information from a given line containing file path, line number, and issue description. Args: - line (str): The input string containing file path, line number, and issue description. + - prefix (str): The prefix to remove from the start of the file path in the line. - was_note (bool): Indicates if the previous issue was a note. - output_string (str): The string containing previous output information. @@ -372,6 +374,9 @@ def extract_info(line): - file_line_end (int): The ending line number of the issue. """ + # Clean up line + line = line.replace(prefix, "").lstrip("/") + # Get the line starting position /path/to/file:line and trim it file_path_end_idx = line.index(":") file_path = line[:file_path_end_idx] @@ -425,12 +430,13 @@ def append_issue(is_note, per_issue_string, new_line, list_of_issues): return per_issue_string -def create_comment_for_output(tool_output, files_changed_in_pr, output_to_console): +def create_comment_for_output(tool_output, prefix, files_changed_in_pr, output_to_console): """ Generates a comment for a GitHub pull request based on the tool output. Parameters: tool_output (str): The tool output to parse. + prefix (str): The prefix to look for in order to identify issues. files_changed_in_pr (dict): A dictionary containing the files that were changed in the pull request and the lines that were modified. output_to_console (bool): Whether or not to output the results to the console. @@ -443,14 +449,14 @@ def create_comment_for_output(tool_output, files_changed_in_pr, output_to_consol was_note = False for line in tool_output: - if not line[0].isspace() and not is_excluded_dir(line): + if line.startswith(prefix) and not is_excluded_dir(line): ( file_path, is_note, file_line_start, file_line_end, issue_description, - ) = extract_info(line) + ) = extract_info(line, prefix) # In case where we only output to console, skip the next part if output_to_console: @@ -523,14 +529,16 @@ def read_files_and_parse_results(): common_ancestor = parser.parse_args().common feature_branch = parser.parse_args().head - debug_print(f"cppcheck result: \n {cppcheck_content} \n") + line_prefix = f"{WORK_DIR}" + + debug_print(f"cppcheck result: \n {cppcheck_content} \n" f"line_prefix: {line_prefix} \n") files_changed_in_pr = {} if not output_to_console and (ONLY_PR_CHANGES == "true"): files_changed_in_pr = get_changed_files(common_ancestor, feature_branch) cppcheck_comment, cppcheck_issues_found = create_comment_for_output( - cppcheck_content, files_changed_in_pr, output_to_console + cppcheck_content, line_prefix, files_changed_in_pr, output_to_console ) if output_to_console and cppcheck_issues_found: diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 1eba5f40..5fc2697a 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -12,6 +12,8 @@ jobs: git clone https://github.com/danmar/cppcheck.git cd cppcheck git checkout 2.13.x + # Disable color output of cppcheck + sed -i 's/ *bool *gDisableColors *= *false;/bool gDisableColors = true;/' lib/color.cpp sudo make -j4 MATCHCOMPILER=yes FILESDIR=/usr/share/cppcheck HAVE_RULES=yes CXXFLAGS="-O2 -DNDEBUG -Wall -Wno-sign-compare -Wno-unused-function" install - name: Install Python dependencies @@ -52,9 +54,9 @@ jobs: run: | analysis_file="analysis.txt" cppcheck_params="--enable=warning --check-level=exhaustive --suppress=internalError --suppress=internalAstError" - cppcheck -j4 $cppcheck_params --project=build/compile_commands.json --output-file=$analysis_file + cppcheck -j4 $cppcheck_params --project=$(pwd)/build/compile_commands.json --output-file=$analysis_file # Since cppcheck does not support OpenCL and CUDA, we need to check these files separately - find reg-lib/cl/. -name "*.cl" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done - find reg-lib/cuda/. -name "*.cu" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done + find $(pwd)/reg-lib/cl/. -name "*.cl" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done + find $(pwd)/reg-lib/cuda/. -name "*.cu" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done cat $analysis_file python3 ${{ github.workspace }}/.github/code_analysis.py -cc $analysis_file -o false -fk false \ No newline at end of file diff --git a/niftyreg_build_version.txt b/niftyreg_build_version.txt index 2b20fd09..29ba0dff 100644 --- a/niftyreg_build_version.txt +++ b/niftyreg_build_version.txt @@ -1 +1 @@ -449 +450