static-code-analysis.yml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # pre_check stage
  2. clang_tidy_check:
  3. extends:
  4. - .pre_check_base_template
  5. - .rules:patterns:clang_tidy
  6. image: ${CLANG_STATIC_ANALYSIS_IMAGE}
  7. artifacts:
  8. paths:
  9. - $OUTPUT_DIR
  10. when: always
  11. expire_in: 1 day
  12. variables:
  13. CLANG_TIDY_RUNNER_PROJ: 2107 # idf/clang-tidy-runner
  14. CLANG_TIDY_DIRS_TXT: ${CI_PROJECT_DIR}/tools/ci/clang_tidy_dirs.txt
  15. RULES_FILE: ${CI_PROJECT_DIR}/tools/ci/static-analysis-rules.yml
  16. OUTPUT_DIR: ${CI_PROJECT_DIR}/clang_tidy_reports
  17. script:
  18. - python -m pip install -U pip
  19. - internal_pip_install $CLANG_TIDY_RUNNER_PROJ pyclang
  20. - export PATH=$PATH:$(python -c "import sys; print(sys.executable.rsplit('/', 1)[0])")
  21. - dirs=$(cat ${CLANG_TIDY_DIRS_TXT} | while read line; do echo ${CI_PROJECT_DIR}/${line}; done | xargs)
  22. - run_cmd idf_clang ${dirs}
  23. --output-path ${OUTPUT_DIR}
  24. --limit-file ${RULES_FILE}
  25. --xtensa-include-dir
  26. --run-clang-tidy-py ${RUN_CLANG_TIDY_PY}
  27. check_pylint:
  28. extends:
  29. - .pre_check_base_template
  30. - .rules:patterns:python-files
  31. - .before_script_minimal
  32. image: $SONARQUBE_SCANNER_IMAGE
  33. artifacts:
  34. when: always
  35. paths:
  36. - pylint-report.txt
  37. expire_in: 1 week
  38. script:
  39. - export PYTHONPATH="$IDF_PATH/tools:$IDF_PATH/tools/ci/python_packages:$PYTHONPATH"
  40. - |
  41. if [ -n "$CI_MERGE_REQUEST_IID" ]; then
  42. export files=$(python ${CI_PROJECT_DIR}/tools/ci/ci_get_mr_info.py files ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} | grep ".py");
  43. else
  44. export files=$(find . -iname "*.py" -print);
  45. fi
  46. - pylint --rcfile=.pylintrc $files -r n --output-format=parseable > pylint-report.txt || exit 0
  47. # build stage
  48. # Sonarqube related jobs put here for this reason:
  49. # Here we have two jobs. code_quality_check and code_quality_report.
  50. #
  51. # code_quality_check will analyze the code changes between your MR and
  52. # code repo stored in sonarqube server. The analysis result is only shown in
  53. # the comments under this MR and won't be transferred to the server.
  54. #
  55. # code_quality_report will analyze and transfer both of the newly added code
  56. # and the analysis result to the server.
  57. #
  58. # Put in the front to ensure that the newly merged code can be stored in
  59. # sonarqube server ASAP, in order to avoid reporting unrelated code issues
  60. .sonar_scan_template:
  61. stage: build
  62. image:
  63. name: $SONARQUBE_SCANNER_IMAGE
  64. before_script:
  65. - source tools/ci/utils.sh
  66. - export PYTHONPATH="$CI_PROJECT_DIR/tools:$CI_PROJECT_DIR/tools/ci/python_packages:$PYTHONPATH"
  67. - fetch_submodules
  68. # Exclude the submodules, all paths ends with /**
  69. - submodules=$(get_all_submodules)
  70. # get all exclude paths specified in tools/ci/sonar_exclude_list.txt | ignore lines start with # | xargs | replace all <space> to <comma>
  71. - custom_excludes=$(cat $CI_PROJECT_DIR/tools/ci/sonar_exclude_list.txt | grep -v '^#' | xargs | sed -e 's/ /,/g')
  72. # Exclude the report dir as well
  73. - export EXCLUSIONS="$custom_excludes,$submodules"
  74. - export SONAR_SCANNER_OPTS="-Xmx2048m"
  75. variables:
  76. GIT_DEPTH: 0
  77. REPORT_PATTERN: clang_tidy_reports/*.txt
  78. artifacts:
  79. when: always
  80. paths:
  81. - $REPORT_PATTERN
  82. tags:
  83. - host_test
  84. dependencies: # Here is not a hard dependency relationship, could be skipped when only python files changed. so we do not use "needs" here.
  85. - clang_tidy_check
  86. - check_pylint
  87. code_quality_check:
  88. extends:
  89. - .sonar_scan_template
  90. - .rules:patterns:static-code-analysis-preview
  91. allow_failure: true # since now it's using exit code to indicate the code analysis result,
  92. # we don't want to block ci when critical issues founded
  93. script:
  94. - export CI_MERGE_REQUEST_COMMITS=$(python ${CI_PROJECT_DIR}/tools/ci/ci_get_mr_info.py commits ${CI_COMMIT_REF_NAME} | tr '\n' ',')
  95. # test if this branch have merge request, if not, exit 0
  96. - test -n "$CI_MERGE_REQUEST_IID" || exit 0
  97. - test -n "$CI_MERGE_REQUEST_COMMITS" || exit 0
  98. - sonar-scanner
  99. -Dsonar.analysis.mode=preview
  100. -Dsonar.branch.name=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
  101. -Dsonar.cxx.clangtidy.reportPath=$REPORT_PATTERN
  102. -Dsonar.exclusions=$EXCLUSIONS
  103. -Dsonar.gitlab.ci_merge_request_iid=$CI_MERGE_REQUEST_IID
  104. -Dsonar.gitlab.commit_sha=$CI_MERGE_REQUEST_COMMITS
  105. -Dsonar.gitlab.merge_request_discussion=true
  106. -Dsonar.gitlab.ref_name=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
  107. -Dsonar.host.url=$SONAR_HOST_URL
  108. -Dsonar.login=$SONAR_LOGIN
  109. -Dsonar.python.pylint.reportPath=pylint-report.txt
  110. code_quality_report:
  111. extends:
  112. - .sonar_scan_template
  113. - .rules:protected
  114. allow_failure: true # since now it's using exit code to indicate the code analysis result,
  115. # we don't want to block ci when critical issues founded
  116. script:
  117. - sonar-scanner
  118. -Dsonar.branch.name=$CI_COMMIT_REF_NAME
  119. -Dsonar.cxx.clangtidy.reportPath=$REPORT_PATTERN
  120. -Dsonar.exclusions=$EXCLUSIONS
  121. -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA
  122. -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
  123. -Dsonar.host.url=$SONAR_HOST_URL
  124. -Dsonar.login=$SONAR_LOGIN
  125. -Dsonar.python.pylint.reportPath=pylint-report.txt