diff --git a/app/controllers/api/v1/sonarqubes_controller.rb b/app/controllers/api/v1/sonarqubes_controller.rb index 4da8bd753..cf5437f02 100644 --- a/app/controllers/api/v1/sonarqubes_controller.rb +++ b/app/controllers/api/v1/sonarqubes_controller.rb @@ -1,5 +1,8 @@ class Api::V1::SonarqubesController < Api::V1::BaseController before_action :load_repository + include Repository::LanguagesPercentagable + include SonarService + def sonar_initialize gitea_params = { has_actions: params[:has_actions] == 'true' ? true :false } gitea_setting = Gitea::Repository::UpdateService.call(@owner, @project.identifier, gitea_params) @@ -89,11 +92,13 @@ class Api::V1::SonarqubesController < Api::V1::BaseController Gitea::CreateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_scanner_content) end + sonar_content = build_sonar_content(params[:owner], @project.id, languages_precentagable.keys) + sonar_project_content = { filepath: 'sonar-project.properties', branch: params[:branch], new_branch: nil, - "content": "sonar.projectKey=#{params[:owner]}-#{@project.id}\nsonar.projectVersion=1.0\nsonar.sourceEncoding=UTF-8\nsonar.sources=.\nsonar.java.binaries=.\nsonar.javascript.lcov.reportPaths=.\nsonar.python.coverage.reportPaths=.\nsonar.cxx.file.suffixes=.h,.cpp", + "content": sonar_content, "message": 'Add sonar-project.properties', committer: { email: @owner.mail, @@ -101,6 +106,7 @@ class Api::V1::SonarqubesController < Api::V1::BaseController }, identifier: @project.identifier } + sonar_project_exit = Repositories::EntriesInteractor.call(@owner, @project.identifier, 'sonar-project.properties', ref: params[:branch]) if sonar_project_exit.success? Gitea::UpdateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_project_content.merge(sha:sonar_project_exit.result['sha'])) diff --git a/app/libs/sonar_service.rb b/app/libs/sonar_service.rb new file mode 100644 index 000000000..57eba3267 --- /dev/null +++ b/app/libs/sonar_service.rb @@ -0,0 +1,46 @@ +module SonarService + class << self + def build_sonar_content(owner,project_id,lang_keys) + lang_map = { + 'Java' => :java, + 'Python' => :python, + 'JavaScript' => :js, + 'TypeScript' => :ts, + 'C++' => :cpp, + 'C' => :cpp + } + + detected_langs = lang_keys.map { |l| lang_map[l] }.compact.to_set + + lines = [] + lines << "sonar.projectKey=#{owner}-#{project_id}" + lines << "sonar.projectVersion=1.0" + lines << "sonar.sourceEncoding=UTF-8" + lines << "sonar.sources=." + lines << "sonar.exclusions=**/test/**,**/tests/**,**/vendor/**,**/node_modules/**,**/__pycache__/**" + + if detected_langs.include?(:java) + lines << "sonar.java.binaries=target/classes,build/classes,out/production" + end + + if detected_langs.include?(:js) + lines << "sonar.javascript.lcov.reportPaths=coverage/lcov.info" + end + + if detected_langs.include?(:ts) + lines << "sonar.typescript.tsconfigPath=tsconfig.json" + end + + if detected_langs.include?(:python) + lines << "sonar.python.coverage.reportPaths=coverage.xml" + end + + if detected_langs.include?(:cpp) + lines << "sonar.cxx.file.suffixes=.cpp,.c,.cc,.h,.hpp,.hh" + lines << "sonar.cxx.errorRecoveryEnabled=true" + end + + lines.join("\n") + end + end +end \ No newline at end of file