change sonar content

This commit is contained in:
呱呱呱 2025-06-04 09:18:26 +08:00
parent fa2448b94d
commit 1acac5d39a
2 changed files with 53 additions and 1 deletions

View File

@ -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']))

46
app/libs/sonar_service.rb Normal file
View File

@ -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