static-code-analysis.yml 4.8 KB

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