172 lines
6.5 KiB
Ruby
172 lines
6.5 KiB
Ruby
class Api::V1::SonarqubesController < Api::V1::BaseController
|
|
before_action :load_repository
|
|
def sonar_initialize
|
|
gitea_params = { has_actions: params[:has_actions] == 'true' ? true :false }
|
|
gitea_setting = Gitea::Repository::UpdateService.call(@owner, @project.identifier, gitea_params)
|
|
if gitea_setting['has_actions'] == true
|
|
Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_HOST_URL', Rails.application.config_for(:configuration)['sonarqube']['url'] ).call
|
|
Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_TOKEN', Rails.application.config_for(:configuration)['sonarqube']['secret'] ).call
|
|
else
|
|
Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_HOST_URL', Rails.application.config_for(:configuration)['sonarqube']['url'] ).destroy
|
|
Gitea::Repository::ActionSecretsService.new(@owner, @project.identifier, 'SONAR_TOKEN', Rails.application.config_for(:configuration)['sonarqube']['secret'] ).destroy
|
|
end
|
|
@project.update(gitea_params)
|
|
render_ok
|
|
end
|
|
|
|
def insert_file
|
|
checkout_url = 'https://gitlink.org.cn/KingChan/checkout@v4'
|
|
scanner_url = 'https://gitlink.org.cn/KingChan/sonarqube-scan-action@master'
|
|
begin
|
|
config = Rails.application.config_for(:configuration)
|
|
sonarqube_config = config.dig('sonarqube')
|
|
|
|
if sonarqube_config.present? && sonarqube_config['checkout'].present?
|
|
checkout_url = sonarqube_config['checkout']
|
|
end
|
|
if sonarqube_config.present? && sonarqube_config['scanner'].present?
|
|
scanner_url = sonarqube_config['scanner']
|
|
end
|
|
|
|
raise 'sonar config missing' if sonarqube_config.blank?
|
|
rescue => ex
|
|
raise ex if Rails.env.production?
|
|
|
|
puts %Q{\033[33m [warning] soanrqube config or configuration.yml missing,
|
|
please add it or execute 'cp config/configuration.yml.example config/configuration.yml' \033[0m}
|
|
end
|
|
|
|
sonar_scanner_content = {
|
|
filepath: '.gitea/workflows/SonarScanner.yaml',
|
|
branch: params[:branch],
|
|
new_branch: nil,
|
|
content: "
|
|
on:
|
|
# Trigger analysis when pushing to your main branches, and when creating a pull request.
|
|
push:
|
|
branches:
|
|
- main
|
|
- master
|
|
- develop
|
|
- 'releases/**'
|
|
pull_request:
|
|
types: [opened, synchronize, reopened]
|
|
|
|
name: Main Workflow
|
|
jobs:
|
|
sonarqube:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: #{checkout_url}
|
|
with:
|
|
# Disabling shallow clones is recommended for improving the relevancy of reporting
|
|
fetch-depth: 0
|
|
- name: SonarQube Scan
|
|
uses: #{scanner_url}
|
|
env:
|
|
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
|
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
|
|
",
|
|
message: 'Add .gitea/workflows/SonarScanner.yaml',
|
|
committer: {
|
|
email: @owner.mail,
|
|
name: @owner.login
|
|
},
|
|
identifier: @project.identifier
|
|
}
|
|
@path = GiteaService.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{params[:branch]}/"
|
|
sonar_scanner_exit = Repositories::EntriesInteractor.call(@owner, @project.identifier, '.gitea/workflows/SonarScanner.yaml', ref: params[:branch])
|
|
if sonar_scanner_exit.success?
|
|
Gitea::UpdateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_scanner_content.merge(sha:sonar_scanner_exit.result['sha']))
|
|
else
|
|
sonar_scanner_content[:content] = Base64.strict_encode64(sonar_scanner_content[:content])
|
|
Gitea::CreateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_scanner_content)
|
|
end
|
|
|
|
sonar_project_content = {
|
|
filepath: 'sonar-project.properties',
|
|
branch: params[:branch],
|
|
new_branch: nil,
|
|
"content": "sonar.projectKey=#{params[:owner]}-#{@project.id}\nsonar.sources=.\nsonar.java.binaries=.",
|
|
"message": 'Add sonar-project.properties',
|
|
committer: {
|
|
email: @owner.mail,
|
|
name: @owner.login
|
|
},
|
|
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']))
|
|
else
|
|
sonar_project_content[:content] = Base64.strict_encode64(sonar_project_content[:content])
|
|
Gitea::CreateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_project_content)
|
|
end
|
|
render_ok
|
|
end
|
|
|
|
def issues_search
|
|
params_data = {
|
|
components: "#{params[:owner]}-#{@project.id}",
|
|
s: params[:s],
|
|
impactSoftwareQualities: params[:impactSoftwareQualities],
|
|
issueStatuses: params[:issueStatuses],
|
|
ps: params[:ps],
|
|
p: params[:p],
|
|
facets: params[:facets],
|
|
additionalFields: params[:additionalFields],
|
|
timeZone: params[:timeZone],
|
|
types: params[:types],
|
|
impactSeverities: params[:impactSeverities],
|
|
tags: params[:tags]
|
|
}
|
|
data = Sonarqube.client.get('/api/issues/search', query: params_data)
|
|
render_ok data
|
|
end
|
|
|
|
def ce_component
|
|
params_data = {
|
|
components: "#{params[:owner]}-#{@project.id}",
|
|
}
|
|
data = Sonarqube.client.get('/api/ce/component', query: params_data)
|
|
render_ok data
|
|
end
|
|
|
|
def sources_issue_snippet
|
|
params_data = {
|
|
issueKey: params[:issueKey]
|
|
}
|
|
data = Sonarqube.client.get('/api/sources/issue_snippets', query: params_data)
|
|
render_ok data
|
|
end
|
|
|
|
def rules_show
|
|
params_data = {
|
|
key: params[:key]
|
|
}
|
|
data = Sonarqube.client.get('/api/rules/show', query: params_data)
|
|
render_ok data
|
|
end
|
|
|
|
def measures_search_history
|
|
params_data = {
|
|
from: params[:form],
|
|
component: "#{params[:owner]}-#{@project.id}",
|
|
metrics: params[:metrics],
|
|
ps: params[:ps]
|
|
}
|
|
data = Sonarqube.client.get('/api/measures/search_history', query: params_data)
|
|
render_ok data
|
|
end
|
|
|
|
def measures_component
|
|
params_data = {
|
|
component: "#{params[:owner]}-#{@project.id}",
|
|
additionalFields: params[:additionalFields],
|
|
metricKeys: params[:metricKeys]
|
|
}
|
|
data = Sonarqube.client.get('/api/measures/component', query: params_data)
|
|
render_ok data
|
|
end
|
|
end
|