Browse Source

新增sonar action文件生成

pull/347/head
kingChan 2 years ago
parent
commit
69568cf626
4 changed files with 89 additions and 7 deletions
  1. +51
    -4
      app/controllers/api/v1/sonarqubes_controller.rb
  2. +33
    -0
      app/services/gitea/repository/action_secrets_service.rb
  3. +4
    -2
      config/initializers/sonarqube.rb
  4. +1
    -1
      config/routes/api.rb

+ 51
- 4
app/controllers/api/v1/sonarqubes_controller.rb View File

@@ -1,15 +1,62 @@
class Api::V1::SonarqubesController < Api::V1::BaseController
before_action :load_repository
def sonar_initialize
gitea_params = { has_actions: true }
Gitea::Repository::UpdateService.call(@owner, @project.identifier, gitea_params)
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
render_ok
end

def execute_sonar_sanner
def insert_file
sonar_scanner_content = {
filepath: '.gitea/workflows/SonarScanner.yaml',
branch: params[:branch],
new_branch: nil,
content: 'b246CiAgIyBUcmlnZ2VyIGFuYWx5c2lzIHdoZW4gcHVzaGluZyB0byB5b3VyIG1haW4gYnJhbmNoZXMsIGFuZCB3aGVuIGNyZWF0aW5nIGEgcHVsbCByZXF1ZXN0LgogIHB1c2g6CiAgICBicmFuY2hlczoKICAgICAgLSBtYWluCiAgICAgIC0gbWFzdGVyCiAgICAgIC0gZGV2ZWxvcAogICAgICAtICdyZWxlYXNlcy8qKicKICBwdWxsX3JlcXVlc3Q6CiAgICAgIHR5cGVzOiBbb3BlbmVkLCBzeW5jaHJvbml6ZSwgcmVvcGVuZWRdCgpuYW1lOiBNYWluIFdvcmtmbG93CmpvYnM6CiAgc29uYXJxdWJlOgogICAgcnVucy1vbjogdWJ1bnR1LWxhdGVzdAogICAgc3RlcHM6CiAgICAtIHVzZXM6IGFjdGlvbnMvY2hlY2tvdXRAdjQKICAgICAgd2l0aDoKICAgICAgICAjIERpc2FibGluZyBzaGFsbG93IGNsb25lcyBpcyByZWNvbW1lbmRlZCBmb3IgaW1wcm92aW5nIHRoZSByZWxldmFuY3kgb2YgcmVwb3J0aW5nCiAgICAgICAgZmV0Y2gtZGVwdGg6IDAKICAgIC0gbmFtZTogU29uYXJRdWJlIFNjYW4KICAgICAgdXNlczogc29uYXJzb3VyY2Uvc29uYXJxdWJlLXNjYW4tYWN0aW9uQG1hc3RlcgogICAgICBlbnY6CiAgICAgICAgU09OQVJfVE9LRU46ICR7eyBzZWNyZXRzLlNPTkFSX1RPS0VOIH19CiAgICAgICAgU09OQVJfSE9TVF9VUkw6ICAke3sgc2VjcmV0cy5TT05BUl9IT1NUX1VSTCB9fQ==',
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?
sonar_scanner_content[:content] = Base64.decode64(sonar_scanner_content[:content])
Gitea::UpdateFileInteractor.call(@owner.gitea_token, @owner.login, sonar_scanner_content.merge(sha:sonar_scanner_exit.result['sha']))
else
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]}-#{params[:repo]}\nsonar.sources=.",
"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[:components],


+ 33
- 0
app/services/gitea/repository/action_secrets_service.rb View File

@@ -0,0 +1,33 @@
class Gitea::Repository::ActionSecretsService < Gitea::ClientService
attr_reader :owner, :repo, :secret_name, :secret

def initialize(owner, repo, secret_name, secret)
@owner = owner
@repo = repo
@secret_name = secret_name
@secret = secret
end

def call
response = put(url, request_params)
render_201_response(response)
end

def destroy
response = delete(url, request_params)
render_201_response(response)
end


private

def request_params
Hash.new.merge(token: owner.gitea_token, data: { data: secret } )
end



def url
"/repos/#{owner.login}/#{repo}/actions/secrets/#{secret_name}".freeze
end
end

+ 4
- 2
config/initializers/sonarqube.rb View File

@@ -1,6 +1,8 @@
sonarqube_config = Rails.application.config_for(:configuration)['sonarqube']

Sonarqube.configure do |config|
config.endpoint = 'http://172.20.32.202:9999' # API endpoint URL, default: ENV['SONARQUBE_API_ENDPOINT']
config.private_token = 'squ_fb81f52a7b2c2db00c71c29f71c9595f48c2ff3f' # user's private token, default: ENV['SONARQUBE_API_PRIVATE_TOKEN']
config.endpoint = sonarqube_config["url"] # API endpoint URL, default: ENV['SONARQUBE_API_ENDPOINT']
config.private_token = sonarqube_config["secret"] # user's private token, default: ENV['SONARQUBE_API_PRIVATE_TOKEN']
# Optional
# config.user_agent = 'Custom User Agent' # user agent, default: 'Sonarqube Ruby Gem [version]'
end

+ 1
- 1
config/routes/api.rb View File

@@ -89,7 +89,7 @@ defaults format: :json do
get :measures_component

post :sonar_initialize
post :execute_sonar_sanner
post :insert_file

end
end


Loading…
Cancel
Save