static-code-analysis.yml 4.4 KB

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