static-code-analysis.yml 5.0 KB

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